How to create pie chart using PHP and MYSQL? - php

I have a table which shows my data and it works perfectly. I need to add in a pie chart also to show the exact same data that was shown on the table.
<thead>
<tr>
<th>Account Type</th>
<th>Account Number</th>
<th>Account Balance</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM account WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)) {
$acc_type = $row ['account_type'];
$acc_num = $row ['account_number'];
$acc_bal = $row ['account_balance'];
?>
<tbody>
<tr>
<td><?php echo $acc_type ?></td>
<td><?php echo $acc_num ?></td>
<td>$ <?php echo $acc_bal ?></td>
<?php
}
?>
</tr>
</tbody>
<tr class="alt">
<?php
$query = "SELECT SUM(account_balance) AS account_total FROM account WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)){
$acc_total = $row ['account_total'];
?>
<td colspan="2" align="right"><b>Total: </b></td>
<td align="right"><b>$ <?php echo $acc_total ?> </b></td>
<?php
}
?>
</tr>

Use a 3rd party.
Here is one. Look at their Example #15 - Basic Pie graphs
http://pchart.sourceforge.net/documentation.php?topic=exemple15

Why don't you try the Google graphs, that is the easiest way.
Check out the link
https://developers.google.com/chart/interactive/docs/gallery/piechart

If anyone is still looking for a solution I really just went to this site
https://developers.google.com/chart/interactive/docs/drawing_charts
and echo the whole code and it works.

Related

How to fix a table causing "502: Bad Gateway" error in PHP

I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>

PHP MySQL only displays Blank Table

Good Day! Im having a problem displaying the data from table. I have already inputted 6 data and it will show how many base from my $i++ but it does not show the userno, fullname and udate. i would really appreciate it Please and thank you! Table Display Here is my code:
table class="table table-striped table-sm">
<thead>
<tr>
<th>No</th>
<th>User No</th>
<th>User Name</th>
<th>Date Registered</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
$uquery= mysqli_query($conn, "SELECT * FROM users");
while ($uresult=mysqli_fetch_array($uquery)){
$i++;
?>
<tr>
<td><?php echo $i;?></td>
<td><?php $uresult ['userno'];?></td>
<td><?php $uresult ['fullname'];?></td>
<td><?php $uresult ['udate'];?></td>
<?php }; ?>
</tbody>
</table>
You forgot to echo your results.
Change
<td><?php $uresult ['userno'];?></td>
<td><?php $uresult ['fullname'];?></td>
<td><?php $uresult ['udate'];?></td>
To
<td><?php echo $uresult ['userno'];?></td>
<td><?php echo $uresult ['fullname'];?></td>
<td><?php echo $uresult ['udate'];?></td>
Or
<td><?= $uresult ['userno'];?></td>
<td><?= $uresult ['fullname'];?></td>
<td><?= $uresult ['udate'];?></td>
$sql = "SELECT userno, fullname, udate FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> userno: ". $row["userno"]. " - Full Name: ". $row["fullname"]. " - udate: " . $row["udate"] . "<br>";
}
} else {
echo "0 results";
}
//Don't Forget To:
$conn->close();
This'll be your PHP, adjust it accordingly to your table, hope this helps!
First of all, you have missed end</tr>, second do you really have to use variable i and increment it? where you can echo all ID? and why there's semi-colon after your closing bracket.Lastly, you should have put echo in variables uresult Hope this could help you
<table class="table table-striped table-sm">
<thead>
<tr>
<th>No</th>
<th>User No</th>
<th>User Name</th>
<th>Date Registered</th>
</tr>
</thead>
<tbody>
<?php
$uquery= mysqli_query($conn, "SELECT * FROM users");
while ($uresult=mysqli_fetch_array($uquery)){
?>
<tr>
<td><?php echo $uresult ['id'];?></td>
<td><?php echo $uresult ['userno'];?></td>
<td><?php echo $uresult ['fullname'];?></td>
<td><?php echo $uresult ['udate'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
and also you can write it, in this way:
and echo variable `i` if you want to see the count of it.
<?php
$uquery= "SELECT * from users";
$viewquery = mysqli_query($con,$uquery);
while($uresult = mysqli_fetch_assoc($viewquery))
{
?>

Created a table with 4 columns and 3 rows. How do I set up a php script to pull each row by id to generate 3 tr from the dababase?

The table I am looking to pull from my database is com/bzkItsK. It currently pulls the first row in the database but I am unsure about how to set up a script that will pull all the rows (currently 4) by their id to the webpage.
Here is the html as I have set it:
<table class="table table-striped">
<thead>
<tr>
<th>user id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
</tbody>
</table>
mysql_query is as such.
mysql_connect("localhost","?","?");
mysql_select_db("?");
$sql = mysql_query("SELECT * FROM users ORDER BY id ASC");
$id = 'id';
$user_id = 'user_id';
$first_name = 'first_name';
$last_name = 'last_name';
$dept = 'dept';
$rows = mysql_fetch_assoc($sql);
?>
I am trying to pull all 4 rows by id to be auto generated by a single table script.
you must iterate over your results.
$rows = mysql_fetch_assoc($sql);
will only fetch the first entry.
You have do something like this:
<?php while($rows = mysql_fetch_assoc($sql)): ?>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
<?php endwhile; ?>
EDIT:
And please do not use mysql, use mysqli instead.

PHP - How can I nest a while loop inside an if isset condition?

I have this table element with the following code:
<?php
if(isset($_POST["submit"])){
if (strlen($cIdMsg = 0) && strlen($cFirstNameMsg = 0) && strlen($cLastNameMsg = 0) && strlen($pCodeMsg = 0)) {
require_once("conn.php");
$sql2 = "SELECT * FROM customer;";
$results = mysqli_query($conn, $sql2)
or die ('Problem with query' . mysqli_error($conn));
echo "no errors found";
}
}
?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
Above this table I have the php code that makes the sql queries inside an if isset condition so that it only loads after pressing submit on the form. I would like to do the same to the table. That is to only make it load after pressing submit. because on page load it is trying to do the mysqli_fetch_array on a non existent $result yet
Wrap the whole table inside:
<?php if (isset($result)) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
I have used isset($result) based on what you have said. You can check for the POST values by checking for count($_POST), or something similar (not a good idea to check for isset($_POST["submit"])). If you are fetching for AJAX Response, it is always better to use a different separate file for this.
<?php if(mysqli_num_rows($result)!=0) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>

Handling three while loops in PHP MYSQL

I'm new to PHP and struck with a while loop. Please throw some lights here.
I'm showing some information related to books vs author vs isbn number.
There are two tables: book and author. The book name and ISBN number comes from book table. The author name comes from author table.
Here is my poor php code(please dont laugh)
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$result1 = mysql_query("SELECT book_name FROM book where status='1'") or die(mysql_error());
$result2 = mysql_query("SELECT FName FROM author") or die(mysql_error());
$result3 = mysql_query("SELECT book_isbn_number FROM book where status='1'") or die(mysql_error());
?>
<table style="margin-top:40px" border="1" width="100%">
<tr style="background-color:#909F51">
<td>Book List</td>
<?php while($row1 = mysql_fetch_array( $result1 )) { ?>
<td><?php echo $row1['book_name']; ?></td>
<?php } ?>
</tr>
<?php while($row2 = mysql_fetch_array( $result2 )) { ?>
<tr>
<td><?php echo $row2['author_name']; ?></td>
<?php while($row3 = mysql_fetch_array( $result3 )) { ?>
<td><?php echo $row3['book_isbn_number']; ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
I'm not able to loop through the third while condition. I know the basic code structure is wrong.
Could someone help me out in getting the result.
Tables:
book
b_id
book_name
isbn_number
status
author
a_id
author_name
Expected Output:
<table width="100%" border="1">
<tr>
<td></td>
<td>Author Name 1</td>
<td>Author Name 2</td>
</tr>
<tr>
<td>Book Name 1</td>
<td>ISBN Number 1</td>
<td>ISBN Number 2</td>
</tr>
<tr>
<td>Book Name 2</td>
<td>ISBN Number 3</td>
<td>ISBN Number 4</td>
</tr>
</table>
You are using $row3['isbn_number'] when the column you are selecting is "book_isbn_number". Try changing that line to be <td><?php echo $row3['book_isbn_number']; ?></td>. The loops themselves, while using the old, deprecated mysql_* functions, should be working fine.
<td><?php echo $row3['isbn_number']; ?></td>
replace it with
<td><?php echo $row3['book_isbn_number']; ?></td>
$result3 = mysql_query("SELECT book_isbn_number FROM book where status='1'") or die(mysql_error());
Should be
$result3 = mysql_query("SELECT isbn_number FROM book where status='1'") or die(mysql_error());**
OR
<td><?php echo $row3['isbn_number']; ?></td>
Should be
<td><?php echo $row3['book_isbn_number']; ?></td>
Whichever fits your need

Categories