views:

28

answers:

1

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!

+1  A: 

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.

bobince