all Developers.
I am developing School Management System, in the Database, I have two tables one is For Subjects, and the other one is designed for obtained marks of the Subjects and it is called scores.
So I am trying to fetch subject Names as Table Head
I have another Query below this query and I am trying to fetch from scores as table data .
At this point, I failed to match the subject name and its score from the scores table.
Here is my code:
<table class="table table-striped table-bordered">
<head>
<tr>
<?php
// Query for Subject Names
$view_subject = $config->prepare("SELECT * FROM subjects");
$view_subject->execute();
while($row = $view_subject->fetch()){
$sub_name = htmlspecialchars($row['sub_name']);
?>
<th class="text-center"style="background-color:#395C7F;color:#fff;"><?php echo $sub_name;?></th>
<?php } ?>
</tr>
</thead>
<body>
<?php
// Query for Subject Scores
$view_scores = $config->prepare("SELECT * FROM scores INNER JOIN subjects ON scores.score_sub_id = subjects.sub_id WHERE scores.std_rand = :random_id ORDER BY scores.score_sub_id ASC");
$view_scores->execute(['random_id' => $rand_ID]);
while($row = $view_scores->fetch()){
$score_id = htmlspecialchars($row['score_id']);
$score_sub_id = htmlspecialchars($row['score_sub_id']);
$score_mid_amount = htmlspecialchars($row['score_mid_amount']);
$score_final_amount = htmlspecialchars($row['score_final_amount']);
?>
<tr>
<td class="text-black" data-title="Subject"><?php echo $score_mid_amount;?></td>
</tr>
<?php } ?>
</tbody>
</table>
Database images:
1- Subjects table
2- Scores table
** Browser UI **
On your second loop you have entered '<tr>' wrapping each '<td>' that means that each one arrives at a different line , '<td>'s should be as much as there are '<th>' for each line.... so :
<?php
// Query for Subject Scores
$view_scores = $config->prepare("SELECT * FROM scores INNER JOIN subjects ON scores.score_sub_id = subjects.sub_id WHERE scores.std_rand = :random_id ORDER BY scores.score_sub_id ASC");
$view_scores->execute(['random_id' => $rand_ID]);
?>
<tr>
<?php
while($row = $view_scores->fetch()){
$score_id = htmlspecialchars($row['score_id']);
$score_sub_id = htmlspecialchars($row['score_sub_id']);
$score_mid_amount = htmlspecialchars($row['score_mid_amount']);
$score_final_amount = htmlspecialchars($row['score_final_amount']);
?>
<td class="text-black" data-title="Subject"><?php echo $score_mid_amount;?></td>
<?php } ?>
</tr>
</tbody>
</table>
This should fix your table but it will only create one line! if you have more than one line you will need to add another loop to wrap this one and it will create the new '<tr>' outside the inner loop.
BTW: I assume that the 2nd while loop is exactly long as the first one... since you are supposed to have the same amount of <td> per line per <th> if it's not in the same length or not sorted the same way you will have an issue... which can be resolved either by adjusting your SELECT or creating an array with ids and injecting to it the data from the second loop according to the keys brought in the first.
I have a simple table that shows invoices and the date they were created. I am looking for a way to show the total of the invoices values at the bottom of the last row of every month while showing the table.
Example:
Date
Amount
04-05-2020
$50
02-05-2020
$30
14-05-2020
$10
TOTAL
$90
Date
Amount
04-07-2020
$15
02-07-2020
$15
14-07-2020
$15
TOTAL
$45
Any idea? This is what I have so far:
<!DOCTYPE html>
<html>
<head>
<title>FACTURAS</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$server = "localhost";
$user = "root";
$password = "";
$db = "facturas";
$conn = new mysqli($server, $user, $password, $db);
if ($conn->connect_error) {
die("CONNECTION ERROR " . $conn->connect_error);
}
$sql = "SELECT * FROM facturas_table ORDER BY id";
$facturas = $conn->query($sql);
echo '<table>
<thead>
<tr>
<td>ID</td>
<td>DATE</td>
<td>INVOICE</td>
<td>PRICE</td>
</tr>
</thead>';
while ($row = $facturas->fetch_assoc()) {
$month = substr($row['fecha'], 3);
// $totalMonths = "SELECT SUM(monto) as montoTotal FROM facturas_table WHERE fecha = ''"
echo '
<tbody>
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['fecha'].'</td>
<td>'.$row['factura'].'</td>
<td>'.$row['monto'].'</td>
</tr>
</tbody>';
}
echo '</table>';
?>
</body>
</html>
First of all try to divide html from php if anyhow possible, as the mix will create problems in the future. Also try to use english language for all variable names (I assume monto is the price).
What I got from your question is, that you try to create the table and also calculate the total. Therefor you would create a variable for the sum and add the current amount of each row to the sum variable (in each iteration of the loop).
I changed the relevant php part:
UPDATED to your comment (sorry this was not so clear from your question).
Important is the trick with $lastMonth and $month, as well as with $first
<tbody>
<?php
$total = 0; $lastMonth = ""; $first = true;
while ($row = $facturas->fetch_assoc()) :
$month = substr($row['fecha'], 3);
$total += $row['monto']; ?>
<?php if(!$first && $lastMonth != $month): ?>
<tr>
<th colspan="2">Total (<?=$month?>): </th>
<th colspan="2"><?=$total?></th>
</tr>
<?php $total=0; endif; ?>
<tr>
<td><?=$row['id']?></td>
<td><?=$row['fecha']?></td>
<td><?=$row['factura']?></td>
<td><?=$row['monto']?></td>
</tr>
<?php $first=false; $lastMonth = $month; endwhile; ?>
<tr>
<th colspan="2">Total (<?=$month?>): </th>
<th colspan="2"><?=$total?></th>
</tr>
</tbody>
</table>
didn't try out the code (so be sure to check for syntax), but you can get the idea how to do that pretty well
while #helle's answer focuses on a solution on PHP side, here's an answer on the database side:
create table `facturas_table`(`id` bigint primary key, `invoice` varchar(100), `price` bigint, `date` date);
insert into `facturas_table` (`id`, `invoice`, `price`, `date`) values
(1, "A25C45", 5000, str_to_date("04-05-2020", "%d-%m-%Y")),
(2, "A25C46", 3000, str_to_date("02-05-2020", "%d-%m-%Y")),
(3, "A25C47", 1000, str_to_date("14-05-2020", "%d-%m-%Y")),
(4, "A25C48", 1500, str_to_date("04-07-2020", "%d-%m-%Y")),
(5, "A25C49", 1500, str_to_date("02-07-2020", "%d-%m-%Y")),
(6, "A25C50", 1500, str_to_date("14-07-2020", "%d-%m-%Y"))
;
select * from (
select `id`, `invoice`, `price`, date_format(`date`, "%d-%m-%Y") `date`, date_format(`date`, "%Y-%m-%d") `ordering_format` from `facturas_table`
union all
select null `id`, null `invoice`, `price`, `month` `date`, date_format(str_to_date(concat("01-", `month`), "%d-%m-%Y"), "%Y-%m-32") `ordering_format` from (
select sum(`price`) `price`, date_format(`date`, "%m-%Y") `month` from `facturas_table` group by `month`
) `totals`
) `a` order by `ordering_format` asc;
From there on
<-- Some Code -->
<table>
<thead>
<tr>
<th>ID</th>
<th>DATE</th>
<th>INVOICE</th>
<th>PRICE</th>
</tr>
</thead>
<tbody>
<?php while($row = $facturas->fetch_assoc()): ?>
<?php if ($row["invoice"] === null): ?>
<tr style="font-size: bolder">
<td colspan="2">Total <?php echo $row["date"]; ?></td>
<td colspan="2"><?php echo $row["price"]; ?></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>ID</th>
<th>DATE</th>
<th>INVOICE</th>
<th>PRICE</th>
</tr>
</thead>
<tbody>
<?php else: ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["date"]; ?></td>
<td><?php echo $row["invoice"]; ?></td>
<td><?php echo $row["price"]; ?></td>
</tr>
<?php endif; ?>
<?php endwhile; ?>
<-- Rest of Code -->
Note that since date is a reserved keyword in MySQL, it is wrapped in backticks (`). It is good practice to wrap any user-defined names (even if they are not reserved) in backticks to avoid confusion and unnecessary and hard-to-catch syntactic bugs.
Also Note that price is written in cents and its type is bigint. It's also good practice that:
Use bigint for integer primary keys on tables that are going to be massive in the long run and most probably getting archived in a warehouse in the future, too. That way you have plenty of time before rotating IDs, better still, use some long guaranteed unique values like UUIDs and GUIDs as primary key. In this case, if invoice numbers are going to be unique, it's best to be chosen as ID since it's the natural ID of this kind of records.
Use the smallest possible currency of the target market, this way you will avoid using decimals (which are inefficient) or worst floating point numbers (which are terrible at precision, since computers work in base 2 and humans work in base 10). Also, if you needed to divide any currency values (like calculating rebate and discounts), always use the same rounding policy on the smallest currency. For example, if you are going to give a 10% discount on an item with 9.95$, you can choose the policy to round discount up (to avoid customer dissatisfaction), but stick to it all the time, also use BCD operations since they are calculating values like humans do and not like computers do, but are expensive. Use them in sensitive error prone situations like this (not just adding simple integers, but divisions and multiplies)
$price = 995;
$discount = 10;
$discounted_value = explode(".", bcdiv(bcmul($price, $discount, 2), "100", 2), 2);
$discounted_value = $discounted_value[0] + (($discounted_value[1] + 0) !== 0);
$final_price = $price - $discounted_value;
Finally, if you think you are going to deal prices greater than PHP_INT_MAX (take care that it's different in 32-bit and 64-bit versions), just go full berserk mode and use strings in combination of bcmath operations for prices.
Having a good online business!
Hi I've managed to sort a table by column when each is clicked.
This is my table:
<table class="table table-hover">
<thead>
<tr>
<th>Game ID </th>
<th>Game title </th>
<th>Developers </th>
<th>Year of release </th>
<th>No. of items in stock </th>
<th>Cost price </th>
<th>Options</th>
</tr>
</thead>
I then use this to order the table according to the column selected.
if (isset($_GET['sort'])){
$sort = $_GET['sort'];
$query = "SELECT * FROM Games ORDER BY " . $sort . ";";
$result = mysqli_query($connection, $query);
(Underneath this code I use a loop to insert data into the table)
All works perfectly apart from my 'Year of release' column, when I click this the table empties.
I have no idea why this is happening.
Release is a reserved term. You need to encapsulate it in backticks; or change your column name. This approach you are taking also opens you to SQL injections.
I'd add make a white list of terms $_GET['sort'] could be and compare that against $_GET['sort']. Rough untested code example:
$valid_columns = array('id', 'title', 'release', 'developer', 'stock', 'price');
if (in_array($_GET['sort'], $valid_columns)) {
$sort = $_GET['sort'];
$query = "SELECT * FROM Games ORDER BY `" . $sort . "`;";
//execute query and valid stuff
} else {
echo 'invalid column supplied
}
This way you know what is going to end up in your SQL; otherwise a user could add anything which may cause errors on hang up your application.
This is again an extended quetion of THIS.
I changed my table format, i mean i have created a separate table for vendor_locations where i store multiple locations against that vendor id in multiple rows. and also created a table vendor_products to store multiple products for that vendor.
Now my problem is, As both products and locations will be multiple for each vendor while displaying i am not getting how to display it in a single row.
if the products are 3 and location is 2 then, all the details will be displayed thrice. if i remove either location or product it will work properly. But how can i do this for both
if(isset($_POST['submit']))
{
$sql="SELECT vendor.id AS venid
, vendor.name AS VNAME
, vendor.category
, vendor.website
, vendor.email
, vendor.phone
, vendor.vat
, vendor.pan
, items.name AS iname
, location.name AS locname
, items.item_id
FROM vendor
INNER JOIN vendor_location ON vendor.id = vendor_location.vendor_id
INNER JOIN vendor_products ON vendor.id=vendor_products.vendor_id
INNER JOIN location ON vendor_location.location = location.loc_id
INNER JOIN items ON vendor_products.item_id=items.item_id
ORDER BY vendor.id";
}
$sql1 = mysql_query($sql) or die(mysql_error());
}
?>
<div class="w-box w-box-blue">
<div class="w-box-header">
<h4>Vendor</h4>
</div>
<div class="w-box-content">
<table id="dt_hScroll" class="table table-striped">
<thead><tr>
<th>Vendor ID</th>
<th>Vendor</th>
<th>Category</th>
<th>Website</th>
<th>Email</th>
<th>Phone</th>
<th>VAT</th>
<th>PAN</th>
<th>Products</th>
<th>Locations</th>
</tr>
</thead>
<tbody>
<?php
$current = ''; // STORE THE SUPPLIER SID
while($row = mysql_fetch_array($sql1)) {
if ($row['venid'] != $current) {
echo "<tr>
<td><a href='edit_vendor_details.php?id=$row[venid]'>{$row['venid']}</a></td>
<td>{$row['VNAME']}</td>
<td>{$row['category']}</td>
<td>{$row['website']}</td>
<td>{$row['email']}</td>
<td>{$row['phone']}</td>
<td>{$row['vat']}</td>
<td>{$row['pan']}</td>
";
$current = $row['venid']; // RESET STORED SID
}
else {
echo "<tr><td colspan='8'> </td>";
}
echo "<td>{$row['iname']}</td>";
echo "<td>{$row['locname']}</td>";
echo "</tr>";
}
echo('</tbody></table>');
?>
</tbody></table>
use group_concat on the fields if you only wish to show. like this:
SELECT vendor.id AS venid --rest of selection
, GROUP_CONCAT(location.name) AS locname
, GROUP_CONCAT(items.item_id)
FROM vendor --rest of your joins
GROUP BY vendor.id
see more about it here: MySQL Aggregate Functions
I have a column name quantity, unit_price and total_amount. The total_amount field in my database set to zero, in my query I used SUM(unit_price*quantity) AS total_amount to get the total value as total_amount. But when I tried to get the sum of total amount as total_sum is always get 0. if is it because the total_amount field in my database is set to zero? What I want to do is get the SUM(total_amount) AS total_sum but it always displays 0.
This is my Query.
<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$term = $_GET['supp'];
$result = $mysqli->query("SELECT *, SUM(unit_cost*quantity) AS total_amount FROM procurement WHERE supplier LIKE '%".$term."%' GROUP BY counter");
echo'<table id="tfhover" cellspacing="0" class="tablesorter" style="text-transform:uppercase;" border="1px">
<thead>
<tr>
<th>SUPPLIER</th>
<th>ITEM</th>
<th>DESCRIPTION</th>
<th>QTY</th>
<th>UNIT</th>
<th>UNIT PRICE</th>
<th>Total Amount</th>
</tr>
</thead>';
echo'<tbody>';
while($row = $result->fetch_assoc()){
echo'<tr>
<td>'.$row['supplier'].'</td>
<td>'.$row['item_name'].'</td>
<td>'.$row['item_description'].'</td>
<td>'.$row['quantity'].'</td>
<td>'.$row['unit'].'</td>
<td>'.number_format($row['unit_cost'], 2, '.', ',').'</td>
<td>'.number_format($row['total_amount'], 2, '.', ',').'</td>
</tr>';
}
echo'<TR> <TD COLSPAN=6 BGCOLOR="#99CCFF">TOTAL AMOUNT</TD> <td>'.number_format($row['total_sum'], 2, '.', ',').'</td></TR>';
echo "</tbody></table>";
?>
My another problem is when I insert this code echo' TOTAL AMOUNT '.number_format($row['total_sum'], 2, '.', ',').''; inside the while loop. I want to have just 1 total amount row.
SELECT *, SUM(unit_cost*quantity) AS total_amount FROM procurement WHERE supplier LIKE '%".$term."%' GROUP BY counter"
This is a select statement which would not update any data in the database.
It seems that your total_amount, which can be evaluated every time when you need it, is not necessarily exists as a column in your table.
Also, can you post your table structure ? Actually the SQL seems not correct anyway.