I'm trying to output JSON in this format:
[{"name":"venue 1"}, {"name":"venue 2"}, {"name":"venue 3"}]
But it's currently coming out like this:
{"name":"venue 1"}{"name":"venue 2"}{"name":"venue 3"}
Here is my code:
query = "SELECT * FROM venues";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
$arr = array(
'name'=> $row['name']
);
print json_encode($arr);
What do I need to change?
Add $arr[] intead of $arr
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
$arr[] = array('name'=> $row['name'] );
}
echo json_encode($arr);
Try this:
$arr[] = array(
'name'=> $row['name']
);
Simply make an array like
$array[]["name"]="value1";
$array[]["name"]="value2";
$array[]["name"]="value3";
echo json_encode($array);
you will get the json data as:
[{"name":"value1"},{"name":"value2"},{"name":"value3"}]
which you wants. You have to make the array using looping statements according to your need.
Related
I have the following PHP code:
while($row = mysqli_fetch_array($query))
{
$data = $row['name'];
}
I fetch all the data with the column name "name" in database. How can I output it like this?
["John", "Doe", "Deer"]
You have to make $data as array type variable.
while($row = mysqli_fetch_array($query))
{
$data[] = $row['name'];
}
print_r($data); // required output
while($row = mysqli_fetch_array($query))
{
$data[] = $row['name'];
}
print_r($data); // output key wise display like
Array ( [0] => John [1] => Doe ) etc.
But output as you suggestion then just add json_encode()
print_r(json_encode($data)); // output like
["John", "Doe", "Deer"]
$data = [];
while($row = mysqli_fetch_array($query))
{
$data = $row['name'];
}
echo json_encode($data); // or you can use print_r for debugging.
You need to use json_encode() method to your array to be accepted in your jquery. Rearrange the code like following..
$data = array();
while($row = mysqli_fetch_array($query))
{
$data[] = $row['name'];
}
$new_array = json_encode($data);
echo $new_array; // use 'echo' to print. The json_encode() convert $data array to string.
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 used MySql_fetch_assoc and then json_encode
$type = mysql_query("SELECT type_food,category_food,name_food FROM menu ORDER BY type_food,category_food,name_food");
$rows = array();
while($t = mysql_fetch_assoc($type)) {
$rows[]= $t;
}
print json_encode($rows);
which gives me the following results:
{"type_food":"pizza","category_food":"Gourmet","name_food":"pepperoni"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"supreme"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"hawaiian"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"tropical"},
{"type_food":"pizza","category_food":"traditional","name_food":"margherita"},
{"type_food":"pizza","category_food":"traditional","name_food":"vegetarian"}
However I need to make a multidimensional json result like so:
{"pizza": [
{"Gourmet": [
{"pepperoni"},
{"supreme"},
{"hawaiian"},
{"tropical"}]},
{"traditional": [
{"margherita"},
{"vegetarian"}]}
]},
{"etc": [ ... ]}
Is this possible using MySQL associative arrays?
Try this:
$type = mysql_query("SELECT type_food,category_food,name_food FROM menu ORDER BY type_food,category_food,name_food");
$rows = array();
while($t = mysql_fetch_array($type)) {
$rows[$t['type_food']][$t['category_food']][]=$t['name_food'];
}
print json_encode($rows);
<?php
$str='[{"type_food":"pizza", "category_food":"Gourmet", "name_food":"pepperoni"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"supreme"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"hawaiian"},
{"type_food":"pizza","category_food":"Gourmet","name_food":"tropical"},
{"type_food":"pizza","category_food":"traditional","name_food":"margherita"},
{"type_food":"pizza","category_food":"traditional","name_food":"vegetarian"}]';
$rows=json_decode($str,true);
$res=array();
foreach($rows as $row)
{
$res[$row['type_food']][$row['category_food']][]=$row['name_food'];
}
echo json_encode($res);
?>
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);
?>
I want to get json with php encode function like the following
<?php
require "../classes/database.php";
$database = new database();
header("content-type: application/json");
$result = $database->get_by_name($_POST['q']); //$_POST['searchValue']
echo '{"results":[';
if($result)
{
$i = 1;
while($row = mysql_fetch_array($result))
{
if(count($row) > 1)
{
echo json_encode(array('id'=>$i, 'name' => $row['name']));
echo ",";
}
else
{
echo json_encode(array('id'=>$i, 'name' => $row['name']));
}
$i++;
}
}
else
{
$value = "FALSE";
echo json_encode(array('id'=>1, 'name' => "")); // output the json code
}
echo "]}";
i want the output json to be something like that
{"results":[{"id":1,"name":"name1"},{"id":2,"name":"name2"}]}
but the output json is look like the following
{"results":[{"id":1,"name":"name1"},{"id":2,"name":"name2"},]}
As you realize that there is comma at the end, i want to remove it so it can be right json syntax, if i removed the echo ","; when there's more than one result the json will generate like this {"results":[{"id":1,"name":"name1"}{"id":2,"name":"name2"}]} and that syntax is wrong too
Hope that everybody got what i mean here, any ideas would be appreciated
If I were you, I would not json_encode each individual array, but merge the arrays together and then json_encode the merged array at the end. Below is an example using 5.4's short array syntax:
$out = [];
while(...) {
$out[] = [ 'id' => $i, 'name' => $row['name'] ];
}
echo json_encode($out);
Do the json_encoding as the LAST step. Build your data structure purely in PHP, then encode that structure at the end. Doing intermediate encodings means you're basically building your own json string, which is always going to be tricky and most likely "broken".
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = array('id'=>$i, 'name' => $row['name']);
}
echo json_encode($data);
build it all into an array first, then encode the whole thing in one go:
$outputdata = array();
while($row = mysql_fetch_array($result)) {
$outputdata[] = $row;
}
echo json_encode($outputdata);