Jump to content
#StayAtHome ×
webguync

help with PHP outputing to JSON

Recommended Posts

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= "";}
    $outp .= '{"Portrait":"'  . $rs["Portrait"] . '",';
    $outp .= '"First Name":"'  . $rs["FName"] . '",';
    $outp .= '"Last Name":"'   . $rs["LName"] . '",';
    $outp .= '"DOI":"'. $rs["DOI"] . '",';
    $outp .= '"MOI":"'. $rs["MOI"] . '",';
    $outp .= '"YOI":"'. $rs["YOI"] . '",';
    $outp .= '"ID":"'. $rs["id"] . '"}';
}
$outp ='{"records":['.$outp.']}';

Hi for the above PHP code the output looks like this JSON format

{

"records": [{

"Portrait": "image1.png",

"First Name": "Dave",

"Last Name": "Smith",

"DOI": "20",

"MOI": "0",

"YOI": "2017",

"ID": "1"

} {

"Portrait": "image2.png",

"First Name": "Sally",

"Last Name": "Jones",

"DOI": "20",

"MOI": "0",

"YOI": "2009",

"ID": "2"

}]

}

 

and this doesn't validate because there should be comma in between records like this:

 

{

"records": [{

"Portrait": "image1.png",

"First Name": "Dave",

"Last Name": "Smith",

"DOI": "20",

"MOI": "0",

"YOI": "2017",

"ID": "1"

}, {

"Portrait": "image2.png",

"First Name": "Sally",

"Last Name": "Jones",

"DOI": "20",

"MOI": "0",

"YOI": "2009",

"ID": "2"

}]

}

 

I am trying to figure out how to get a comma added between records without also adding it at the end, b/c that is invalid also.

 

Any ideas?

Share this post


Link to post
Share on other sites

Don't try to build JSON strings yourself. It's silly.

 

If you want a JSON representation of an array (or object) then use an actual PHP array and json_encode it.

$outp = ["records" => []];
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    $outp["records"][] = [
        "Portrait" => $rs["Portrait"],
	etc.
    ];
}
echo json_encode($outp);
  • Like 1

Share this post


Link to post
Share on other sites

Sorry to not understand fully, but what am I doing wrong here?

$outp = ["records" => []];
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= "";}
    $outp .= '{"Portrait":"'  . $rs["Portrait"] . '",';
    $outp .= '"First Name":"'  . $rs["FName"] . '",';
    $outp .= '"Last Name":"'   . $rs["LName"] . '",';
    $outp .= '"DOI":"'. $rs["DOI"] . '",';
    $outp .= '"MOI":"'. $rs["MOI"] . '",';
    $outp .= '"YOI":"'. $rs["YOI"] . '",';
    $outp .= '"ID":"'. $rs["id"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();

echo json_encode($outp);

I get an error with this line 

$outp = ["records" => []];

Parse error: syntax error, unexpected '[' in Database-search/DB-Search.php on line 7

Share this post


Link to post
Share on other sites

What you're doing wrong is not listening to what you were already told. @Barand showed you what to do.

Edited by benanamen

Share this post


Link to post
Share on other sites

What you're doing is completely disregarding what I and Barand said.

 

Your database table uses columns like FName and LName. If you're okay with using those in your JSON as well then do what Barand said.

If you want to keep the "First Name" and "Last Name" keys in your JSON then either do what Barand said after modifying your query to use aliases (like "FName AS `First Name`") or do what I said.

 

Neither of those options involve building $outp as a string.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.