Reputation: 169
Afternoon all. The code below works perfectly, however, I need to pull each row of the php sql array out and into the script var. Any ideas on how to write a while loop that could do this? Thanks for any help
var enableDays = ["<?php echo mysql_result($result, 0, 'date'); ?>"];
enableDays.push("<?php echo mysql_result($result, 1, 'date'); ?>");
Additional Code::
$rows = array();
while ($row = mysql_fetch_assoc($result))
{
$rows[] = $row;
}
var enableDays = [<?php echo json_encode($rows); ?>];
console.log(enableDays[1]);
Upvotes: 1
Views: 136
Reputation: 27812
You can get the rows using mysqli_fetch_assoc
and then encode it to JSON:
<?php
$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
?>
var enableDays = <?php echo json_encode($rows); ?>;
Upvotes: 3
Reputation: 1634
Hope this would help
<?php
$res = mysql_fetch_assoc($result)
foreach($res as $key => $val){ ?>
enableDays.push('<?php echo $val; ?>');
<?php }
?>
Upvotes: 1
Reputation: 3203
This can't work. Once the page is loaded, all the PHP has run already. But if you're really just adding the next mysql_result for each push, you could have PHP create the entire enableDays array for you:
<?php
echo 'var enableDays = ["' . mysql_result(...) . '", "' . mysql_result(...) . '"];'
?>
Upvotes: 1
Reputation: 437634
Pull your data out into a PHP array, then transfer that to JavaScript with
var enableDays = <?php echo json_encode($enableDays); ?>;
As an aside, the obligatory recommendation that you should stop using the PHP mysql
extension immediately because it is not very safe and has been deprecated; switch to something better (mysqli
and PDO
are both good choices).
Upvotes: 2