I want to achieve this output in php.
[
{
"id": 1388534400000,
"author": "Pete Hunt",
"text": "Hey there!"
},
{
"id": 1420070400000,
"author": "Paul O’Shannessy",
"text": "React is *great*!"
}
]
I have a while loop in my backend below.
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json['id'] = $row['id'];
$json['author'] = $row['author'];
$json['text'] = $row['text'];
}
echo json_encode($json);
It only returns the last row in the database, and I want to display them all.
Thank you.
If your $row contains only these three fields from database then use below code otherwise #govindkr13 answer
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
$json = [];
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[] = $row;
}
echo json_encode($json);
You are simply overwriting your $json array every time. Try this:
$i=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$i]['id'] = $row['id'];
$json[$i]['author'] = $row['author'];
$json[$i]['text'] = $row['text'];
$i++;
}
echo json_encode($json);
use this
$pull = "SELECT * FROM mydb";
$result = $con->query($pull);
$final = [];
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json['id'] = $row['id'];
$json['author'] = $row['author'];
$json['text'] = $row['text'];
$final[] = $json;
}
echo json_encode($final);
Maybe this can help.
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$row['id']]['author'] = $row['author'];
$json[$row['id']]['text'] = $row['text'];
}
$i=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$i]['id'] = $row['id'];
$json[$i]['author'] = $row['author'];
$json[$i]['text'] = $row['text'];
$i++;
}
echo json_encode($json);
try this
$count=0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$json[$count]['id'] = $row['id'];
$json[$count]['author'] = $row['author'];
$json[$count]['text'] = $row['text'];
$count++;
}
echo json_encode($json);
Related
i want my json response to be like this
{
subtotal="null",
create time ="2017-06-25 11:35:50",
products:[{product name:first,
price:55}
]}
but what i'm getting is this
[{subtotal="null", create time ="2017-06-25 11:35:50",
product name:first ,price:55},
]
here is my php script
<?php
require "connect.php";
$sql ="SELECT * FROM products_orders JOIN products ON (products.PRODUCT_CODE =products_orders.PRODUCT_CODE) LEFT JOIN offers ON (offers.OFFER_ID = products_orders.OFFER_ID) ";
$result = mysqli_query($conn, $sql) or die("Error in READING " . mysqli_error($conn));
$readsarray = array();
while($row =mysqli_fetch_assoc($result))
{
$readsarray[] = $row;
}
echo json_encode($readsarray, JSON_UNESCAPED_UNICODE);
$conn->close();
?>
i'm beginner i hope you can help me
thanks in advance
please check this answer.
$data = array();
$final_data = array();
$products = array();
while($row=mysqli_fetch_assoc($res)){
$data['subtotal'] = $row['subtotal'];
$data['create time'] = $row['create time'];
$products['product name'] = $row['product name'];
$products['price'] = $row['price'];
$data['products'][] = $products;
$final_data[] = $data;
}
echo json_encode($final_data, JSON_UNESCAPED_UNICODE);
$conn->close();
please check this code this json data store all order detail.
$data = array();
$final_data = array();
$products = array();
while($row=mysqli_fetch_assoc($res)){
$data['subtotal'] = $row['subtotal'];
$data['create time'] = $row['create time'];
$products['product name'] = $row['product name'];
$products['price'] = $row['price'];
$data['products'] = $products;
$final_data[] = $data;
}
echo json_encode($final_data, JSON_UNESCAPED_UNICODE);
$conn->close();
Try to define $readsarray in this way
$i=0;
while($row =mysqli_fetch_assoc($result))
{
$readsarray[$i]["subtotal"] = $row["subtotal"];
$readsarray[$i]["create time"]=$row["create time"];
$readsarray[$i]["products"]=array('product name'=>$row["product name"],'price'=>$row["price"]);
$i++;
}
echo json_encode($readsarray,true);
$conn->close();
?>
I am new to PHP. I wanted to add array in each object of array. It is array inside array and each array object contains a array. I also search on internet I didn't find anything related to this.
Here is my desire json
[{
"id":1,
"name":"Grey",
"list":[
{
"id":1,
"name":"60 X 60 ABC"
},
{
"id":2,
"name":"40 X 40 PQR"
},
{
"id":3,
"name":"45 X 45 XYZ"
}
]
},
{
"id":2,
"name":"Yarn",
"list":[
{
"id":4,
"name":"YARN ABC"
},
{
"id":5,
"name":"YARN XYZ"
}
]
}]
All this data is filled from mysql database.
Here is my php code
$sql = "select * from tblType";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($rows = $result->fetch_assoc()) {
$obj = new stdClass;
$obj->id = $rows["id"];
$obj->name = $rows["name"];
$obj->list = $array2;
$sql2 = "select * from tblQuality where typeId = $obj->id";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
$obj2 = new stdClass;
$obj2->id = $row["id"];
$obj2->name = $row["name"];
array_push($array2, $obj2);
}
}
array_push($array, $obj);
}
}
echo json_encode($array);
mysqli_close($conn);
I don't know how to add array inside array. Please help.
try this:
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$sql = "select * from tblType";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($rows = $result->fetch_assoc()) {
$obj = new stdClass;
$obj->id = $rows["id"];
$obj->name = $rows["name"];
$obj->list = array();
$sql2 = "select * from tblQuality where typeId = $obj->id";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
$obj2 = new stdClass;
$obj2->id = $row["id"];
$obj2->name = $row["name"];
array_push( $obj->list, $obj2);
}
}
array_push($array, $obj);
}
}
echo json_encode($array);
mysqli_close($conn);
Here you are:
$sql = "select * from tblType";
$result = $conn->query($sql);
$results = [];
if ($result->num_rows > 0) {
while($rows = $result->fetch_assoc()) {
$item = [];
$item['id'] = $rows['id']
$item['name'] = $rows['name'];
$item['list'] = [];
$sql2 = "select * from tblQuality where typeId = " . $rows['id'];
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
$item['list'][] = [
'id' => $row['id'],
'name' => $row['name']
]
}
}
$results[] = $item
}
}
echo json_encode($results);
mysqli_close($conn);
Hint: don't use stdClass objects as arrays. They're not meant for that.
By using below code
$data = array();
$sql = "SELECT * FROM list";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$data['title'] = $row['title'];
$data['name'] = $row['name'];
}
}
echo json_encode($data);
I got 1 result, I can get full result if I do $data[] = $row['title'], but I want to make the result like this
{'title' : ['title 1','title 2'], 'name':['John','Amy']}
You are overwriting the title in each loop iteration. You need to accumulate all the titles and then set it in your data array.
$data = array();
$sql = "SELECT title FROM mainlist";
$result = $db->query($sql);
$titles = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$titles[] = $row['title'];
}
}
$data['title'] = $titles;
echo json_encode($data);
The easiest away is probably this:
$rows = array();
$sql = "SELECT * FROM list";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
}
echo json_encode($rows);
you could achieve by using group_concat on each of the columns in your query. That way you do not need to loop the result again and add column etc...
$sql = "SELECT group_concat(title) as title,group_concat(name) as name FROM list";
$result = $db->fetch(PDO::FETCH_ASSOC);
echo json_encode($result);
Try this:
$titles = array();
$names = array();
$sql = "SELECT title,name FROM mainlist";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$titles[] = $row['title'];
$names[] = $row['name'];
}
}
echo json_encode(array("title" => $titles, "name" => $names));
UPDATE
Updated my code to let you manage an undefined number of columns as result
$out = array();
$sql = "SELECT * FROM wp_cineteca";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$keys = array_keys($row);
for ($i = 0; $i < count($row); $i++) {
$out[$keys[$i]][] = $row[$i];
}
}
}
echo json_encode($out);
I have the following code:
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings WHERE location = '$locationListResults' AND industry = '$selected'");
while($row = mysqli_fetch_array($results))
{
echo $row['industry'];
echo $row['location'];
echo $row['title'];
echo $row['description'];
}
}
mysqli_close($con);
}
?>
Could anyone tell me how I would go about storing the echo part into a variable so I can then display it as and where I want in other parts of the site?
If I remove the echo and instead store $row as a variable when I echo that variable it only outputs once and doesn't run the loop.
You should use mysqli_fetch_all for this. The result will be an array where each element is a row and each row is an associative array with the same keys as row in your example
$data = mysqli_fetch_all($result);
$data[0]["industry"]; //Data in the first row
You can then loop over $data to output it any place on your page.
put in array,
$list = array();
while($row = mysqli_fetch_array($results))
{
$list[] = $row;
}
you may try this
$arrayList = array();
while($row = mysqli_featch_array($results)){
$arrayList[] = $row;
}
print_r($arrayList);
Put all the values in an array
$rows = array();
$x = 0;
while($row = mysqli_fetch_array($results))
{
$rows[$x]['industry'] = $row['industry'];
$rows[$x]['location'] = $row['location'];
$rows[$x]['title'] = $row['title'];
$rows[$x]['description'] = $row['description'];
$x++;
}
return $rows;
Then you can use $rows as an array.
foreach($rows as $v){
echo $v['industry']." ".$v['location']."<br />";
echo $v['title']." ".$v['description']."<br />";
}
I'm trying to display this query output in a Highchart (line). I’dlike to know how to input this MySQL loop into the Highchart.
<?php
$qu = "SELECT *,COUNT(url) FROM clicks WHERE url='aaaa' GROUP BY date";
$result = mysql_query($qu) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$hits = $row['COUNT(url)'];
$date = $row['date'];
}?>
Maybe this gives you some idea:
$label = array();
while($row = mysql_fetch_array($result))
{
$label[] = $row["date"];
$data_count[] = (float)$row["COUNT(url)"];
}
$series = array();
$series[] = array("name"=> 'total', "color" => "#4572a7", "data" => $data_count);
$data = array();
$data["chart"]["renderTo"] = "report";
$data["chart"]["defaultSeriesType"] = "column";
$data["title"]["text"] = "Some Title Here";
$data["series"] = $series;
$data["xAxis"]["categories"] = $label;
$data["yAxis"]["allowDecimals"] = true;
header('Content-Type: application/json; charset: utf-8;');
echo json_encode($data);