0

I have a function like this:

function searchSingleUrlGraph($mysql){
    $query="SELECT * FROM `dailydata` WHERE `userid`='".$_SESSION['userdata']['userid']."' AND `url`='faboolis.com'";
    $HTML='[';
    $result=$mysql->query($query);
        while($row=$result->fetch_assoc()){
            $HTML .= "[
                        ['".$row['date']."',".$row['fblikes']."],
                        ['".$row['date']."',".$row['fbshares']."],
                    ],";
        }
    $HTML .=']';
    return $HTML;
}   

the result I want is this:

[[['Jun 4',224],['Jun 5',34],],[['Jun 4',220],['Jun 5',30],],];

but right now Im getting:

[[['Jun 4',224],['Jun 4',34],],[['Jun 5',220],['Jun 5',30],],];

I know there is an easy solution I'm just brain dead right now and its driving me crazy.

6
  • Are you trying to build a JSON array? Am I missing something here, as what you want and what you are getting look the same to me.
    – Mike Brant
    Commented Jun 5, 2013 at 22:24
  • No, im using this data to construct a graph with javascript. I want the dates different. look at the dates and you will see what I mean.
    – Johnny Kay
    Commented Jun 5, 2013 at 22:29
  • however it IS a JSON Array
    – steven
    Commented Jun 5, 2013 at 22:30
  • so you should build a simple php array and then do json_encode($array)
    – steven
    Commented Jun 5, 2013 at 22:31
  • Isn't your desired output going to be duplicates/redundant? Jun 4, 224 fblikes / Jun 4, 224 fblikes and Jun 5, 34 fbshares / Jun 5, 30 fbshares. That doesn't make sense.
    – Sean
    Commented Jun 5, 2013 at 22:43

1 Answer 1

0

The simplest solution would be to just use 2 while loops.

while($row=$result->fetch_assoc()){
    //process likes
}
$result->data_seek(0); //go back to the beginning.
while($row=$result->fetch_assoc()) {
    //process shares
}
1
  • I actually tried a variation of the $result->data_seek(0) and it worked fine. Thanks.
    – Johnny Kay
    Commented Jun 19, 2013 at 15:22

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.