+ Post New Thread
Results 1 to 9 of 9
Coding Thread, Creating simple bar graphs in PHP in Coding and Web Development; Hello I am trying to create simple bar graphs in my php file wherein in the first part of my ...
  1. #1
    ivn
    ivn is offline

    Join Date
    Aug 2010
    Posts
    7
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Creating simple bar graphs in PHP

    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α 0 E( {e gH(=!Žɥ5 @ @ @ @ @ @ @ @ w5wyf8["#~$Iz'����IENDB`
    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

  2. #2
    danbuntu's Avatar
    Join Date
    Dec 2009
    Location
    Maidstone, Kent
    Posts
    289
    Thank Post
    0
    Thanked 52 Times in 49 Posts
    Rep Power
    19
    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

  3. #3

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,613
    Thank Post
    1,229
    Thanked 772 Times in 670 Posts
    Rep Power
    234
    Quote Originally Posted by ivn View Post
    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

  4. #4

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,529
    Thank Post
    513
    Thanked 2,406 Times in 1,862 Posts
    Blog Entries
    24
    Rep Power
    822
    Can you post the code for the image generation script, and for the file where you're then embedding the image?

  5. #5
    ivn
    ivn is offline

    Join Date
    Aug 2010
    Posts
    7
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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<BR>";
    $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<br>";
    }
    $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 "<br>$selectedoptions[$i]<br>";
    }
    $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 "<div class=\"studenthelp mdl-align\">";
    echo "<h2 style='font-family:Sans Serif; font-size:16pt; text-align:center; color:red; background-color:#FFFF66;'>Count</h2>";
    echo "<table> <tr> <th> Q.Text </th> <th> Options </th> <th> Count </th> <th>&nbsp;&nbsp; % Count </th> </tr>";
    echo "<tr> <td rowspan=5>$prevques</td></tr>";
    echo "<tr> <td>$alloptions[0]</td> <td>&nbsp;&nbsp;$countA</td><td>&nbsp;&nbsp; $formatA %</td></tr>";
    echo "<tr> <td>$alloptions[1]</td> <td>&nbsp;&nbsp;$countB</td><td>&nbsp;&nbsp; $formatB %</td></tr>";
    echo "<tr> <td>$alloptions[2]</td> <td>&nbsp;&nbsp;$countC</td><td>&nbsp;&nbsp; $formatC %</td></tr>";
    echo "<tr> <td>$alloptions[3]</td> <td>&nbsp;&nbsp;$countD</td><td>&nbsp;&nbsp; $formatD %</td></tr>";
    echo "</table><br>";
    echo "</div>";


    $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);
    ?>

  6. #6

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,613
    Thank Post
    1,229
    Thanked 772 Times in 670 Posts
    Rep Power
    234
    Quote Originally Posted by ivn View Post
    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

  7. #7
    ivn
    ivn is offline

    Join Date
    Aug 2010
    Posts
    7
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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.

  8. #8

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,529
    Thank Post
    513
    Thanked 2,406 Times in 1,862 Posts
    Blog Entries
    24
    Rep Power
    822
    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 <img> tags for any images, calling the second script, and passing parameters to it for the creation of any dynamic features.

  9. #9

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,403
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    168
    Try this

    Save your code as a separate PHP file and then use this:

    Code:
    <img src="[Insert PHP file name]">

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 1
    Last Post: 11th June 2010, 09:23 PM
  2. Simple PHP mail save script
    By phpelite in forum Scripts
    Replies: 2
    Last Post: 1st December 2009, 12:56 AM
  3. Simple way of creating photo-slideshow-with-music
    By dhicks in forum How do you do....it?
    Replies: 21
    Last Post: 11th May 2009, 12:15 PM
  4. Simple php framework
    By Jona in forum Web Development
    Replies: 2
    Last Post: 2nd May 2008, 02:43 PM
  5. Simple PHP script for notes about students?
    By ChrisS in forum Scripts
    Replies: 1
    Last Post: 29th November 2007, 10:25 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •