So I have a object that is successfully pulling all the rows from a database and storing them like so:
Object {models: Array[2]}
[Object, Object]
0: Object
address: "1234 Cooper"
firstname: "Rick"
lastname: "Bross"
address: "1234 Cooper"
__proto__: Object
How can I restructure this so I can just say:
alert(potentialModels.rickbross.firstname)
//rickbross = *whatever model i want to find*
and it output:
"Rick"
Here is how I am currently creating this object:
<?php
if($_SESSION['username']) {
$result = mysql_query("SELECT * FROM `potentials`") or die(mysql_error());
$rows = array();
//retrieve and print every record
while($r = mysql_fetch_assoc($result)){
// $rows[] = $r; has the same effect, without the superfluous data attribute
$rows[] = $r;
}
// now all the rows have been fetched, it can be encoded
$myJSON = json_encode(array('models' => $rows));
?>
and how I am getting it in the console:
var potentialModels = <?php print($myJSON); ?>;
console.log(potentialModels);
Change:
$rows[] = $r;
To:
$rows[strtolower($r['firstname'].$r['lastname'])] = $r;
And you can use:
alert( potentialModels.models.rickbross.firstname);
If you want to drop the models, stop adding it to your JSON here:
json_encode(array('models' => $rows));
By changing it to:
json_encode( $rows);
Related
I'm writing a PHP code for inserting the work order name in a spinner.
I need a JSON String called success = 1 along with JSON Array.
I want to use that JSON string in the android spinner.
I'm getting only JSON array.
here is my code:
<?PHP
require_once('connection.php');
$workName = "SELECT workorder_name FROM workorder_category";
$con=mysqli_connect($server_name,$user_name,$password,$db);
$r = mysqli_query($con,$workName);
$result = array();
$resultArr = array('success' => true);
while($row = mysqli_fetch_array($r)){
array_push($result,array('$workOrderName'=>$row['workorder_name']));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
I want an output as like this:
{"success":1,result":[{"$workOrderName":"electrician"},{"$workOrderName":"plumber"},{"$workOrderName":"carpenter"}]}
currently, I am getting the output like
{
"result": [
{
"$workOrderName": "electrician"
},
{
"$workOrderName": "plumber"
},
{
"$workOrderName": "carpenter"
}
]
}
Instead of creating two arrays, one for the result and one for the success, define the structure in just one array and append to that:
// The main structure
$result = [
'success' => 1,
'result' => [],
];
while($row = mysqli_fetch_array($r)){
// Append to the sub key "result" (which we already defined as an array)
$result['result'][] = ['$workOrderName'=>$row['workorder_name']];
}
// Just encode the complete array
echo json_encode($result);
Note: This example uses the short array syntax [] (introduced in PHP 5.4) instead of the long syntax: array(). They do the exact same thing.
I also changed array_push() to a shorter version: $someArray[] =.
$result = array();
$resultArr['success'] =true;
while($row = mysqli_fetch_array($r))
{
array_push($result,array('$workOrderName'=>$row['workorder_name']));
}
$resultArr['result'] = $result;
echo json_encode($resultArr);
This will work fine.
I am new to PHP so far I managed to get the following output from database in JSON format.I created an array result which returns workorderName, but I want to get workOrderId also in the same array so that I can use it in android string request.
{
"result": [
{
"$workOrderName": "electrician"
},
{
"$workOrderName": "plumber"
},
{
"$workOrderName": "carpenter"
}
]
}
my php code is
<?PHP
require_once('connection.php');
$workName = "SELECT work_order_id,workorder_name FROM workorder_category";
$con=mysqli_connect($server_name,$user_name,$password,$db);
$r = mysqli_query($con,$workName);
$result = array();
$resultArr = array('success' => true);
while($row = mysqli_fetch_array($r)){
array_push($result,array('$workOrderName'=>$row['workorder_name']));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
I want the Output like
{
"result": [
{
"$workOrderId":"1"
"$workOrderName": "electrician"
},
{
"$workOrderId":"2"
"$workOrderName": "plumber"
},
{
"$workOrderId":"3"
"$workOrderName": "carpenter"
}
]
}
I'm pretty sure you didn't mean to have the $ in the key, but this is easier and will give you the column names (work_order_id, workorder_name) from the table as keys. Make sure to use mysqli_fetch_assoc:
while($row = mysqli_fetch_assoc($r)){
$result[] = $row;
}
If you want to change the keys then you can alias them in the query and use the above:
$workName = "SELECT work_order_id AS workOrderID, workorder_name AS workOrderName FROM workorder_category";
Or you could build the array:
$result[] = array('workOrderID' => $row['work_order_id'],
'workOrderName' => $row['workorder_name']);
You need to tweak your code to add $workOrderId also in the array like below
array_push($result,array(
'$workOrderId' => $row['work_order_id']
'$workOrderName' => $row['workorder_name']
));
I am trying to know if the new data can be added to JSON before encoding it?
I am retrieving the data from MySQL database in the following way:
//fetch the data from the database
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$to_encode[] = $row;
}
which gives me this:
[
{
name: "aaa"
},
{
name: "bbb"
}
]
Then I encode it to JSON with:
$array1 = json_encode($to_encode)
I wanted to know if I can add more data into the array before encoding it to make it like this?
[
{
name: "aaa"
age: '5'
},
{
name: "bbb"
age: '5'
}
]
or should I decode the encoded JSON, add the new values and then encode it back?
Simply you can do like this:
//fetch the data from the database
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$to_encode[] = $row;
}
for ($i = 0; $i < count($to_encode); $i++) {
$to_encode[$i]['age'] = '14';
}
$array1 = json_encode($to_encode);
print_r($array1);
Try something like this :
$i=0;
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$to_encode[$i]["name"] = $row;
$to_encode[$i]["age"] = 5;
$i++;
}
$array1 = json_encode($to_encode)
You can push an array to the $row variable, the idea is to build the array before you use json_encode
$to_encode = [];
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$age = array('age'=>5);
array_push($to_encode,$row);
array_push($to_encode,$age);
}
$array = json_encode($to_encode);
I'm currently working to retrieve some data for which I want to use a FLOT line chart.
At present, this is how I am creating the array by getting data from the MYSQL database:
$data = array("label" => $title);
while($row = $query->fetch()) {
$data['data'][] = array($row[3], $row[2]);
}
$json = json_encode($data, JSON_NUMERIC_CHECK);
echo $json;
The above code output the following array (Based on the data from the Database):
{"label":"hosting","data":[[2,"06\/07\/2014"],[7,"09\/07\/2014"]]}
This is what I want, however I need to somehow change the formatting of the array so that it is correctly formatted for the FLOT charts.
How would I got about changing the array from this:
{"label":"hosting","data":[[2,"09\/07\/2014"],[2,"09\/07\/2014"]]}
To this:
{label: 'hosting', data: [[2,09\/07\/2014], [2,09\/07\/2014]]}
Using JQuery try this,
<script type="text/javascript">
var obj = jQuery.parseJSON ( ' + <?php echo $json; ?> + ' );
</script>
try doing this
$data = array(label => $title, data => array());
while($row = $query->fetch()) {
$data[data][] = array($row[3], $row[2]);
}
instead
$data = array("label" => $title);
while($row = $query->fetch()) {
$data['data'][] = array($row[3], $row[2]);
}
I have this json currently :
{"quest_id":"1","quest_title":"Buy 3 pints of draft and a large pizza and then get desert","quest_price":"15","quest_points":"100"}{"quest_id":"2","quest_title":"Hello WOrld","quest_price":"50","quest_points":"10"}
I was wondering how I could output this :
{"quests": {"quest_id":"1","quest_title":"Buy 3 pints of draft and a large pizza and then get desert","quest_price":"15","quest_points":"100"}{"quest_id":"2","quest_title":"Hello WOrld","quest_price":"50","quest_points":"10"}
}
Here is the code in php:
while($result=mysql_fetch_array($number, MYSQL_ASSOC)){
print(json_encode($result));
}
Try this:
$result = array('quests' => array());
while($row = mysql_fetch_array($number, MYSQL_ASSOC)){
$result['quests'][] = $row
}
echo json_encode($result);
If I understand correctly what you are trying to do, get a JSON packet with all the rows, then loop over them to put them in an array, then encode the whole array:
<?php
$result = mysql_query($query);
$out = array('quests' => array());
while ($row = mysql_fetch_assoc($result)) {
$out['quests'][] = $row;
}
print json_encode($out);
?>