1

hope you well..

I have an API made using php, to grab from SQL and convert it to JSON which is working fine. My only problem was, i can't manipulate this php to fetch JSON like i wanted. The solution i believe just a placement in the $outp at my api.php .. i have the id as primary in SQL can i used it as index to display the JSON like i expected?

Thank YOu, any effort will be rewarded

Here is the output JSON :

{
talents: [
{
id: "1",
tag: "001",
name: "Jasmina",
images: "assets/images/Jasmina3.jpg",
images02: "assets/images/Jasmina4.jpg",
images03: "assets/images/Jasmina7.jpg",
skills: "Usher",
skills02: "Modeling",
indexing: "usher"
},
{
id: "2",
tag: "002",
name: "Bruna",
images: "assets/images/BrunaD17.jpg",
images02: "assets/images/BrunaD18.jpg",
images03: "assets/images/BrunaD10.jpg",
skills: "Usher",
skills02: "Modeling",
indexing: "usher"
}
]
}

Here is the Outcome i expected :

{
talents: [
1: {
tag: "001",
name: "Jasmina",
images: "assets/images/Jasmina3.jpg",
images02: "assets/images/Jasmina4.jpg",
images03: "assets/images/Jasmina7.jpg",
skills: "Usher",
skills02: "Modeling",
indexing: "usher"
},

2:{
tag: "002",
name: "Bruna",
images: "assets/images/BrunaD17.jpg",
images02: "assets/images/BrunaD18.jpg",
images03: "assets/images/BrunaD10.jpg",
skills: "Usher",
skills02: "Modeling",
indexing: "usher"
}
]
}

And take a peek at my API, please help me play around with that $outp. to get it.

<?php
//header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("localhost", "root", "", "application");

$result = $conn->query("SELECT * FROM talents");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"id":"'  . $rs["id"] . '",';
    $outp .= '"tag":"'  . $rs["tag"] . '",';
    $outp .= '"name":"'   . $rs["name"]        . '",';
    $outp .= '"images":"'. $rs["images"]     . '",';
    $outp .= '"images02":"'. $rs["images02"]     . '",';
    $outp .= '"images03":"'. $rs["images03"]     . '",';
    $outp .= '"skills":"'. $rs["skills"]     . '",';
    $outp .= '"skills02":"'. $rs["skills02"]     . '",';
    $outp .= '"indexing":"'. $rs["indexing"] . '"}';
}
$outp ='{"talents":['.$outp.']}';
$conn->close();

echo($outp);
?>

1 Answer 1

2

Instead of trying to build the json by yourself use native json_encode. It solves all the edge cases that there are with JSONs.

All you need to prepare for it is an associative array, you can specify field by field (like i did in the example, it is recommended because that way you are specifying the exact fields to send to the user, it prevent sensitive data exposure) or just $outp[$rs["id"]] = $rs;

<?php
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("localhost", "root", "", "application");

$result = $conn->query("SELECT * FROM talents");

$outp = array();
while ($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    $outp[$rs["id"]] = array(
        "tag" => $rs["tag"],
        "name" => $rs["name"],
        "images" => $rs["images"],
        "images02" => $rs["images02"],
        "images03" => $rs["images02"],
        "skills" => $rs["skills"],
        "skills02" => $rs["skills02"],
        "indexing" => $rs["indexing"]
    );
}
$outp["talents"] = array($outp);
$conn->close();

echo (json_encode($outp));
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Working like charms. Thank you mate

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.