creating a chart (line or bar) from php and mysql - php

i am new somehow to PHP and mysql,
I've simple database that receives the values from php form as id, date, time, max, today, tomorrow... i can insert into the table, all ok, i can also fetch the data to a php page using the below code, all ok, i can see the table of the values inserted in the db, all ok...
my need is to crate a simple line or bar chart that shows the max (which is static number each day) and the today number will be the variable that will be defined by the chart, so if it is a line it will show better that each day where is the number from the max... forget about tomorrow, it is not important...
here is my code of the display of the table of the page...
i want the simplest way to complete this, imagine only i have one column (today) that is changed each while or each day, so i want to show it in a graph against the max number... so i think the line chart will be suite best...
<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT id, date, time, max, today, tomorrow, FROM testtable1");
echo "<table border='1'>
<tr>
<th>id</th>
<th>date</th>
<th>time</th>
<th>max</th>
<th>today</th>
<th>tomorrow</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['max'] . "</td>";
echo "<td>" . $row['today'] . "</td>";
echo "<td>" . $row['tomorrow'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

I recommend using something like Google Visualizations for line charts. Visualizations are easy to implement and you can write them directly on the page see their example Line charts

Related

Reference two SQL databases with a Join via PHP

I have written an SQL statement which works in phpmyadmin. I have two databases on the same server which I have used an INNERJOIN to connect them and im getting the correct results. I am building this up in php and so far have done a few normal queries and got the correct results. I have an include for my db, which is connecting to one database, now I want to connect to two and im not sure how I reference this in in my php sql statement. Can someone please help.
I have this.
<?php
error_reporting(0);
include './includes/opendb.php';
extract($_POST);
$sql1 = mysql_query("SELECT f_clients.CLIENT_COMPANY, vtmastertrail.consultant, f_clients.CLIENT_CODE, vtcards.description, vtmastertrail.inspdate_start, vtmastertrail.inspdate_end, vtcards.typeofcard, vtcards.colour, vtcards.frequency, vtcards.priorityon, vtmastertrail.islive, f_clients.CLIENT_DEFAULTINSPECTIONSELL
FROM `f_clients`
INNER JOIN tcards.vtcards ON tcards.vtcards.client_code = f_clients.CLIENT_CODE
INNER JOIN tcards.vtmastertrail ON tcards.vtmastertrail.card_id = tcards.vtcards.id
WHERE tcards.vtmastertrail.consultant = '".$con_name."'
AND tcards.vtmastertrail.inspdate_start >= '".$from_date."'
AND tcards.vtmastertrail.inspdate_start <= '".$to_date."'");
echo "<table border='1' align='center'>
<tr>
<th>Consultant</th>
<th>Client Code</th>
<th>Client</th>
<th>Address</th>
<th>Inspection Start Date</th>
<th>Inspection End Date</th>
<th>Type Of T-Card</th>
<th>T-Card Colour</th>
<th>Frequency</th>
<th>Priority</th>
<th>Report Sent Out</th>
<th>Cost</th>
</tr>";
while($row = mysql_fetch_array($sql1))
{
echo "<tr>";
echo "<td>" . $row['consultant'] . "</td>";
echo "<td>" . $row['client_code'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['inspdate_start'] . "</td>";
echo "<td>" . $row['inspdate_end'] . "</td>";
echo "<td>" . $row['typeofcard'] . "</td>";
echo "<td>" . $row['colour'] . "</td>";
echo "<td>" . $row['frequency'] . "</td>";
echo "<td>" . ($row['priorityon'] ? 'Yes' : 'No') . "</td>";
echo "<td>" . ($row['signedoff'] ? 'Yes' : 'No') . "</td>";
echo "<td>" . $row['CLIENT_DEFAULTINSPECTIONSELL'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Im basically getting no results from this search.
here is my db file.
<?php
// ** database connection string
$conn = mysql_connect("localhost", "#####", "########") or die ('I cannot connect to the database because: ' . mysql_error());
// ** Tcards database
mysql_select_db ("tcards");
Ive changed hidden the credentials. I have another database called 'test' which is where the f_clients table is. This is on the same server and has the same login credentials. Can anyone help me out please?
You won't need to call mysql_select_db if you're planning to select from two separate databases.
Your query will need to use fully-qualified names (that means "test.f_clients.CLIENT_COMPANY", etc.). I think your ON clause will be OK as long as you fully qualify the DB names.
Note that MySQL functions (mysql_query, etc.) are deprecated; please consider switching to MySQLi or PDO. MySQLi is an easy switch.
Also pay attention to all Ian's comment above, about extracting the POST array and escaping data for your queries. You MUST code securely unless this is a company intranet behind a firewall someplace, and even then you cannot be assured it won't be exploited....
one thing you can do is add same user and password for both databases. or you can use view for overcome your problem. in practice i successful with adding same username and password to both databases. If you like try with "mysql workbench" that with easy to work with databases.

How to see what reports are received more than 2 days later than they were finished in SQL

Im trying to do a query to show only reports which were received more than two days after their end completion date, within a specified date range. I have this so far but cant quite figure out how to get the 'more than 2 days' part right. can anyone help me please.
$result = mysqli_query($con, "SELECT *
FROM tcards.vtmastertrail
INNER JOIN tcards.vtcards ON tcards.vtcards.id = tcards.vtmastertrail.card_id
WHERE tcards.vtcards.colour = 'Beige' AND datetime_report_received > (inspdate_end + 2) AND
inspdate_end >= '2014-04-01' AND inspdate_end <= '2014-04-30'
ORDER BY tcards.vtmastertrail.inspdate_end, datetime_report_received");
current output is a table like so:
echo "<table border='1'>
<tr>
<th>Report End Date date</th>
<th>Received Report date</th>
<th>Job Finish Date</th>
<th>Client</th>
<th>Client Code</th>
<th>Employee</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['inspdate_end'] . "</td>";
echo "<td>" . $row['datetime_report_received'] . "</td>";
echo "<td>" . $row['datetime_completed_report'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['client_code'] . "</td>";
echo "<td>" . $row['employee'] . "</td>";
echo "</tr>";
}
echo "</table>";
This info is being pulled by the query and displayed in my table but all the records 'datetime_report_received' is not 2 days more than the 'inspdate_end'. It is just displaying all records which have a 'inspdate_end within the daterange ive specified. Same result as i get if i remove the + 2) part of the query. so the +2) part must'nt be doing anything
cant post pic as i dont have good enough rep.
If you used the PHP function substr() to get the last two characters of the date in the database (so substr($variable, -2)) then you could add 2 to that as you have done in your code to check whether the date was before or after today.
It might be worth storing the time as well because with this currently, if I had a record in the database submitted at 2014-07-27 23:59 and I checked it at 2014-07-29 00:01, it would still return that two days have passed from the original submission of the entry to the database, when in reality only 1 day and 2 minutes have passed.

How to put a break in the output when specific values change using mysqli php

I am building a website to list statistics for bowling tournaments over the last 24 years. Using the following code generates a long, single table showing all the data. I would like to put a break in the table when the $row['season'] value changes, i.e., from 1990-1991 to 1991-1992, and for each subsequent change of seasons and echo either an html horizontal line between seasons or put the value of the season from the database, i.e., 2013-2014 at the top of each table segment. After a week of searching the web haven't figured out an answer. Here's the code I have now. Needs to be mysqli.
$result = mysqli_query($conn,"SELECT * FROM members INNER JOIN scores ON members.id=scores.memberID WHERE format LIKE '%s%' ORDER BY year, STR_TO_DATE( month, '%b' ), format ASC;");
echo "<table border='0'>
<tr>
<th>Name</th>
<th>Hometown</th>
<th>Month</th>
<th>Year</th>
<th>Season</th>
<th>Center</th>
<th>Center City</th>
<th>Format</th>
</tr>";
foreach($result as $row) {
echo "<tr>";
echo "<td>" . $row['firstName'] . " ". $row['lastName'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['month'] . "</td>";
echo "<td>" . $row['year'] . "</td>";
echo "<td>" . $row['season'] . "</td>";
echo "<td>" . $row['center'] . "</td>";
echo "<td>" . $row['centerCity'] . "</td>";
echo "<td>" . $row['format'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
The answer is actually already given but it looks like your new with php so here is a smal example i hope its useful to you.
You need to insert some if statements to check if there is any change of year. same goes for any other checks you want to perform.
this is something you could do...
<?php
$lastYear = null;//you can also set the first year manually of course
foreach($result as $row) {
//set the first year
if($lastYear == null){$lastYear = $row['year'];}
//check if the year changed or not
if($lastYear == $row['year']){
//if the year didnt change... do something
}else{
//your year changed... do something different
$lastYear = $row['year']; //update your 'last'year
}
}
?>
I hope this will help you.

php mysql echo | How to inherit colors from a submit form?

I'm having some real trouble finding information on this topic and I would be very appreciative for any help. In short I have a form where users select a category from a drop down list, enter some contents, and hit submit which goes to SQL. Each category in the dropdown is color coded:
<option STYLE="color: #00CC66;" value="Option_1">Option_1</option>
<option STYLE="color: #0066CC;" value="Option_2">Option_2</option>
<option STYLE="color: #996633;" value="Option_3">Option_3</option>
etc
Then I have a php that pulls up the stored submitted data (categories and contents) into a table on that same page sorted by date.
<?php
$con=mysqli_connect("localhost","myuser","mypassword","mydb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM mytable order by date DESC");
echo "<table border='1'>
<tr>
<th>Category</th>
<th>Contents</th>
<th>Date/Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['contents'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
My question is, when echo places the information in the table, is there a way I can get 'category' showing up in the same colors as the user form? (IE, on the table, Option_1 would show up as #00CC66, Option 2 as 0066CC, etc...)
Basically I want the actual category text on the fetched table to display the same as it is in the drop down form. I don't mind if I need to manually set each one as the categories are limited, I just have no clue where to begin on this one. Appreciate any help in advance!
Yes, but you would need to either change the value of the select box to the colour or manually do it like this:
function getColor($strOption)
{
switch ($strOption)
{
case "Option_1":
return "#00CC66";
case "Option_2":
return "#0066CC";
#etc
}
}
Then in your while loop:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><font color='".getColor($row['category'])."'> " . $row['category'] . "</font></td>";
echo "<td>" . $row['contents'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}

Once we create trigger in php page then do we need to create same in Mysql?

I am using Mysql for database purpose for my php code.
I have created trigger in php code as below, Now do I need to create it in mysql??
My following insert data into table, and also show content of tables. But action I performed in trigger does not make any change. Is there any problem in trigger?
Once it started working fine but after i changed table name it stopped working though I kept table name same my php page and mysql.
<html>
<body>
<?php
$id=$_POST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$city=$_POST['city'];
$con=mysqli_connect('127.0.0.1:3306' ,'root','root','my_db');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="select * from student";
$result = mysqli_query($con,$sql1);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>City</th>
</tr>";
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";
}
echo "</table>";
**$sql3 = "CREATE TRIGGER MysqlTrigger AFTER INSERT ON student FOR EACH ROW BEGIN INSERT INTO details VALUES ($id,$fname,$lname,$city);";**
mysqli_query($con,$sql3);
$sql5="INSERT INTO student (id,fname, lname, city)
VALUES
('$_POST[id]','$_POST[fname]','$_POST[lname]','$_POST[city]')";
mysqli_query($con,$sql5);
echo "1 record added";
print "<h2>After performing Trigger updated table details</h2>";
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>City</th>
</tr>";
$sql4="select * from details";
$res = mysqli_query($con,$sql4);
while($row = mysqli_fetch_array($res,MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Short answer - no, you don't have to because creating it in the code also creates it in MySQL. But you have bigger problems.
Longer answer -
Triggers are part of the database, and typically you wouldn't create a trigger from code. You would create triggers the same way you create tables - create them once in MySQL and they stick around until you drop them.
Technically the code you have will work, but the CREATE TRIGGER statement will only succeed the first time it is called. In subsequent executions of that script the CREATE TRIGGER will error out because the trigger already exists. But since you aren't checking for errors, your script will probably continue on happily.
Also, the way your trigger is made, it will always insert the same record into the details table that was inserted when the trigger was created.
Finally, you have some serious security issues with your code:
You are directly using POST variables in SQL which opens you up to SQL Injection
Whatever user your site is running as probably shouldn't have permissions to execute DDL statements like CREATE TRIGGER

Categories