0

I'm building a simple service for mobile apps with php which give a JSON to mobile apps. I have two table in code, below the snippet :

$con = mysqli_connect(HOST, USER, PASS, DB);
$sql_results = mysqli_query($con, "SELECT * FROM `table-images`");
$sql_results_1 = mysqli_query($con, "SELECT * FROM `table-code` WHERE id='$id'");

$rows = array();
while($r = mysqli_fetch_assoc($sql_results)) {
   $rows[] = $r;
}
echo'{"response":'.json_encode($rows).'}';

Resul code above :

{
"response": [{
    "id": "31",
    "shirtImage": "Content\/Images\/Short Sleeve\/874be7b82812f76c944d71706c9651eb.gif"
}, {
    "id": "32",
    "shirtImage": "Content\/Images\/Short Sleeve\/b-Cleaned.png"
}]
}

I want to put result of sql_results_1 as key to the result, here I the JSON I need :

{
"response": [{
    "id": "31",
    "shirtImage": "Content\/Images\/Short Sleeve\/874be7b82812f76c944d71706c9651eb.gif",
    "idcode": [{
        "X": 0,
        "Y": 1,
        "Z": 2
    }]
}, {
    "id": "32",
    "shirtImage": "Content\/Images\/Short Sleeve\/b-Cleaned.png",
    "idcode": [{
        "X": 2,
        "Y": 1,
        "Z": 0
    }]
}]
}

Where key idcode is result from '$sql_results_1'. I new in PHP programming, I read some question regarding my own but the result is not I expect.

Edit

As @Ashu answer, here the code now :

$final_array = array();
$i=0;

while($r = mysqli_fetch_assoc($sql_results)) {
    array_push($final_array,$r);
    $res = mysqli_query($con, "SELECT * FROM `table-code` WHERE id='".$row['id']."'");
    while($r = mysqli_fetch_assoc($res)) {
       $final_array[$i]["idcode"] = array("x"=>$r['x']);
    }
    $i++; 
}

But the key idcode is JSONObject "idcode":{x=0}, I need it as JSONArray "idcode":[{x=0}]

3
  • idcode value is ={"X": 0,"Y": 1, "Z": 2} on single row or xyz is on separate rows...please mention table 2 structure for 'idcode' Commented Mar 4, 2019 at 10:08
  • how looks like $sql_results_1 ? what did you try by far ? Commented Mar 4, 2019 at 10:10
  • @Ashu they are is single row (x, y and z) is a field from table table-code Commented Mar 4, 2019 at 10:12

2 Answers 2

1

First of all I would recommend PDO over mysqli. But with mysqli things are as follows:

The fetch assoc function:

Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset.

If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysqli_fetch_row() or add alias names.

As you push everything to an array you have an multidimensional array $rows containing all results:

 $rows[];

Go with:

foreach($rows as $key => $row) {
    $res = mysqli_query($con, "SELECT * FROM `table-code` WHERE id='".$row['id']."'");
   $row["idcode"] = mysql_fetch_assoc($res);
   $rows[$key] = $row;
}

Untested. Is it giving you the correct idea?

Sign up to request clarification or add additional context in comments.

1 Comment

yes, i use your concept and combine with @Ashu answer. But the key idcode is JSONObject, I need it as JSONArray
1

1st result store into array :

$final_array = array();
while($r = mysqli_fetch_assoc($sql_results)) {
   array_push($final_array,$r);
}

2nd table result, you have to create idcode key in final_array using $i.

$i=0;
while($r = mysqli_fetch_assoc($sql_results1)) {
   $final_array[$i]["idcode"] = array("x"=>$r['x'],"y"=>$r['y'],"z"=>$r['z']);
   $i++; 
}

echo'{"response":'.json_encode($final_array).'}';

1 Comment

I modify your answer, i put the second step in first step and its working, but in line ` $final_array[$i]["idcode"] = array("x"=>$r['x'],"y"=>$r['y'],"z"=>$r['z']); ` is give me JSONObject instead JSONArray. I need JSONArray in key idcode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.