Bar Graph using the Data from MySQL Database - php

Hello Everyone Good Afternoon
as of now here's what I've got
Database: Election2016
Table Name: Candidate_Info
Here is the 2 of my fields in my Table that I think is important in creating my Output
candidatename[varchar] and numberofvotes[integer]
as of now heres my data
candidatename numberofvotes
Person1 24
Person2 1
From this Scratch How can i create a Bar Graph?
Im sorry for asking this easy question. Im only a beginner in this thing.
Im using PHP and it will be used in website
any help is appreciated TY
As of now here is my code
inside mysql_graph_bar.php
<?php
include("phpgraphlib.php");
$graph=new PHPGraphLib(550,350);
$link = mysql_connect('localhost', 'root', '')
or die('Could not connect: ' . mysql_error());
mysql_select_db('election2016') or die('Could not select database');
$dataArray=array();
//get data from database
$sql="SELECT * from candidate_info group by position";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$salesgroup=$row["salesgroup"];
$count=$row["count"];
//add to data areray
$dataArray[$salesgroup]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("candidates");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
inside bargraph.php
<html>
<h3>This is where I want to display my graph</h3>
<img src="mysql_graph_bar.php" />
</html>

Related

Fetching SQL Current date for Graph lib php from timestamp

So i have setup the code to display graph from php sql. But i cant seem to order it by todays date from a timestamp in my table. Here is my code:
<?php
include("phpgraphlib.php");
$graph=new PHPGraphLib(550,450);
$link = mysql_connect('xxx', 'xxx', 'xxx')
or die('Could not connect: ' . mysql_error());
mysql_select_db('test') or die('Could not select database');
$dataArray=array();
//get data from database
// $sql="SELECT rating, COUNT(*) AS 'datentry' FROM main_table GROUP BY rating";
$sql="SELECT rating, COUNT(*) AS 'datentry' FROM main_table GROUP BY rating";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$salesgroup=$row["rating"];
$count=$row["datentry"];
//add to data areray
$dataArray[$salesgroup]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Daily");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
What i want to achieve is to be able to display just todays data. This specific code displays all of the data in my table.

Download a file in PHP

I'm trying to download a PowerPoint file. I'm saving its path in column slide in a table called lesson. Whenever I try to download it, it downloads the whole table.
All I want is the column slide, how can I do that?
// connect to the database
$link = mysql_connect('localhost','root','');
if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}
// query the server for the file
$L_ID = $_GET['id'];
$query = "SELECT * FROM lesson WHERE LID = '$L_ID'";
$result = mysql_query($query) or die(mysql_error());
// define results into variables
$name=mysql_result($result,0,"Lname");
$content=mysql_result($result,0,"slide");
header("Content-disposition: attachment; filename=$name");
echo $content;
mysql_close();
If all you want is the column slide, you have to select only that column in your select clause. Right now you're selecting all of the table's columns by using SELECT *.
Try this:
$query = "SELECT slide FROM lesson WHERE LID = '$L_ID'";
That should only return the column slide from table lesson.

Display Multiple IDs columns from table

At the moment I am displaying only the first ID sorted ascending.
I have to display multiple ID results, from the same table. How would I accomplish this task?
<?php
$dbhost ='localhost';
$dbuser =‘user’; $dbpass =‘pass’;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn){
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM table_name ORDER BY id ASC LIMIT 0, 1';
mysql_select_db('database_name’);
$retval = mysql_query($sql, $conn);
if(!$retval){
die('Could not get data: ' . mysql_error());
}
?>
<?php while($row = mysql_fetch_assoc($retval))
{echo "{$row[‘full_name’]}”.”
{$row[‘telephone']}"."
{$row[‘email’]}”;}
mysql_close($conn);
?>
All you have to do is change the LIMIT 1 to whatever number you want, or just remove it. Removing it would look like:
$sql = 'SELECT * FROM table_name ORDER BY id ASC'
why do you have a limit if you want all the ids to be displayed?
you can use a while loop in your script
for eg:
while($row = mysql_fetch_assoc($test)){
<div id="'.$row['id'].'">'.$row['id']'.</div>;
}
which will output:
<div id="my id 1">my data 1</div>
<div id="my id 2">my data 2</div>
<div id="my id 3">my data 3</div>
.
.
.
and so on
your modified code that outputs the data in different divs
<?php
$db = mysqli_connect("localhost", "user", "pass" ,"database name") or die("Could not connect database");//keep it in one line and use (mysqli) instead of (mysql) because newer mysql versions donot support (mysql)
$retreive=mysqli_query($db,'SELECT * FROM table_name ORDER BY id');//if youre connection is in another file you need to include the connection variable before the selection line.In this case $db is the connection variable
while($row=mysqli_fetch_array($retrieve))
{ the following code creates a div for each result
echo '<div class="'.$row['fullname'].'">'.$row['fullname'].'</div>';.
echo '<div class="'.$row['telephone'].'">'.$row['telephone'].'</div>';
echo '<div class="'.$row['email'].'">'.$row['email'].'</div>';
}
?>
tried the above code on my test server and it works

Dynamic display of table based on values in combobox using MySQL and PHP

I have two tables in MySQL db namely vendor and menu.
vendor table
vendor_id(Primary) vendor_name
menu table
menu_id(Primary) menu_details cost vendor_id(Foreign)
I need to create a combo box which fetches vendor_name instances from the MySQL db dynamically. On selection of vendor_name from the combo box, menu table(HTML and table should be in editable mode) should be displayed from the MySQL database. I am new to MySQL and PHP. So please help me to deal with this criteria.
Below is the code that I'm working on:
<html><body>
<select name="vendor" id="vendor">
<option value="Choose">Choose</option>
<?php $conn = mysql_connect('localhost','root','');
if (!$con) { die('Could not connect: ' . Mysql_error()); }
mysql_select_db('project',$conn);
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
while($fetch= mysql_fetch_array($query)){
echo "<option>".$row[1]."</option>";
}?>
</select>
</body></html>
If I understood You correctly then this should do the thing that you're after. It displays the combobox from vendor table and if you change the selection, it reloads the page and shows the menu that corresponds to the vendor_id in the menu table.
<html>
<body>
<?php
// connecting to the server
$conn = mysql_connect('localhost','root','') or die('Could not connect: ' . Mysql_error());
// selecting the database
mysql_select_db('project',$conn) or die("db error: ".mysql_error());
// is menu_id set?
if (isset($_GET['menu']) && intval($_GET['menu'])>0) {
// if yes, then query the menu items
$query = "select menu_id, menu_details, cost, vendor_id from menu where vendor_id=".intval($_GET['menu']);
$result = mysql_query($query, $conn) or die("SQL error: ".mysql_error());
while ($row = mysql_fetch_assoc($result)) {
// print the menu items
print_r($row);
}
}
// select the vendor combobox
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
// If the vendor selection has changed then reload the page
?>
<select name="vendor" id="vendor" onchange="document.location='index.php?menu='+this.options[this.selectedIndex].value;">
<option value="Choose">Choose</option>
<?php
// loop through the results
while($fetch= mysql_fetch_assoc($query)){
echo "<option value=\"".$row['vendor_id']."\">".$row['vendor_name']."</option>";
}
?>
</select>
</body>
</html>
the assumption is that the file is named index.php. (look at the select onchange="" filename. You'd need to change it if You have a different filename.

Can't get results from mySQL query to display in PHP

I am very new to PHP and can't seem to use mySQL data at all from PHP. The SQL query I wrote works fine in phpMyAdmin when I run it in the SQL editor, but the most I am able to get from the code is the following from var_dump. Nothing else displays anything at all.
resource(3) of type (mysql result)
Any help at all would be greatly appreciated!!
<?php
ini_set(‘display_errors’,1);
error_reporting(E_ALL|E_STRICT);
$con = mysql_connect("localhost","XXXXXXXX","XXXXXXX");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sharetrader", $con);
$sharelist = mysql_query("SELECT DISTINCT tblStocks.stockSymbol, tblShareData.lookupDate FROM tblStocks LEFT JOIN tblShareData ON tblShareData.tickerCode = tblStocks.stockSymbol ORDER BY tblShareData.lookupDate ASC LIMIT 0 , 30");
if (!$sharelist) {
die('Invalid query: ' . mysql_error());
}
var_dump($sharelist);
while ($row = mysql_fetch_array($sharelist)) {
echo $row['tblStocks.stockSymbol'];
}
mysql_close($con);
?>
I am very new to PHP
But you're making great progress - just missing some of the finer points.
try:
while ($row = mysql_fetch_array($sharelist)) {
var_dump($row);
}
...and it should be obvious what's happening.

Categories