2

I wanted to display a graph in javascript using data from php. My code is:

<?php $i = 20; ?>
while (data.length < totalPoints) {
    array_js.push(<?php echo json_encode($array_php[++$i][1]);?>);
}

The problem is that even though $i is declared before the while loop, it gets back to 20 all the time, so the data pushed in array_js is always $array_php[21][1].

Edit: here is more code, (not same variable name...)

startConn(); //connect to database

$query = "SELECT * FROM Reading";
$data = getAllDatas($query);
?>

<script type="text/javascript">
    $(function() {

    var data = [], totalPoints = 300;

        function getRandomData() {

            <?php $i = 20; ?>
            while (data.length < totalPoints) {
                data.push(<?php echo json_encode($data[--$i][1]);?>);
                //data.push(<?php echo ++$i;?>);
            }
        } // END getRandomData()
9
  • You can't mix php and javascript in this way. What is your goal? Why you want read a php array in javascript? Commented Feb 10, 2016 at 1:02
  • to display a graph in javascript using data from php Commented Feb 10, 2016 at 1:04
  • why not just encode your php array from the start, then make your way into pushing whatever values you have there Commented Feb 10, 2016 at 1:06
  • can show you more code? the javascript reside in html page, I suppose. Javascript can't use php data. you have to set the complete javascript array in php. but if you show more code, we can be more accurate Commented Feb 10, 2016 at 1:07
  • The problem is not the php array. I want to walk through the php array and to do that I need to increment $i and it seems that I am not able to achieve that because it does reset automatically. Commented Feb 10, 2016 at 1:10

2 Answers 2

1

You have a misunderstanding on how server side and client side code work.
I think this may help you

$(function() {
    var data = [], totalPoints = 3;
    var i = <?php $i = 1;echo $i; ?>;
    var j = parseInt(i);
    var arr = <?php echo json_encode($data); ?>;

    function getRandomData() {
        while (data.length < totalPoints) {
            data.push(arr[j][1]);
            j++;
        }
        return data;
    }
    var data1 = getRandomData();
    console.log(data1);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, it works perfect. I didn't know that it was possible to pass more complex variables like multidimensional arrays. My lack of knowledge in javascript doesn't help. should probably spend some time learning it.
0

Try this one. You can pass the value of the php variable using echo while declaring another variable on the java script.

<?php $i = 20; ?>
var i = <?php echo $i;?>;
while (data.length < totalPoints) {
    array_js.push(<?php echo json_encode($array_php[++i][1]);?>);
}

1 Comment

Haven't tried your code but I doubt it will work since 'i' cannot be seen in a php code. Might be wrong though...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.