tags:

views:

257

answers:

3

i have this file C:\\xampp\htdocs\exact\sample_pie.php that contains a graph. here's the code:

<?php
include("phpgraphlib.php");
include("phpgraphlib_pie.php");
include("connection.php");
$graph=new PHPGraphLibPie(400,200); 
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('exact');

$querypa = "SELECT COUNT(nature) FROM approved WHERE nature='Parade'";
$resultpa = mysql_query($querypa);
$printpa = mysql_result($resultpa,0);
$querycs = "SELECT COUNT(nature) FROM approved WHERE nature='Community Service'";
$resultcs = mysql_query($querycs);
$printcs = mysql_result($resultcs,0);
$queryga = "SELECT COUNT(nature) FROM approved WHERE nature='General Assembly'";
$resultga = mysql_query($queryga);
$printga = mysql_result($resultga,0);
$querypl = "SELECT COUNT(nature) FROM approved WHERE nature='Play/Showcase/Socio-Cultural Show/Film Showing'";
$resultpl = mysql_query($querypl);
$printpl = mysql_result($resultpl,0);
$queryco = "SELECT COUNT(nature) FROM approved WHERE nature='Competition/Sportsfest'";
$resultco = mysql_query($queryco);
$printco = mysql_result($resultco,0);
$queryfr = "SELECT COUNT(nature) FROM approved WHERE nature='Fund Raising'";
$resultfr = mysql_query($queryfr);
$printfr = mysql_result($resultfr,0);
$queryse = "SELECT COUNT(nature) FROM approved WHERE nature='Seminar/Convention/Conference/Training'";
$resultse = mysql_query($queryse);
$printse = mysql_result($resultse,0);


$totalAct=$printpa+$printga+$printpl+$printcs+$printco+$printfr+$printse;
$avepa=($printpa/$totalAct)*100;
$avecs=($printcs/$totalAct)*100;
$avega=($printga/$totalAct)*100;
$avepl=($printpl/$totalAct)*100;
$aveco=($printco/$totalAct)*100;
$avefr=($printfr/$totalAct)*100;
$avese=($printse/$totalAct)*100;
$pa=number_format($avepa,2);
$cs=number_format($avecs,2);
$ga=number_format($avega,2);
$pl=number_format($avepl,2);
$co=number_format($aveco,2);
$fr=number_format($avefr,2);
$se=number_format($avese,2);

$data=array("Parade"=>$pa, "General Assembly"=>$ga, "Play/Showcase/Socio-Cultural Show/Film Showing"=>$pl, "Community Service"=>$cs, "Competition/Sportsfest"=>$co, "Fund Raising"=>$fr, "Seminar/Convention/Conference/Training"=>$se);
$graph->addData($data);
$graph->setTitle("Total Activities per Nature of Activity");
$graph->setPrecision(2);
$graph->setLabelTextColor("0,0,0");
$graph->setLegendTextColor("0,0,0");
$graph->setGradient("210,245,255","pastel_purple");

$graph->createGraph();

?>

how can i get the graph as a .jpg through php script? i tried this file_get_contents("C:\\xampp\htdocs\exact\sample_pie.php"); but it only outputs this:

$pa, "General Assembly"=>$ga, "Play/Showcase/Socio-Cultural Show/Film Showing"=>$pl, "Community Service"=>$cs, "Competition/Sportsfest"=>$co, "Fund Raising"=>$fr, "Seminar/Convention/Conference/Training"=>$se); $graph->addData($data); $graph->setTitle("Total Activities per Nature of Activity"); $graph->setPrecision(2); $graph->setLabelTextColor("0,0,0"); $graph->setLegendTextColor("0,0,0"); $graph->setGradient("210,245,255","pastel_purple"); $graph->createGraph(); ?>

please help me. thanks.

+1  A: 

Using file_get_contents() like that is only going to retrieve the script itself. That text is what the file contains and that's what file_get_contents() does; exactly what it says on the tin.

Try looking at the documentation for PHPGraphLib or in the source code failing that. There should be some sort of save method you can call instead of createGraph() or maybe you can just pass a filename parameter to createGraph().

Ollie Saunders
A: 

On a sidenote, I should mention that your script is a lot more complicated than it should be. Everything between mysql_select_db() and $graph->addData() could be rewritten into something like this:

$data = array(
    'Parade' => 0,
    'Community Service' => 0,
    'General Assembly' => 0,
    'Play/Showcase/Socio-Cultural Show/Film Showing' => 0,
    'Competition/Sportsfest' => 0,
    'Fund Raising' => 0,
    'Seminar/Convention/Conference/Training' => 0
);

$sql = "SELECT nature, COUNT(*) AS cnt
          FROM approved
         WHERE nature IN ('" . implode("','", array_keys($data)) . "')
      GROUP BY nature";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result))
{
    $data[$row['nature']] = $row['cnt'];
}
$totalAct = array_sum($data);

foreach ($data as $nature => &$value)
{
    $value = number_format(100 * $value / $totalAct, 2);
}

For the rest, consult the documentation of your graph library as Ollie Saunders suggested.

Josh Davis
A: 
<img src="/exact/sample_pie.php" />

You may want to use header() to set the appropriate Content-type from within sample_pie.php.

hlpiii