我需要制作一个显示所有项目的页面,单击后将所有可用的地方都带给我。
我需要在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']}
];
});
我的PHP是
<?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);
?>
即时通讯收到很多错误。
如何在mysql中实现并在json中获取它的正确方法。
最佳答案
您的代码应如下所示
<?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;
?>
希望对你有帮助!!
关于php - 如何在mysql中创建具有类别的json?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39054777/