Im not sure how to output MySQL data into formats below. (eg: timelist, usersex, userage from table users.)
<script type="text/javascript">
timeList = new Array(),
userSex = new Array('female','male','male'),
userAge = new Array('21','36开发者_运维百科'),
userMid = new Array('liuple','anhu');
</script>
Thanks!
Use json_encode
to output a PHP array into JavaScript literal format.
<?php
$result= mysql_query('SELECT DISTINCT something FROM sometable ORDER BY something');
$somethings= array();
while ($row= mysql_fetch_assoc($results)) {
$somethings[]= $row['something'];
}
?>
<script type="text/javascript">
var somethings= <?php echo json_encode($somethings, JSON_HEX_TAG); ?>;
</script>
This gives you array literal ["anhu", "liuple"]
format rather than new Array()
. You don't generally want to use new Array()
today.
精彩评论