How to change the JSON output format and how to support chinese character?
Posted
by sky
on Stack Overflow
See other posts from Stack Overflow
or by sky
Published on 2010-04-20T05:17:27Z
Indexed on
2010/04/20
5:23 UTC
Read the original article
Hit count: 231
Currently I using the following code to get my JSON output from MySQL.
<?php
$session = mysql_connect('localhost','name','pass');
mysql_select_db('dbname', $session);
$result= mysql_query('SELECT message FROM posts', $session);
$somethings = array();
while ($row = mysql_fetch_assoc($result)) {
$somethings[] = $row;
}
?>
<script type="text/javascript">
var somethings= <?php echo json_encode($somethings); ?>;
</script>
And the output is:
<script type="text/javascript">
var somethings= [{"message":"Welcome to Yo~ :)"},{"message":"Try iPhone post!"},{"message":"????"}];
</script>
Here is the question, how can I change my output into format like :
<script type="text/javascript">
userAge = new Array('21','36','20'),
userMid = new Array('liuple','anhu','jacksen');
</script>
Which I'll be using later with following code :
var html = '
<table class="map-overlay">
<tr>
<td class="user">' +
'<a class="username" href="/' + **userMid[index]** + '" target="_blank"><img alt="" src="' +
getAvatar(signImgList[index], '72x72') +
'"></a><br>
<a class="username" href="/' + **userMid[index]** + '" target="_blank">' +
userNameList[index] +
'</a><br>
<span class="info">' + **userSex[index]** + ' ' + **userAge[index]** + '?<br>
' +
cityList[index] +
'</span>' +
'</td>
<td class="content">' + picString
+ somethings[index] + '<br>
<span class="time">' +
timeList[index] + picTips +
'</span></td>
</tr>
</table>
';
Thanks for helping and reading!
© Stack Overflow or respective owner