how to POST multidimensional array to webserver DB

You must Login before you can answer or comment on any questions.

I am using the latest titanium SDK building an iphone/adroid app and xampp is my webserver on my mac osx

I have data in a titanium sqlite db and would like to transfer this data to the webserver db...i am having quite some issue doing this....I figured out how to do it with a single array but its quite odd with a multi it seems...here is my code

titanium side:

var db = Ti.Database.install('areas.sqlite','templates');
 
var dataArray = []
 
var rows = db.execute('SELECT * from properties')
 
    while (rows.isValidRow())
    {
        dataArray.push({address:'' + rows.fieldByName('address') + '', city:'' + rows.fieldByName('city') + '', state:'' + rows.fieldByName('state') + ''});
        rows.next();    
    };
 
var JSONstring = JSON.stringify(dataArray);
Titanium.API.log(dataArray);
Titanium.API.log(JSONstring);
Titanium.API.log(dataArray[20]['address']);
Titanium.API.log({address: JSONstring});
 
var loginReq = Titanium.Network.createHTTPClient();  
    loginReq.open("POST","http://localhost/arrays.php");  
 
        var params = dataArray 
 
       /* 
        {  
            username: 'username.value',  
            password: 'password.value'  
        };  
   */
 Titanium.API.log(params);
 
 
loginReq.send(JSONstring);
arrays.php
<?php  
// Connect to the database(host, username, password)  
$con = mysql_connect('localhost','root','root');  
if (!$con)  
{  
    echo "Failed to make connection.";  
    exit;  
}  
// Select the database. Enter the name of your database (not the same as the table name)  
$db = mysql_select_db('propinspect');  
if (!$db)  
{  
     die('Could not connect: ' . mysql_error());
}  
 
// $_POST['username'] and $_POST['password'] are the param names we sent in our click event in login.js  
$username = $_POST[1];  //have tried $_POST[address][1]; $_POST[1][address]
 
 
$sql2 = "INSERT INTO users (id, username, password, name, email) VALUES(17, '$username', 'test3', 'test3', 'test3@test.com')";
//$query = mysql_query($sql);  
mysql_query($sql2);
i was originally also not sending the jsonSTRING and just the data array...been trying a few different combinations. My goal is to transfer the db from titanium to the webserver so that i can use a server side script to generate a pdf of this data

1 Answer

Accepted Answer

Hi Tareq

Have you considered converting the array/json object to a string using;

var str = JSON.stringify(dataArray);
Then post str to PHP. You can the decode the string to JSON on the server and simply loop through that.

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
6 Comments
  • Thats exactly how I'd do it.

    — commented 10 months ago by Stephen Feather

  • so i did create var JSONstring = JSON.stringify(dataArray); and tried to send that ... i guess i'm lost with when you say "decode the string to JSON on the server and simply loop through that."

    any good tuts out there i can check to see that??

    — commented 10 months ago by tareq dowla

  • Hi

    You can use json_decode().

    Docs here

    Should have you sorted.

    — commented 10 months ago by Malcolm Hollingsworth

  • Show 3 more comments

Your Answer

Think you can help? Login to answer this question!