MyadsEditPage .ts
export class MyadsEditPage {
lastImage: string = null;
loading: Loading;
targetPath:string;
getMyads:any;
myAllCat:any;
subCat:any
cat_id:string;
sub_cat_id:string;
mycat:any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
public http:Http,
private camera: Camera,
private transfer: Transfer,
private file: File,
private filePath: FilePath,
public actionSheetCtrl: ActionSheetController,
public toastCtrl: ToastController,
public platform: Platform,
public loadingCtrl: LoadingController
) {
this.getMyads = navParams.get('GetMyad');
}
//------------------------------------- Start Take Photo ---------------------------------------------------
public presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Select Image Source',
buttons: [
{
text: 'Load from Gallery',
handler: () => {
this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
}
},
{
text: 'Use Camera',
handler: () => {
this.takePicture(this.camera.PictureSourceType.CAMERA);
}
},
{
text: 'Cancel',
role: 'cancel'
}
]
});
actionSheet.present();
}
public takePicture(sourceType) {
// Create options for the Camera Dialog
var options = {
quality: 100,
sourceType: sourceType,
saveToPhotoAlbum: true,
correctOrientation: true
};
// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
// Special handling for Android library
if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
});
} else {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}, (err) => {
this.presentToast('Error while selecting image.');
});
}
// Create a new name for the image
private createFileName() {
var d = new Date(),
n = d.getTime(),
newFileName = n + ".jpg";
return newFileName;
}
// Copy the image to a local folder
private copyFileToLocalDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
this.lastImage = newFileName;
}, error => {
this.presentToast('Error while storing file.');
});
}
private presentToast(text) {
let toast = this.toastCtrl.create({
message: text,
duration: 3000,
position: 'middle'
});
toast.present();
}
// Always get the accurate path to your apps folder
public pathForImage(img) {
if (img === null) {
return '';
} else {
return cordova.file.dataDirectory + img;
}
}
//------------------------------------- end Take Photo ---------------------------------------------------
//------------------------------------- Start Register ---------------------------------------------------
public submitAdd(data) : void {
alert(this.getMyads.img)
// Destination URL
var url = "mydomainName/addsApp/append/includes/classes/update.php";
// File for Upload
var targetPath = this.pathForImage(this.lastImage);
// File name only
var filename = this.lastImage;
var options = {
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
params : {
"fileName": filename,
"present_img":this.getMyads.img,
"addtitle": this.getMyads.addtitle,
"cat_id": this.cat_id,
"sub_cat_id": this.subCat.sub_cat_id,
"description": this.getMyads.description,
"price": this.getMyads.price,
"name": this.getMyads.username,
"phone": this.getMyads.userphone,
"city":this.getMyads.usercity,
"ad_store_id":this.getMyads.ad_store_id // this ad_store_id id get from my-ads.ts
}
};
const fileTransfer: TransferObject = this.transfer.create();
//this.loading = this.loadingCtrl.create({
//content: 'Uploading...',
//});
//this.loading.present();
// Use the FileTransfer to upload the image
fileTransfer.upload(targetPath, url, options).then(data => {
//this.loading.dismissAll()
this.presentToast('Your ad successfully Submited!');
//this.navCtrl.setRoot(HomePage);
}, err => {
//this.loading.dismissAll()
this.presentToast('Something worng. \n Try again!');
});
}
//------------------------------------- End Register ---------------------------------------------------
ionViewDidLoad() {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
this.http.get("http://www.spangu.com/addsApp/append/index.php/Getallcat")
.subscribe((res:Response)=>{
let dataTemp = res.json(); //dataTemp is the json object you showed me
this.myAllCat = dataTemp.GetCategotyList; //contains a [] object with all yours user
// alert(JSON.stringify(this.myAllCat.cat_name))
//here start the remove duplicate value from array
})
}
catNameSelect(CatName){
///alert(this.cat_id);
this.cat_id= CatName;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
this.http.get("http://www.spangu.com/addsApp/append/index.php/GetallSubcat/"+this.cat_id)
.subscribe((res:Response)=>{
let dataTemp = res.json(); //dataTemp is the json object you showed me
this.subCat = dataTemp.GetSubCategotyList; //contains a [] object with all yours user
})
}
catSubNameSelect(CatSubName){
this.sub_cat_id=CatSubName
}
}
update.php
<?php
header('Access-Control-Allow-Origin: *');
$target_path = "../../image-folders/";
$target_path1 = $target_path . basename( $_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path1)) {
echo "Upload and move success";
} else {
echo $target_path1;
echo "There was an error uploading the file, please try again!";
}
mysql_connect("localhost","username","password") or die("mysql not coneected".mysql_error());
mysql_select_db("dbName");
$addtitle = $_POST["addtitle"];
$cat_id = $_POST["cat_id"];
$sub_cat_id = $_POST["sub_cat_id"];
$ad_store_id = $_POST["ad_store_id"];
$description = $_POST["description"];
$price = $_POST["price"];
$img = $_FILES['file']['name'];
$present_img=$_POST["present_img"];
$name = $_POST["name"];
$phone = $_POST["phone"];
$city = $_POST["city"];
if($img!='')
{
$sql="UPDATE `ad_store` SET `addtitle`='$addtitle', `cat_id`= '$cat_id', `sub_cat_id`='$sub_cat_id', `user_id`, `description`='$description',
`price`= '$price',`username`= '$name', `userphone` = '$phone', `usercity` = '$city' WHERE `ad_store_id` = '$ad_store_id'";
}
else
{
$sql="UPDATE `ad_store` SET `addtitle`='$addtitle', `cat_id`= '$cat_id', `sub_cat_id`='$sub_cat_id', `user_id`, `description`='$description',
`price`= '$price',`img`= '$present_img',`username`= '$name', `userphone` = '$phone', `usercity` = '$city' WHERE `ad_store_id` = '$ad_store_id'";
}
$res=mysql_query($sql);
?>
Please help me anyone.