开发者

output mysql data into variables for jquery

I have a 2 field database (daynumber & datavalue), the data of which I am retrieving with php then creating and passing variables into jquery for use with a graph.

I am not doing this as an array because I need to apply a formula to the datavalue field. The formula can be altered by the user.

In jquery, I need to be able to do this:

cday_1 = <?php echo $day_1?>;
cday_2 = <?php echo $day_2?>;
tday_1 = (<?php echo $day_1 ?> * formula1 / formula2;
tday_2 = (<?php echo $day_2 ?> * f开发者_如何学Goormula1 / formula2;

The cday series and the tday series will be plotted separately on the graph.

My problem is that I could manually name each var in php, ie:

$day_1=mysql_result($result,$i,"datavalue");

but I have 30 rows and I'd prefer to do it with a loop. So far, I've got this far:

$i=0;
while ($i < $num) {
$datavalue=mysql_result($result,$i,"datavalue");
echo "\$day_";
echo $i;
echo " = ";
echo $datavalue;
echo "<br>";
$i++;
}

So there are a couple of problems I'm having with this.

The first problem is that while everything is echoing, I'm not sure how to actually create variables like this(!).

The second problem is that there are only 30 rows in the db, but 34 are actually outputting and I can't work out why.

I'm self-taught, so apologies for clumsy coding and/or stupid questions.


You could just store them in a PHP-side array:

$days = array(
    'cday' => 'the cday value',
    'dday' => 'the dday value',
    etc...
);

and then via json_encode() translate them to a Javascript array/object automatically. This object could be sent to your page as an AJAX response, or even embedded into the page directly:

<script type="text/javascript">
    var days = <?php echo json_encode($days) ?>;
</script>

after which you just access the values as you would any other array in JS.


Have ended up doing this:

$i=0;
while ($i < $num) {
${"day_$i"}=mysql_result($result,$i,"datavalue");
$i++;
}

I have a feeling this was a basic issue and I just didn't explain myself properly. I am unable to use an array as I needed to fiddle with formulas so this solution worked fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜