I am not sure how to write the following code.
$rowID = $_POST['rowID'];
if ($listing = $Listings->getData($rowID)) {
$jsonArray = array(
'listing_number' => $listing['listing_number'],
);
exit(json_encode($jsonArray));
}
When I do it like that, the response is Undefined Index: listing_number.
However, If I write it like this,
$rowID = $_POST['rowID'];
if ($listing = $Listings->getData($rowID)) {
$jsonArray = array(
'listing_number' => $listing[0],
);
exit(json_encode($jsonArray));
}
The response is
{"listing_number":{"id":"24","client_id":"1","address":"","address_2":"","city":"","state":"","zip":"","price":"","listing_number":"asdasdasdasd","remarks":"","link":"","status":"","bd":"","ba":"","lot_sz":"","sq_ft":"","yr":"","type":"","thumb":""}}
Which lets me know my SQL is and PHP is correct, I just don't know how to access $listing['listing_number] correctly.
Any help would be appreciated.
as GrumpCrouton said in the comment, your query is returning an array of results. So if you want to access a value in the first result, you first need to access this result using it's index : $listing[0]->listing_number.
$rowID = $_POST['rowID'];
if ($listing = $Listings->getData($rowID)) {
$jsonArray = array(
'listing_number' => $listing[0]->listing_number,
);
exit(json_encode($jsonArray));
}
P.S. You can convert object to array using a simple cast ( $result = (array) $result ), but it is not a must in your case. Casting your object to array will allow you to acces it's data using result['key'] rather than result->key.
Related
I am querying MySQL to perform an operation and return an array. To do this I am using the following code:
$return_array = array(
"title" => "nearby media",
"nearby_media_list" => array()
);
$my_query = "select * from `Media`";
$result = $conn->query($my_query);
while($row = $result->fetch_assoc()) {
$mediaLat = $row["lat"];
$mediaLon = $row["lon"];
$calculated_distance = distance( $userLat, $userLon, $mediaLat, $mediaLon, "M");
if( $calculated_distance <= $distance_limit ) {
// Build array
$return_array["nearby_media_list"][] = array(
'uid' => $row['uid']
);
}
}
echo json_encode($return_array);
$conn->close();
?>
and my output looks like this:
{"title":"nearby media","nearby_media_list":[{"uid":"-Kn1f0jo_36qnQBCqjCq"}]}
Since the array only contains one type of data I don't need to store a key and don't need JSON. What I want is a simple array of the values separated by a token so I can take it apart more easily. desired outcome is something like
[value1##$%value2##$%]
Can anyone instruct me how to do this? I haven't had any luck
I have some user preferences I am keeping in an array in MySQL. This is how I get the current array:
$array = DB::queryFirstField("SELECT dashboard_array FROM compel_dashboard
WHERE user_id = %i", $user_id);
Which gives me:
"s:19:"dashboard-reccomend";"
Now if I want to add a new string ($dashboard_item contains a string like dashboard-progress) to this array I was thinking:
$array[] = serialize($dashboard_item);
$query = DB::update('compel_dashboard', array(
'dashboard_array' => $array
), "user_id=%s", $user_id);
When I make this call though it doesnt update. The array stays the same as my first call, nothing gets added. If I just update that dashboard_array field without trying to add to the array it replaces the value fine so I know its something I am doing with adding to the array.
This is what I did to get it to work. I had to unserialize the array before adding to it.
$dashboard = DB::queryFirstField("SELECT dashboard_array FROM compel_dashboard
WHERE user_id = %i", $user_id);
$array[] = unserialize($dashboard);
array_push($array, $dashboard_item);
$new_dash = serialize($array);
$query = DB::update('compel_dashboard', array(
'dashboard_array' => $new_dash
), "user_id=%s", $user_id);
I'm trying to get an output in json, but currently I have a to either group them together, or remove the key in the json output.
$output = array();
foreach ($data->results() as $data) {
$output[]['id'] = $data->id;
$output[]['userid'] = $data->userid;
$output[]['title'] = $data->title;
}
echo json_encode($output);
This current code will out following, but they don't group in id, userid and title as I would like it to do.
[{"id":"31"},{"userid":"1"},{"title":"Test 1"},{"id":"52"},{"userid":"1"},{"title":"Test 2"},{"id":"53"},{"userid":"1"},{"title":"Test 3"},{"id":"58"},{"userid":"1"},{"title":"Test 4"}]
I then tried to do it in another way, by giving the array a key to group them, but my problem is that the system dont regonize the code then, if they have the ID as key.
$output = array();
foreach ($data->results() as $data) {
$id = $data->id;
$output[$id]['id'] = $data->id;
$output[$id]['userid'] = $data->userid;
$output[$id]['title'] = $data->title;
}
{"31":{"id":"31","userid":"1","title":"Test 1"},"52":{"id":"52","userid":"1","title":"Test 2"},"53":{"id":"53","userid":"1","title":"Test 3"},"58":{"id":"58","userid":"1","title":"Test 4"}}
I would like it to remove the key which I have input. Currently I can't really think of a way to do it.
As Anant requested:
The output for $data->results() is all the informations from my database in a table. The problem is that I would like to change either the array $data->results(), but could not find a method for that, so choose to play around with this method instead shown above.
[{"id":"31","userid":"1","title":"Test 1"},{"id":"52","userid":"1","title":"Test 2"},{"id":"53","userid":"1","title":"Test 3"},{"id":"58","userid":"1","title":"Test 4"}]
Just create the new array in one step
$output = array();
foreach ($data->results() as $data) {
$output[] = array(
'id' => $data->id,
'userid' => $data->userid,
'title' => $data->title);
}
echo json_encode($output);
I'm using the Twilio API to send sms. I'm trying to change it so that I can send to a list of recipients from mysql results.
The example code given is:
$people = array(
"+14155551212" => "First Lastname",
);
My code is:
$people = array(
while($res = mysql_fetch_array($usersphone)) {
$people[$res['UserMobile']] = $res['UserFirstName'];
}
);
The syntax is bad but I can't figure out where.
You can't put control structures into arrays.
$people = array();
while ($res = mysql_fetch_array($usersphone)) {
$people[$res["UserMobile"]] = $res["UserFirstName"];
};
Also, there's a ton of posts here on SO that will tell you all about not using the mysql_* functions anymore, since they're deprecated.
You have logic in your array definition. You should define the array, and then populate it with the while.
// define the array
$people = array();
while($res = mysql_fetch_array($usersphone)) {
// populate key with mobile and value with name
$people[$res['UserMobile']] = $res['UserFirstName'];
}
I want to add a bit more information to a json object before sending it back to my app.
$sql = "SELECT * FROM users WHERE repo=?";
$q=$dbh->prepare($sql);
$q->execute(array($repo));
$res = $q->fetchAll(PDO::FETCH_OBJ);
$res['isnew']="1"; //this part isn't working
echo '{"items":'. json_encode($res) .'}';
The PDO query returns a result set like this when I echo($res)
Array{"items":[{"uid":"10","repo":"bnef"}]}
then it gets encoded back to jquery- echo '{"items":'. json_encode($res) .'}';
giving me
{"items":[{"uid":"10","repo":"bnef}]}
I'd like to add "isnew":"1" to that but when I try
$res['isnew']="1"; or array_merge I end up with
{"items":{"0":{"uid":"10","repo":"bnef"},"isnew":"1"}}
which doesn't work. I need
{"items":[{"uid":"10","repo":"bnef, "isnew":"1"}]}
Am I misguide in try to do this?
I misread your question and got confused on the code... you shoudl incat be dealign with an array initially try the following:
$sql = "SELECT * FROM users WHERE repo=?";
$q=$dbh->prepare($sql);
$q->execute(array($repo));
$items = $q->fetchAll(PDO::FETCH_OBJ);
// you actually wnt isnew as a property of each row
// so you need to loop over the results
foreach($items as $key => $item){
$item->isnew = 1;
}
echo json_encode(array(
'items' => $items
));
$res = $q->fetchAll(PDO::FETCH_OBJ);
$res['isnew']="1"; //this part isn't working
Its not working because you used FETCH_OBJ instead of FETCH_ASSOC so youre wokring with an StdObject instance not an array. In that case you need to use -> to assign:
$res = $q->fetchAll(PDO::FETCH_OBJ);
$res->isnew = "1";
Alternatively you could fetch as an associative array:
$res = $q->fetchAll(PDO::FETCH_ASSOC);
$res['isnew']="1"; //this will work now
Additionalyl i wouldnt try to manipulate the JSON serialized string. I would doo all modifications natively:
$items = array(
'items' => $res
);
echo json_encode($items);