Jump to content

Recommended Posts

Posted

Hello

 

I am trying to create simple bar graphs in my php file wherein in the first part of my code I am retrieving the values from the database, and based on the count of those values I want to create simple bar graph. But whenever I am trying to create a graph it throws me an junk values like this,

‰PNG ��� IHDR��@���ÿ���ýç-��� PLTEÿÿÿ���ÿ��Àɝ°���{IDATxœíα €0EÁ( {e gH¦(=!ŽâËÕÉ¥è5 @ @ @ @ @ @ @ @ w€5wyfÇ8³¸[¹"ú#~ý¡$Izׁí'š‰Óú����IEND®B`‚

Please let me know how can I solve this error and how do I create a simple bar graph based on values from database.

 

Regards

ivn

Posted

what are you using to create the graphs?

 

I've had good success with google charts, have a search around on the web and you'll find a good few helper classes to download to interface it with PHP.

 

I've also used jpgraph which is pretty good

Posted
But whenever I am trying to create a graph it throws me an junk values

 

The string you posted looks like it might be the first part of a PNG file - remember to set the MIME type for PNG instead of plain text.

 

--

David Hicks

Posted

I kind of do not have an idea how to set the MIME type. Can someone please let me know how should i do it? the code I have written over all is as follows, the first part is retrieving the values I want the graph for, the second part is creating the graph. Please go through the code nd let me know what I can do.

 

<?php

require_once("../../config.php");

require_once("lib.php");

 

$qval='';

$ansval=array();

$arr=array();

$options=array();

$alloptions=array();

$selectedoptions=array();

$countA=0;

$countB=0;

$countC=0;

$countD=0;

$percentA=0;

$percentB=0;

$percentC=0;

$percentD=0;

$formatA=0;

$formatB=0;

$formatC=0;

$formatD=0;

echo "HI
";

$query="select c.currentquestion from {$CFG->prefix}quiz_currentquestion c where c.id=1";

$execute=get_records_sql($query);

foreach($execute as $currentquestion => $execute1)

{

$qval=$execute1->currentquestion;

echo "$qval

";

}

$opquery="select s.id,s.answer from {$CFG->prefix}question_states s where s.question in ($qval) and s.seq_number=1";

$opexec=get_records_sql($opquery);

foreach($opexec as $answer => $opexec1)

{

$ansval[]=$opexec1->answer;

}

$n=count($ansval);

echo "$n";

for($i=0;$i<$n-1;$i++)

{

$arr = explode(":",$ansval[$i]);

$selectedoptions[$i]=$arr[1];

//echo "

$selectedoptions[$i]

";

}

$options=explode(",",$arr[0]);

$opcount=count($options);

//Keeping a count of options selected by user.

for($r=0;$r<$n-1;$r++)

{ switch($selectedoptions[$r])

{ case $options[0]:

$countA=$countA+1;

//$percentA=$countA/$c*100;

break;

case $options[1]:

$countB=$countB+1;

//$percentB=$countB/$c*100;

//$formatB=number_format($percentB, 2, '.', '');

break;

case $options[2]:

$countC=$countC+1;

//$percentC=$countC/$c*100;

//$formatC=number_format($percentC, 2, '.', '');

break;

case $options[3]:

$countD=$countD+1;

//$percentD=$countD/$c*100;

//$formatD=number_format($percentD, 2, '.', '');

break;

}

}

$percentA=$countA/$n*100;

$formatA=number_format($percentA, 2, '.', '');

$percentB=$countB/$n*100;

$formatB=number_format($percentB, 2, '.', '');

$percentC=$countC/$n*100;

$formatC=number_format($percentC, 2, '.', '');

$percentD=$countD/$n*100;

$formatD=number_format($percentD, 2, '.', '');

//Displaying answer text for feedback table.

for($p=0;$p<$opcount;$p++)

{

$optionquery="select a.answer from {$CFG->prefix}question_answers a where a.id in ($options[$p])";

$opanswer=get_records_sql($optionquery);

foreach($opanswer as $answer => $opanswer1)

{

$oneoption=$opanswer1->answer;

$alloptions[$p]=$oneoption;

}

}

//Displaying the questiontext

$qtextquery="select q.questiontext from {$CFG->prefix}question q where q.id in ($qval)";

$qtextanswer=get_records_sql($qtextquery);

foreach($qtextanswer as $questiontext => $qtextanswer1)

{

$prevques=$qtextanswer1->questiontext;

}

echo "

";

echo "

Count

";

echo "

";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "
Q.Text Options Count    % Count
$prevques
$alloptions[0]   $countA   $formatA %
$alloptions[1]   $countB   $formatB %
$alloptions[2]   $countC   $formatC %
$alloptions[3]   $countD   $formatD %

";

echo "

";

 

 

$values=array($alloptions[0] => $countA, $alloptions[1] => $countB, $alloptions[2] => $countC, $alloptions[3] => $countD);

$sum = array_sum($values);

 

$height = 255;

$width = 320;

 

$im = imagecreate($width,$height); // width , height px

 

$white = imagecolorallocate($im,255,255,255);

$black = imagecolorallocate($im,0,0,0);

$red = imagecolorallocate($im,255,0,0);

 

 

imageline($im, 10, 5, 10, 230, $black);

imageline($im, 10, 230, 300, 230, $black);

 

 

$x = 15;

$y = 230;

$x_width = 20;

$y_ht = 0;

$i=0;

for ($i=0;$i<9;$i++){

 

$y_ht = ($values[$i]/$sum)* $height;

 

imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);

imagestring( $im,2,$x-1,$y+10,$values[$i],$black);

 

$x += ($x_width+20);

 

}

 

imagejpeg($im);

?>

Posted
I kind of do not have an idea how to set the MIME type.

 

Me neither, but checking Google ("php set mime type") suggests you need to call the header function first thing:

 

<?php

header('Content-Type: image/png');

...

 

--

David Hicks

Posted
Setting the header right at the start would give me no output, as the values of which I want to create a bar graph is in the first part of the code. So putting the header in the start of the code just prints the url of that file back on the screen.
Posted

You appear to be creating a HTML page, and then trying to embed an image directly into it. That won't work - a PHP page is simply pre-processing and then presenting an HTML page. You can't embed an image directly into HTML.

 

Instead, you need 2 scripts. One which presents the page, and one which creates the image. The first one would then have tags for any images, calling the second script, and passing parameters to it for the creation of any dynamic features.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...