SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jan 2001
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generate graphic bars and percent for a Poll

    Hello,
    I'm making a Poll script and I have all set up for the registration and validating them. But I'm now in the part where the script reads the values in the MySQL table and displays it in a graphical way. The result is for 3 polls and there are 5 choices for each poll. When the data is recorded in the DB is something like this.
    id poll1 poll2 poll3
    01 5 5 3
    02 2 3 1
    03 1 3 5
    04 1 1 1
    05 2 5 2
    06 2 5 1
    07 2 3 3
    08 5 1 4
    09 2 5 5
    10 5 1 3
    id is the unique id for each record (user vote on the polls).
    poll1 thru poll3 are the values for each of the polls that the user voted.
    There are other fields in the table, but they won't be used for what I want now that is to generate the graphical values and percent of each poll.

    I don't know what more I can write to explain what I'm looking, but If someone can help me and has any question in order to be able to help me, I'll be very grateful.
    Last edited by pulsorocker; Jul 1, 2002 at 12:59.
    Guillermo

  2. #2
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You really didn't give any indication as to what in particular you are looking for.

    Do you want help graphing the data? If this is the case, what type of graph? What data should be in the graph, and how?

  3. #3
    SitePoint Addict
    Join Date
    Jan 2001
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes... I want to generate the graph. Just like a normal poll graph where the choices are displayed and the values of each of them besides, along with the percent value. The graph should be for the 3 polls in the table (poll1 thru poll3) and each poll has 5 options (or values that the user can vote on)
    Guillermo

  4. #4
    SitePoint Addict
    Join Date
    Jan 2001
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... let me see if I can explain myself a little bit more. For example, let look at the column for the poll1
    It has 10 votes in total (10 rows). Here are the results for the poll1.

    Choice 1 = 2 votes
    Choice 2 = 5 votes
    Choice 3 = 0 votes
    Choice 4 = 0 votes
    Choice 5 = 3 votes

    That is the data for column poll1. I need that the script generates the graphical value of that and the percent value. And the same for the other polls.
    Guillermo

  5. #5
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's one way you can handle it...

    for each of the three polls, you can call this script.
    PHP Code:
    <?php
    /**
     * data is posted to this file via GET in
     * variables p1 - p10 and output as a bar graph
     *
     * an example of calling this script:
     * <img src="graph.php?p1=3&p2=8&p3=9&p4=2&p5=4&p6=10&p7=3&p8=1&p9=9&p10=7" border="0">
     */

    //let's get data from the request
    for($i=1$i<=10$i++){
        
    //$pts[$i] = $HTTP_GET_VARS["p$i"];
        
    $pts[$i] = $_GET["p$i"];
    }

    if(
    $img imagecreate(100,110)){

        
    //tell the browser it's a JPEG
        
    header('Content-type: image/png');

        
    //allocate some colors
        
    $black imagecolorallocate($img,0,0,0);
        
    $color[] = imagecolorallocate($img,255,255,255);
        
    $color[] = imagecolorallocate($img,255,0,0);
        
    $color[] = imagecolorallocate($img,0,255,0);
        
    $color[] = imagecolorallocate($img,0,0,255);
        
    $color[] = imagecolorallocate($img,255,255,0);
        
    $color[] = imagecolorallocate($img,255,0,255);
        
    $color[] = imagecolorallocate($img,0,255,255);
        
    $color[] = imagecolorallocate($img,255,127,0);
        
    $color[] = imagecolorallocate($img,0,127,255);
        
    $color[] = imagecolorallocate($img,0,200,127);
        
    $color[] = imagecolorallocate($img,127,0,200);

        
    //start with a white background
        
    imagefill($img00$color[0]);

        
    //make some bars
        
    for($i=1$i<=10$i++){
            
    $x $i 10;
            
    $y 10 $pts[$i];
            
    imagefilledrectangle($img$x-910 $y$x100$color[$i]);
            
    imagestring($img110*$i-9102$i$black);
        }

        
    //send the image to the browser
        
    imagepng($img);

        
    //free resources
        
    imagedestroy($img);
    }
    ?>

  6. #6
    SitePoint Addict
    Join Date
    Jan 2001
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi randem
    Here is the script as you posted. Is that what is supposed to appear?
    Guillermo

  7. #7
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My, my, my.... that does look a bit fishy. It worked for me here.

    Anyhow, having already written this script last night, I decided to add a little more to the script, make it a little easier to use, and post it on my web site. It's a little more dynamic now, and seems to work fine for me.

    http://www.serverscripting.com/view/1162.html

    Let me know if that helps.


Bookmarks

Posting Permissions

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