1

I need values from my database to plot a graph. I have multiple lines I want to plot on the same graph. I need the values, for each line, in the following form, for as many different $dates there are in the database:

$graph_points1 = array(array($date, $value), array($date, $value), array($date, $value)); 

I have a query to get the data from the database:

$data = $wpdb->get_results($wpdb->prepare( "SELECT * FROM `wp_graph_info` ORDER BY DATE ASC"));

If I use the following code:

foreach($data as $datas){
$list_name = $datas->list_name;
$date = $datas->date;
$value = $datas->subscriber_count;
if($list_name == 'line1'){
$result = array(array($date,$value));}}

print_r($result); will give:

Array ( [0] => Array ( [0] => 2015-05-16 [1] => 131 ) ) 
Array ( [0] => Array ( [0] => 2015-05-17 [1] => 133 ) ) 
Array ( [0] => Array ( [0] => 2015-05-18 [1] => 137 ) ) 

which is almost what I want. I need to have the results as:

$graph_points1 = array(array(2015-05-16, 131), array(2015-05-17, 133), array(2015-05-18, 137));

how to I put it in the above form?

Also I can't plot the date on the x-axis. How do I only retrieve the 'day' value so I have:

$graph_points1 = array(array(16, 131), array(17, 133), array(18, 137));

Also if there a way to loop through all th $list_names (I have 7 different lists, so my graph will plot 7 lines) or do I need to use an if() statement for each one?

3
  • do you mean: array('2015-05-16' => '131') ?? Commented May 18, 2015 at 9:40
  • 1
    I don't really understand the print_r is the same as what you want? Commented May 18, 2015 at 9:40
  • because it's the same what you wantnow, I agree with @Daan Commented May 18, 2015 at 9:40

1 Answer 1

1
$result = [];
foreach($data as $datas){
  $list_name = $datas->list_name;
  $date = $datas->date;
  $value = $datas->subscriber_count;
  list($year, $month, $day) = explode('-', $date);
  $result[$list_name][] = array($day,$value);
}

echo "<pre>";var_dump($result);echo "</pre>";
Sign up to request clarification or add additional context in comments.

2 Comments

amazing! Thanks.Is there a way to loop through all the $list_names (there are 7) or do I need to write if() statements for each one?
I'd simply put the list_name as a key of the array. I'll update the script, one sec

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.