简体   繁体   中英

how can i make a json with category in mysql?

i need to make a page which show all the items and when clicked bring me all the places where it is available.

i need to make this in mysql

app.controller('namesCtrl', function($scope) {

 $scope.items = [
    {name: 'item1', place: ['place1', 'place2']},
    {name: 'item2', place: ['place2', 'place3']},
    {name: 'item3', place: ['place1', 'place2', 'place3']},
    {name: 'item4', place: ['place1']},
    {name: 'item5', place: ['place1', 'place2']}
];

});

my php is

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

 $conn = new mysqli("localhost", "user", "pass", "table");

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

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}

    $outp .= '{"id":"'  . $rs["id"] . '",';
    $outp .= '"nombre_especialidad":"'  . $rs["nombre_especialidad"] . '",';
    $outp .= '"aguadilla":"'   . $rs["aguadilla"] . '",';
    $outp .= '"arecibo":"'  . $rs["arecibo"] . '",';
    $outp .= '"bayamon":"'    . $rs["bayamon"]        . '",';
    $outp .= '"caguas":"'    . $rs["caguas"] . '",';
    $outp .= '"carolina":"'    . $rs["carolina"] . '",';
    $outp .= '"guayama":"'   . $rs["guayama"] . '",';
    $outp .= '"hato_rey":"' . $rs["hato_rey"] . '"}'; 
}
$outp ='{"especialidades":['.$outp.']}';
$conn->close();

echo($outp);
?>

im getting a lot of error.

how is the correct whay to make this in mysql and get it in json.

Your code should be like this

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

$conn = new mysqli("localhost", "user", "pass", "table");

$result = $conn->query("SELECT id, nombre_especialidad,aguadilla,arecibo, bayamon,caguas,carolina,guayama,hato_rey FROM `especialidades`");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    $outArray["especialidades"][] = $rs;
}
$conn->close();
$outp = json_encode($outArray);
echo $outp;
?>

Hope this will help you!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM