I'm building an iOS app in which I allow the user to navigate through products and order them. When the user taps order, I want to save his/her username, email, address, date of delivery and of course the array of products he ordered into my mysql database. I need to be able to see the order on my other app which I'm making for businesses.
Saving strings like a username or address is not a problem, but the saving the array of products (represented by custom objects) seems to be going wrong. my database does not return the array it should return when I try to download it on my other app, And i can'f figure out why.
My custom object has the next properties:
itemName, itemCode, itemBarCode, itemCompany, itemPrice, itemQuantity, itemUserRemark, itemCompany, productImageNumber
each variable is a string, and the object represents one item in the shopping cart (represented by orderProducts array) of the user.
I send the order to my web service as follows:
NSString *orderToSend = [NSString stringWithFormat:@"http://www.imagine-app.nl/orderInsert.php?user=%@&orderAdressStreet=%@&orderAdressZip=%@&orderAdressNumber=%@&orderAdressCity=%@&paymentType=%@&orderDate=%@&orderDelivery=%@&orderProducts=%@&company=%@", orderClass.user, orderClass.orderAdressStreet, orderClass.orderAdressZip, orderClass.orderAdressNumber, orderClass.orderAdressCity, orderClass.orderPaymentType, orderClass.orderDate, orderClass.orderDelivery, orderClass.orderProducts, orderClass.company];
NSString *strURL = [orderToSend stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"connection %@", strURL);
and on my web service:
<?php
if (isset($_GET["user"]) && isset($_GET["orderAdressStreet"]) && isset($_GET["orderAdressZip"]) && isset($_GET["orderAdressNumber"]) && isset($_GET["orderAdressCity"]) && isset($_GET["paymentType"]) && isset($_GET["orderDate"]) && isset($_GET["orderDelivery"]) && isset($_GET["orderProducts"]) && isset($_GET["company"]) ){
$user = $_GET["user"];
$orderAdressStreet = $_GET["orderAdressStreet"];
$orderAdressZip = $_GET["orderAdressZip"];
$orderAdressNumber = $_GET["orderAdressNumber"];
$orderAdressCity = $_GET["orderAdressCity"];
$paymentType = $_GET["paymentType"];
$orderDate = $_GET["orderDate"];
$orderDelivery = $_GET["orderDelivery"];
$orderProducts = $_GET["orderProducts"];
$company = $_GET['company'];
$result = insertOrder($user, $orderAdressStreet, $orderAdressZip, $orderAdressNumber, $orderAdressCity, $paymentType, $orderDate, $orderDelivery, $orderProducts, $company);
echo $result;
}
function makeSqlConnection()
{
$DB_HostName = "xxxxx";
$DB_Name = "xxxxx";
$DB_User = "xxxxx";
$DB_Pass = "xxxxxxx";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
return $con;
}
function disconnectSqlConnection($con)
{
mysql_close($con);
}
function insertOrder($user, $orderAdressStreet, $orderAdressZip, $orderAdressNumber, $orderAdressCity, $paymentType, $orderDate, $orderDelivery, $orderProducts, $company)
{
$con = makeSqlConnection();
$sqlInsert = "INSERT INTO Orders (user, orderAdressStreet, orderAdressZip, orderAdressNumber, orderAdressCity, paymentType, orderDate, orderDelivery, orderProducts, company) VALUES ('$user', '$orderAdressStreet', '$orderAdressZip', '$orderAdressNumber', '$orderAdressCity', '$paymentType', '$orderDate', '$orderDelivery', '$orderProducts' , '$company');";
$insertNewItems = mysql_query($sqlInsert,$con) or die(mysql_error());
disconnectSqlConnection($con);
}
?>
this seems to work since all the objects are inserted in my database. my orderProducts array, containing one ordered products, gets inserted in a cell like this:
( "" )
When I download this order on my business side app, I use this code:
on my server side:
<?php
if (isset($_GET["company"])){
$company = $_GET["company"];
$result = getOrders($company);
echo $result;
}
function makeSqlConnection()
{
$DB_HostName = "xxxxx";
$DB_Name = "xxxxx";
$DB_User = "xxxxx";
$DB_Pass = "xxxxxxxx";
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
return $con;
}
function disconnectSqlConnection($con)
{
mysql_close($con);
}
function getOrders($company)
{
$con = makeSqlConnection();
$query = mysql_query("SELECT * from items WHERE company = '$company'");
$ordersFromDatabase = array();
while ($row = mysql_fetch_assoc($query)) {
$orders['ordersFromDatabase'][] = $row;
}
print json_encode($orders);
$res = mysql_query($sql,$con) or die(mysql_error());
echo $res;
disconnectSqlConnection($con);
}
?>
and in the viewDidLoad of my app:
- (void)viewDidLoad
{
[super viewDidLoad];
Store* myStore = [Store sharedStore];
NSString *companyID = [[NSString alloc] initWithString: myStore.companyID];
NSURL *strUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.imagine-app.nl/getOrders.php?company=%@", companyID]];
NSLog(@"connection: %@", strUrl);
NSData *jsonData = [NSData dataWithContentsOfURL:strUrl];
NSLog(@"%@", jsonData);
NSMutableArray *ordersFromDataBbase = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL][@"ordersFromDatabase"];
NSLog(@"Products Array: %@", ordersFromDataBbase);
}
no matter what I do, the array is always null, and I'm nut sure if this is the right way of sending an array of custom objects to mysql.
So my final question, Does anybody know some sample code, a tutorial or just how to send and retrieve arrays of custom objects from Objective-c through php to and from mysql?
Thank you in advance, any held would be much appreciated