0

I could not find any answer to my question. I'm using PhPmyGraph ( http://phpmygraph.abisvmm.nl/ ) to display a graph of some data from my databases. The problem is that I have to create my arrays in the file itself, and if I want 2 graphs on the page I need to create 2 different files. Apparently the file is easier to use with a CMS but I'm not using one.

This is the file graph.php:

<?php

//Set content-type header for the graphs
header("Content-type: image/png");
//Include phpMyGraph5.0.php
include_once('../phpMyGraph5.0.php');

//Set config directives
$cfg['title'] = 'Example graph';
$cfg['width'] = 500;
$cfg['height'] = 250;

//Set data
$data = array(
    'Jan' => 12,
    'Nov' => 78,
    'Dec' => 23
);

//Create phpMyGraph instance
$graph = new phpMyGraph();
//Parse
$graph->parseVerticalPolygonGraph($data, $cfg);
?>

I call it in my page index.php:

echo " < img src=\"graph.php\"> ";

Is there another way to do it? And send the data from index.php to graph.php? Or maybe move the code graph.php into index.php ? The problem is for the image object, I don't really know how to do it!

UPDATE: I have almost found a solution, my code is now:

in graph.php:

//Parse
$graph->parseVerticalPolygonGraph(unserialize($_GET['data']), $cfg);

index.php :

$select_daily = mysql_query("SELECT * FROM table");
    while ($row_daily = mysql_fetch_assoc($select_daily) ){     
        $y = substr($row_daily['ymd'], 0, -4);  // Year
        $m = substr($row_daily['ymd'], 4, -2);  // Month
        $d = substr($row_daily['ymd'], -2); // Day

        $key = $d."/".$m."/".$y;
        $data_daily [$key] = $row_daily['members'];
    }
    foreach($data_daily as $key => $value) {
        echo $key ,' : ', $value ,'<br/>';
    }

echo "< img src=\"graph.php?data=".serialize($data_daily)."\">";

But I get the error "provided data is not an array"

I can't see what's wrong with it? if I do var_dump($data_daily) I get:

array(8) { ["14/12/2011"]=> string(1) "0" ["13/12/2011"]=> string(2) "11" ["12/12/2011"]=> string(1) "0" ["11/12/2011"]=> string(1) "2" ["10/12/2011"]=> string(1) "9" ["09/12/2011"]=> string(1) "3" ["08/12/2011"]=> string(1) "6" ["07/12/2011"]=> string(1) "6" }

UPDATE2:

var_dump($data1); gives:

array(12) { ["Jan"]=> int(12) ["Feb"]=>
int(25) ["Mar"]=> int(0) ["Apr"]=> int(7) ["May"]=> int(80) ["Jun"]=>
int(67) ["Jul"]=> int(45) ["Aug"]=> int(66) ["Sep"]=> int(23)
["Oct"]=> int(23) ["Nov"]=> int(78) ["Dec"]=> int(23) }

and var_dump($s_data1 = serialize($data1)) gives:

a:12:s:3:"Jan";i:12;s:3:"Feb";i:25;s:3:"Mar";i:0;s:3:"Apr";i:7;s:3:"May";i:80;s:3:"Jun";i:67;s:3:"Jul";i:45;s:3:"Aug";i:66;s:3:"Sep";i:23;s:3:"Oct";i:23;s:3:"Nov";i:78;s:3:"Dec";i:23;}

Then unserialize($s_data1); gives the same thing than $data1

So the argument 1 of the parse should be correct... I can’t see what is wrong

I finally gave up and loaded my arrays in graph.php:

if ($_GET['data'] == 'daily'){
    $cfg['title'] = 'daily';
    $graph->parseVerticalPolygonGraph($data_daily, $cfg);
}

And I call the file like that:

echo "<img src=\"graph.php?data=daily\">";

Thanks for your help anyway

3 Answers 3

1

I previously needed a page to display multiple graphs using phpMyGraph and the approach I took was to use data URI's and php's ob_start() and ob_get_clean()

Simply use this for each graph:

ob_start();
$graph->parseVerticalPolygonGraph($data, $cfg);
$img = ob_get_clean();
echo "<img src='data:image/gif;base64," . base64_encode($img) . "/>";

I recommend using gif's for the format since that way your page size will not be huge, you can do this by setting $cfg["type"] to "gif" (See here http://phpmygraph.abisvmm.nl/#ConfigDirectives)

This will also reduce the overhead of multiple requests and prevent hotlinking to the images.

You can read more about data URI's here http://en.wikipedia.org/wiki/Data_URI_scheme

Sign up to request clarification or add additional context in comments.

Comments

0

you might want to try

echo "< img src=\"graph.php?data=".urlencode(serialize($data_daily))."\">"

I might be misunderstanding which script is throwing the error, however (I'm presuming that it's graph.php that's giving you the provided data is not an array).

2 Comments

It didn't help. Still the same error returned by graph.php. I tried to do urlencode(json_encode($data_daily) et decode on the other side but it's the same...
What is printed out if you var_dump(unserialize($_GET['data']))
0

Try using json instead of serialize

echo "< img src=\"graph.php?data=".urlencode(json_encode($data_daily))."\">"

$graph->parseVerticalPolygonGraph(json_decode($_GET['data'],true), $cfg);

I see no reason for this to throw an error.

Comments

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.