1
Votes

Building a bar chart using PHP

Posted By tamilsolai on Mar 29, 2010   FROM: advancedphptutorial.blogspot.com report abuse

Building a bar chart using PHP, Building a bar chart

In the example below, using graphic functions we will build a bar chart based on the values stored in MySQL database. In our case, values represent poll results.In this example we will use poll database. To create it run the following SQL script in your MySQL server:
CREATE DATABASE `poll`;
USE poll;

CREATE TABLE `results` (
book_type VARCHAR(50),
num_votes INT
);

INSERT INTO `results` values
('Classic', 15),
('Fantasy', 7),
('Humor', 32),
('Mystery', 12),
('Poetry', 25);
Script building a bar chart is quite long, so let's divide it into two parts and discuss them separately. The first part of the script connects to MySQL database and requests data that will be represented in the bar chart.
<?php
//Querying the database to get poll results

//connection to the database
$dbhandle = mysql_connect("localhost", "root", "")
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("poll",$dbhandle)
or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM results");

$num_poller = mysql_num_rows($result);
$total_votes = 0;

//fetch the data from the database
while ($row = mysql_fetch_array($result)) {
$total_votes += $row{'num_votes'}; //calculating total number of votes
}

//nulling the pointer $result
mysql_data_seek($result,0);

//close the connection
mysql_close($dbhandle);

Read Full Story from advancedphptutorial.blogspot.com

Post new comment

  • Allowed HTML tags: <a> <strong> <ul> <ol> <li> <table> <tr> <td> <tbody> <embed> <object> <param> <b> <p> <i> <div> <h3> <h4> <br> <img> <style>
  • Lines and paragraphs break automatically.

More information about formatting options