topical media & game development

talk show tell print

server-php-sqlite-voting.php / php



  <?php
  header ("Content-type: image/png");
  im = imagecreatefrompng ("server-php-sqlite-voting.png");
  red = imagecolorallocate (im, 255, 0, 0);
  black = imagecolorallocate (im, 0, 0, 0);
  
  

create database


  
  db = new SQLiteDatabase("server-php-sqlite-voting.db"); 
  
  /* 
  db->query("BEGIN;
          CREATE TABLE voteoptions (id INTEGER PRIMARY KEY, name CHAR(20));
          INSERT INTO voteoptions (name) VALUES('hat');
          INSERT INTO voteoptions (name) VALUES('mug');
          INSERT INTO voteoptions (name) VALUES('shirt');
          INSERT INTO voteoptions (name) VALUES('cup');
          COMMIT;"); 
          
  db->query("BEGIN;
          CREATE TABLE poll (id INTEGER PRIMARY KEY, vote CHAR(20));
          INSERT INTO poll (vote) VALUES('hat');
          INSERT INTO poll (vote) VALUES('hat');
          INSERT INTO poll (vote) VALUES('shirt');
          INSERT INTO poll (vote) VALUES('mug');
          INSERT INTO poll (vote) VALUES('hat');
          INSERT INTO poll (vote) VALUES('hat');
          COMMIT;"); 
  */
  
  

query


  
  optionsquery = db->query("SELECT * FROM voteoptions");
  numoptions = optionsquery->numRows();
  pollquery = db->query("SELECT * FROM poll");
  numvotes = pollquery->numRows();
  
  

setup display(s)


  xval = 30;
  barwidth = floor(300/numoptions);
  for (i=0;i<=(numoptions-1);i++)
  {
      entry = optionsquery->fetchAll(SQLITE_ASSOC);
      voteoption = entry[i]['name'];
      votevalue = voteoption; //sqlite_result(optionsquery,i,'name');
      currentnumquery = db->query("SELECT * FROM poll WHERE vote='votevalue'");
      currentnum = currentnumquery->numRows();
      per = floor((currentnum/numvotes)*184);
      rper = floor((currentnum/numvotes)*100);
      imagefilledrectangle (im, xval, (200-per), (xval+barwidth), 200, red);
      imagerectangle (im, xval, (200-per), (xval+barwidth), 200, black);
      imagestring (im, 1, (xval+(barwidth/2)), 205, voteoption, black);
      imagestring (im, 2, (xval+(barwidth/2)), ((200-per)-15), "rper%", black);
      xval+=(barwidth+10);
  }
  

generate image(s)


  imagepng(im);
  // imagepng(im,'voting-plot.png');
  // echo '<center>';
  // echo '<img src=voting-plot.png width=100% height=100%>';
  // echo '</center>';
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.