admin管理员组文章数量:1415664
i am trying to bine AngularJS with a php backend. Right now i am trying to generate a json with php to return it to a http request to angular. So far i created this php.
$dbhost = "localhost";
$dbport = "5432";
$dbname = "fixevents";
$dbuser = "postgres";
$dbpass = "123";
$connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass);
$query = "SELECT contact_firstname, contact_lastname, contact_id, contact_email FROM contact WHERE user_id = 1";
$result = pg_query($connect, $query);
$ma = '';
$json = '[';
while ($row = pg_fetch_array($result)) {
$json .= $ma . '{';
$json .= 'contact_firstname:"' . addslashes($row['contact_firstname']) . '",';
$json .= 'contact_lastname:"' . addslashes($row['contact_lastname']) . '",';
$json .= 'contact_id:' . addslashes($row['contact_id']) . ',';
$json .= 'contact_email:[';
$contact_email = explode(',,,', addslashes($row['contact_email']));
$ma_email = '';
foreach($contact_email as $email) {
$json .= $ma_email . '"' . $email . '"';
$ma_email = ',';
}
$json .= ']';
$json .= '}';
$ma = ',';
}
$json .= ']';
echo $json;
But i read some ments by greater minds than mine :) and they said creating the json manually is not the best idea. Can anyone tlel me how to generate this json is a more stylish way? I saw something about json_endode and array but i dont know how to add a list inside a list. I have a list and inside each list item i have another list with emails because 1 contact can have more emails. My generated JSON right now looks like this
[{contact_firstname:"Daniel",contact_lastname:"Pacuraru",contact_id:1,contact_email:["[email protected]","[email protected]"]},{contact_firstname:"Someone",contact_lastname:"Else",contact_id:2,contact_email:["[email protected]"]}]
Thank you, Daniel!
EDIT
$myjson = array();
while($row = pg_fetch_array($result)) {
$json = array(
'contact_firstname' => addslashes($row['contact_firstname']),
'contact_lastname' => addslashes($row['contact_lastname']),
'contact_id' => addslashes($row['contact_id']),
'contact_email' => explode(',,,', addslashes($row['contact_email']))
);
array_push($myjson, $json);
}
echo json_encode($myjson);
i am trying to bine AngularJS with a php backend. Right now i am trying to generate a json with php to return it to a http request to angular. So far i created this php.
$dbhost = "localhost";
$dbport = "5432";
$dbname = "fixevents";
$dbuser = "postgres";
$dbpass = "123";
$connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass);
$query = "SELECT contact_firstname, contact_lastname, contact_id, contact_email FROM contact WHERE user_id = 1";
$result = pg_query($connect, $query);
$ma = '';
$json = '[';
while ($row = pg_fetch_array($result)) {
$json .= $ma . '{';
$json .= 'contact_firstname:"' . addslashes($row['contact_firstname']) . '",';
$json .= 'contact_lastname:"' . addslashes($row['contact_lastname']) . '",';
$json .= 'contact_id:' . addslashes($row['contact_id']) . ',';
$json .= 'contact_email:[';
$contact_email = explode(',,,', addslashes($row['contact_email']));
$ma_email = '';
foreach($contact_email as $email) {
$json .= $ma_email . '"' . $email . '"';
$ma_email = ',';
}
$json .= ']';
$json .= '}';
$ma = ',';
}
$json .= ']';
echo $json;
But i read some ments by greater minds than mine :) and they said creating the json manually is not the best idea. Can anyone tlel me how to generate this json is a more stylish way? I saw something about json_endode and array but i dont know how to add a list inside a list. I have a list and inside each list item i have another list with emails because 1 contact can have more emails. My generated JSON right now looks like this
[{contact_firstname:"Daniel",contact_lastname:"Pacuraru",contact_id:1,contact_email:["[email protected]","[email protected]"]},{contact_firstname:"Someone",contact_lastname:"Else",contact_id:2,contact_email:["[email protected]"]}]
Thank you, Daniel!
EDIT
$myjson = array();
while($row = pg_fetch_array($result)) {
$json = array(
'contact_firstname' => addslashes($row['contact_firstname']),
'contact_lastname' => addslashes($row['contact_lastname']),
'contact_id' => addslashes($row['contact_id']),
'contact_email' => explode(',,,', addslashes($row['contact_email']))
);
array_push($myjson, $json);
}
echo json_encode($myjson);
Share
Improve this question
edited Oct 2, 2013 at 21:31
Pacuraru Daniel
asked Oct 2, 2013 at 21:12
Pacuraru DanielPacuraru Daniel
1,2059 gold badges31 silver badges57 bronze badges
3 Answers
Reset to default 4That's invalid json (the keys must use double quotes). My advice: just create nested PHP arrays (and/or objects), then encode in one go with json_encode
. This way you'll also avoid other problems, like escaping newlines inside your strings.
For example:
$some_data = array(
'something' => 10,
'something_else' => "foo",
'some_array' => array(1,2,3,4)
);
echo json_encode($some_data);
Output:
{"something":10,"something_else":"foo","some_array":[1,2,3,4]}
Build an array with your data, and then pass the arry into json_encode
, you will get a nicely formatted string.
$BigArray = array();
$Index = 0;
while($row = getSomeData()){
$BigArray[$Index] = array(
'Name' => $row['name'],
'Age' => $row['age'],
'Colors' => array(
'0' => 'Red',
'1' => 'Blue'
)
);
$Index++;
}
echo json_encode($array);
{"Name":"Matt","Age":"20","Colors":["Red","Blue"]}
EDIT
More efficient way (I think, less data anyway...)
$Names = array();
$Ages = array();
$Colors = array();
$Index = 0;
while($row = getSomeData()){
$Names[$Index] = $row['name'];
$Ages[$Index] = $row['age'];
$Colors[$Index] = array(
'0', => 'Red',
'1', => 'Blue'
};
$Index++;
}
$JSonArray = array(
'Names' => $Names,
'Age' => $Ages,
'Color' => $Colors
);
json_encode($JSonArray);
The difference here will be that you will name have a name valued pair for each object in your array:
Name: Matt Age: 20, Name: Jason, Age: 24
You will have an array of names.
Names: Matt, Jason, Eric
Ages: 20, 24, 26
Just create a native PHP object and use json_encode
:
json_encode($obj);
So instead of this:
$ma = '';
$json = '[';
while ($row = pg_fetch_array($result)) {
$json .= $ma . '{';
$json .= 'contact_firstname:"' . addslashes($row['contact_firstname']) . '",';
$json .= 'contact_lastname:"' . addslashes($row['contact_lastname']) . '",';
$json .= 'contact_id:' . addslashes($row['contact_id']) . ',';
$json .= 'contact_email:[';
$contact_email = explode(',,,', addslashes($row['contact_email']));
$ma_email = '';
foreach($contact_email as $email) {
$json .= $ma_email . '"' . $email . '"';
$ma_email = ',';
}
$json .= ']';
$json .= '}';
$ma = ',';
}
$json .= ']';
echo $json;
You only need this:
echo json_encode($result);
本文标签: javascriptphp dynamically generate jsonStack Overflow
版权声明:本文标题:javascript - php dynamically generate json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745230357a2648796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论