2

I've looked at the other topics concerning passing JSON data to feed JQPlot but can't quite seem to find what I need. The issue is that I can't get the JSON data formatted correctly. (Yes, this is my first time with formatting and using JSON) I've tried all kinds of combinations, but I'm still looking for the right one.

JQPlot wants to see the data as

[[x,y],[x,y],[x,y],[x,y]]

but the best I've been able to output is

[x,y][x,y][x,y][x,y]

I'm hoping that someone might be able to tell me what I'm missing. My code is below...

$sql = "SELECT client_id, SUM(gross) FROM s_pr_wcomp GROUP BY client_id ORDER By SUM(gross) DESC LIMIT 10";
$result = mysqli_query($mysql,$sql) or die(mysqli_error('Top 10 Query Failed!'));

// Encode Top 10

while($row = mysqli_fetch_array($result)){ 
$grossTop = array(
    $grossTop[] = $row['client_id'],
    $grossTop[] = '$'.number_format($row['SUM(gross)'], 2)
);

header('ContentType: application/json; charset=utf-8');
echo json_encode($grossTop);
}

Thank you!

2
  • Looks like your array should be imploded with a comma as glue and wrapped in a new array before it is encoded ? Commented Nov 8, 2012 at 15:02
  • Someone else had mentioned that in a post somewhere but didn't give an example. I tried reading up to wrap my head around it to no avail. Would it be possible for you to elaborate on it? Commented Nov 8, 2012 at 15:14

2 Answers 2

4

You would need to append an array to the $grossTop array. Inside your while loop have something like:

while($row = mysqli_fetch_array($result)){ 
    $grossTop[] = array(
        $row['client_id'],
        '$'.number_format($row['SUM(gross)'], 2)
    );
}
header('ContentType: application/json; charset=utf-8');
echo json_encode($grossTop);
5
  • That's looking a bit better but it is incrementally duplicating entries with each pass and encasing each one in an extra set of brackets... [[a,b]],[[a,b]],[[c,d]],[[a,b]],[[c,d]],[[e,f]] Commented Nov 8, 2012 at 15:12
  • Are you sure you are using the above code exactly? Because the output you are seeing is (most certainly) because of the way you are using $grossTop inside the loop: $grossTop=array($grossTop[]=$row['..'],$grossTop[]='$'.$row[..]) (WRONG!) Commented Nov 8, 2012 at 15:16
  • copied and pasted to make sure Commented Nov 8, 2012 at 15:18
  • I edited my answer, Check again. You were echo-ing the JSON inside your loop. Commented Nov 8, 2012 at 15:20
  • PERFECT! I knew it would be something stupid like that. Thank you! Commented Nov 8, 2012 at 15:24
0

At a quick glance, wrap each iteration of $grossTop in another array.

while($row = mysqli_fetch_array($result)){ 
$grossTop = array(array(
    $grossTop[] = $row['client_id'],
    $grossTop[] = '$'.number_format($row['SUM(gross)'], 2))
);
3
  • I tried doing that one before, but then it wraps each entry in extra brackets rather than grouping them all in one set of brackets separated by commas... [[x,y]][[x,y]][[x,y]] Thanks for the input though! Every little bit helps. Commented Nov 8, 2012 at 15:07
  • OK. How about adding an extra set of square brackets to the $grossTop variable? $grossTop[][] = $row[... Commented Nov 8, 2012 at 15:13
  • ...or add a number iteration $i++; and do $grossTop[$i][] = $row[... Commented Nov 8, 2012 at 15:14

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.