0

I'm trying to fill those two arrays (time2 and text) from the same query ($result), however only the first array is filled, and I don't understand why...
Note that I'm invoked from the same query, because ORDER BY RAND () functionality is implemented, and when I need to get the values of some array positions, the correct value should appear. And that's why I can't create another identical query.
Is this possible to do?

    //Array Time
    var time2 = [<?php $title=$_GET["title"];

    $result = mysql_query("SELECT tag, round(avg(timecode)) as timecode, group_id
    FROM tags
    WHERE tags.filename = '".$title."'
    GROUP BY group_id, tag
    ORDER BY RAND()");

    while($row = mysql_fetch_array($result))
        {
        $timecode = $row['timecode'];
        print $timecode;
        print ",";
        }?>]

    //Array Text

    var text = [<?php

    while($row = mysql_fetch_array($result))
        {
        $tag = $row['tag'];
        print "'";
        print $tag;
        print "'";
        print ",";
        }?>]

2 Answers 2

2

mysql_fetch_array is a cursor based function. It will run to the end of the result set and stop. Calling it again will still return the 'end of result set'.

.. and it's deprecated.

You should:

  1. switch to PDO or mysqli
  2. read out the whole result set to a local var and then loop over that to create your javascript
Sign up to request clarification or add additional context in comments.

8 Comments

So you're saying that I should for instance create an Array to store all the data, with the timecode, group_id and tag, and then separate them and create news arrays for each one, right?
Yes. You'll read in all the data once and then reuse the resultant array to populate your javascript arrays.
I wouldn't do that. With alot of data you would risk filling server memory... So it doesn't scale well.
@fredrik So, what you advise me to do?
@zppinto Use an array. fredrik is assuming your query will be retrieving an absurd amount of data. And if you're sending that much to the client, you need to rethink how the whole thing works.
|
2

First of, you should definently change from the mysql_* family of functions, since they are clearly deprecated. The only valid reason not to is that you php installation is outdated and you are not in control of the server (as in it's hosted), and even that is barely acceptable.

I would solve your problem by doing this:

<script>
var time2 = [];
var text = [];
<?php
  $title=$_GET["title"];

  $result = mysql_query("SELECT tag, round(avg(timecode)) as timecode, group_id FROM tags WHERE tags.filename = '".$title."' GROUP BY group_id, tag ORDER BY RAND()");

  while($row = mysql_fetch_array($result))
  {
    echo "time2.push('".$row['timecode']."');";
    echo "text.push('".$row['tag']."');";
  }
?>
</script>

This allows you to fill up the two variables "at the same time". Well actually you let the client do the work, which I personally do not see a problem with.

Another, perhaps more sensible, way of doing it - which I won't show you - is to make the page do an AJAX request for the data, return all the data in a simple JSON structure and sort thru it on the client...

6 Comments

You're code should fill the arrays time2 and text, right? Because that doesn't happen. The code is printed, but it doesn't populate the arrays...
Then you havn't encosed it all in script tags? I've updated with that.
Yes, it's enclosed! This is the printed code: time2.push('82');text.push('tag1');time2.push('26');text.push('tag2'); etc
If it is printed visibly on page instead of executed as javascript I find it hard to believe that it is properly enclosed in script tags. Could you provide more of the code? (say in a pastebin)
Hmm, it looks correct. Try switching to print instead of echo - other than that I cannot explain why it wouldn't work.
|

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.