json array with inner join - php

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();
?>

Related

How to output like this in php

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);

Form json using php json_encode failed

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);

Remove last Comma from while loop

My code is resulting in a output like this
Title1,1,Title2,2,
How can i remove the last comma from this?
$comma = ",";
$query = "SELECT name, listid FROM playlist ORDER BY id DESC";
$result = $mysqli->query($query);
while(list($name, $listid) = $result->fetch_row()) {
echo $name;
echo $comma;
echo $listid;
echo $comma;
}
$lines = array();
while(list($name, $listid) = $result->fetch_row()) {
$lines[] = "$name,$listid";
}
echo implode(',', $lines);
Don't manually echo commas. Just make an array, then implode it.
echo implode(',', array($name, $listid));
Use trim
<?php
$str = "1,2,3,4,5,";
echo trim($str, ",");
Output
1,2,3,4,5
You can try this:
$query = "SELECT name, listid FROM playlist ORDER BY id DESC";
$result = $mysqli->query($query);
$data = array();
while(list($name, $listid) = $result->fetch_row()) {
$data[] = $name.",".$listid;
}
echo implode(',', $data);
try this
$comma = ",";
$html = "";
$query = "SELECT name, listid FROM playlist ORDER BY id DESC";
$result = $mysqli->query($query);
while(list($name, $listid) = $result->fetch_row()) {
$html .= $name.$comma.$listid.$comma;
}
echo substr($html, 0, strlen($html) - 2);
I would recommend going with Joseph Silber's answer, although this would work :
$comma = ",";
$k=0;
$query = "SELECT name, listid FROM playlist ORDER BY id DESC";
$result = $mysqli->query($query);
while(list($name, $listid) = $result->fetch_row()) {
if($k!=0){
echo $comma;
}
echo $name;
echo $comma;
echo $listid;
$k++;
}
$result = mysqli_query($con,$query);
$num_rows= mysqli_num_rows($result);
$ctr=0;
while($row=mysqli_fetch_array($result)){
$ctr++;
echo $row['example'];
if($ctr!=$num_rows){
echo ',';}
else{
echo '';
}
}

How to contain a while loop in a variable

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 />";
}

Echo all variable from while loop

I hava a while loop that throught to databases records. Like this:
$query = mysql_query("SELECT * FROM table");
while($articles = mysql_fetch_array($query)){
// something happen here
}
How can I echo each $articles out of the while loop ? So I don't want to have something like
while(...){
echo $articles['article_name'];
}
How can I save all the $articles['article_name'] into an array an echo them back ?
How about this:
$articleList = array();
while(...){
$articleList[] = $articles['article_name'];
}
That will put everything in the array $articleList for you.
You can do something like:
$articles = array();
while ($art = mysql_fetch_array($query)) {
$articles[] = $art;
}
print_r($articles);
$i=0; $articles = array();
$query = mysql_query("SELECT * FROM table");
while($articles[] = mysql_fetch_array($query)){
echo $articles['article_name'][$i];
$i++;
}
$query = mysql_query("SELECT * FROM table");
while($articles = mysql_fetch_array($query)){
$names[] = $articles['article_name'];
}
echo 'array of names are :';
print_r($names);

Categories