I hav this sql query:
$sql = "SELECT (SELECT SUM(total_pay) FROM workers) total,workers. * FROM workers WHERE projects_id = ".$id;
And I want to display data into table rows:
$stmt = mysqli_query($con, $sql) or die($sql."<br/><br/>".mysqli_errno($con));
<?php while($rows = mysqli_fetch_array($stmt)){ ?>
<form action="update_del.php" method="post">
<th>Payments</th>
<th>Date</th>
<th>Project</th>
<th width="25%">Delete</th>
<tr>
<?php while($rows = mysqli_fetch_array($stmt)){ ?>
<tr>
<td align="center"><?php echo $rows['total_pay']?></td>
<td align="center"><?php echo $rows['date_of_pay']?></td>
<td align="center"><?php echo $name['project_name'] ?></td>
<td align="center"><!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
</tr>
<tr>
<td colspan="3">Total <?php echo $name['project_name']?></td>
<td><?php echo $rows['total'] ?></td>
</tr>
<?php } ?>
</table>
The problem is, that the sum of the payment is repeated with every row displayed, so how can I display the sum query just once in the end of the table ? Should put it in a single query specified for the sum only ?
You can either remove the total from your query and use PHP to calculate, or you can just simply store the total and use it outside your while statement. Something like
<?php
$total = 0;
while($rows = mysqli_fetch_array($stmt)){
$total = $rows['total'];
?>
<tr>
<td align="center"><?php echo $rows['total_pay']; ?></td>
<td align="center"><?php echo $rows['date_of_pay']; ?></td>
<td align="center"><?php echo $name['project_name']; ?></td>
<td align="center">
<!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="3">Total <?php echo $name['project_name']; ?></td>
<td><?php echo $total; ?></td>
</tr>
</table>
Though since you're traversing all the results anyway it would be best to remove it from the SQL and calculate it.
$sql = "SELECT workers.* FROM workers WHERE projects_id = ".$id;
<?php
$total = 0;
while($rows = mysqli_fetch_array($stmt)){
$total += $rows['total_pay'];
?>
<tr>
<td align="center"><?php echo $rows['total_pay']; ?></td>
<td align="center"><?php echo $rows['date_of_pay']; ?></td>
<td align="center"><?php echo $name['project_name']; ?></td>
<td align="center">
<!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="3">Total <?php echo $name['project_name']; ?></td>
<td><?php echo $total; ?></td>
</tr>
If i understand your problem properly, then this might help you to generate sum in the last rows as you require.
"SELECT total_pay, date_of_pay,project_name
FROM workers WHERE project_id=".$id."
UNION
SELECT 'ALL', SUM(total_pay)
FROM workers";
Related
I have 17 records inserted in my table "book".
<?php
include 'config.php';
session_start();
$sql = "SELECT * from book";
$result = mysqli_query($db, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
$error = "There is no books in the list";
}
?>
<html>
<body>
<table border="2" style="width:80%;" align="center">
<tr>
<th>ISBN</th>
<th>Title of the book</th>
<th>Cost</th>
<th>Copies</th>
<th>Edition</th>
<th>Publisher</th>
<th>Copy Year</th>
<th>Shelf Number</th>
<th>Subject</th>
</tr>
<?php while($row = mysqli_fetch_array($result)){
$ISBN = $row['ISBN'];
$Title = $row['Title'];
$Cost = $row['Cost'];
$Copies = $row['Copies'];
$Edition = $row['Edition'];
$Publisher = $row['Publisher'];
$CopyYr = $row['CopyYr'];
$Shelf = $row['ShelfNo'];
$Subject = $row['SubName'];
?>
<tr>
<td><?php echo $ISBN; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $Cost; ?></td>
<td><?php echo $Copies; ?></td>
<td><?php echo $Edition; ?></td>
<td><?php echo $Publisher; ?></td>
<td><?php echo $CopyYr; ?></td>
<td><?php echo $Shelf; ?></td>
<td><?php echo $Subject; ?></td>
</tr>
<?php
}
?>
</table>
<br>
<table align="center" style="width: 30%">
<tr>
<th><form action="AddBook.php" method="post">
<input type="submit" value="Add Book"/>
</form></th>
<th><form action="editbook.php" method="post">
<input type="submit" value="Edit List"/>
</form></th>
<th><form action="deletebook.php" method="post">
<input type="submit" value="Delete Book/s"/>
</form></th>
<th><form action="AdminSummary.php" method="post">
<input type="submit" value="Back"/>
</form></th>
</tr>
</table>
</center>
</body>
</html>
This will display 17 records in the table. But when I add another record to the table, It will display an incomplete record, a "500 server error", or an empty table.
I have been in the same problem for days, searching for a solution, and I can't find any.
I don't know what the problem is, so any help would be appreciated. Thank You :)
I have this PDO that fetch rows into html table:
<?php
$selectAll = "SELECT * FROM sales WHERE date_now = :date";
$stmtAll=$conn->prepare($selectAll);
$stmtAll->bindValue(':date', date("y-m-d"));
$execAll=$stmtAll->execute();
$result=$stmtAll->fetchAll();
?>
<?php foreach($result as $rows){ ?>
<tr>
<td height="30" align="center"><?php $rows['type'] ?></td>
<td align="center"><?php $rows['provider'] ?></td>
<td align="center"><?php $rows['pay'] ?></td>
<td align="center"><?php $rows['facture'] ?></td>
<td align="center"><?php $rows['type'] ?></td>
<td align="center"><form action='/architect/pages/delete_row.php' method="post">
<input type="hidden" name="rowid" value="" />
<input type="hidden" name="projid" value="" />
<input class="imgClass_dell" type="submit" onclick="return confirm('هل أنت متأكد؟')" name="delete_workers" value="" />
</form></td>
</tr>
<?php } ?>
</table>
This query should select all rows with current date. I have gotten a table with the correct number of rows but with no data (empty table):
You missed the echo before each values.
You could either use:
<td align="center"><?php echo $rows['provider']; ?></td>
Or if you are using PHP 5.4 or later:
<td align="center"><?= $rows['provider'] ?></td>
You forget to echo/print the values.
Replace
<td height="30" align="center"><?php $rows['type'] ?></td>
<td align="center"><?php $rows['provider'] ?></td>
<td align="center"><?php $rows['pay'] ?></td>
<td align="center"><?php $rows['facture'] ?></td>
<td align="center"><?php $rows['type'] ?></td>
with
<td height="30" align="center"><?=$rows['type'] ?></td>
<td align="center"><?=$rows['provider'] ?></td>
<td align="center"><?=$rows['pay'] ?></td>
<td align="center"><?=$rows['facture'] ?></td>
<td align="center"><?=$rows['type'] ?></td>
Note: Short Tags must b enable for above solution. Or alternatively use can replace<?php $rows with <?php echo $rows
When I click on a project in my web app, that it contains values, I do not get any error, but I created another project with empty fields in MySQL, and I clicked on it, in my web app (PHP app), so I got this error:
Undefined variable: total in
C:\wamp\www\architect\projDetails.php on line 80
I have this PHP code:
$id = $_REQUEST['id'];
$sql = "SELECT (SELECT SUM(total_pay) FROM workers) total,workers. * FROM workers WHERE projects_id = ".$id." ORDER BY date_of_pay DESC";
$stmt = mysqli_query($con, $sql) or die($sql."<br/><br/>".mysqli_error($con));
And here html and php code near line 80:
<tr>
<?php while($rows = mysqli_fetch_array($stmt)){ $total = 0; ?>
<tr>
<?php if($rows['total']!=0){
$total = $rows['total'];
}
else {
$total = "غير متوفر";
}
?>
<td align="center"><?php echo $rows['total_pay']?></td>
<td align="center"><?php echo $rows['date_of_pay']?></td>
<td align="center"><?php echo $name['project_name'] ?></td>
<td align="center"><!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
</tr>
<?php } ?>
<tr>
<td colspan="3">مجموع تكاليف العمال في مشروع <?php echo $name['project_name']?></td>
<td align="center"><?php echo $total ?></td>
</tr>
So when total is empty, I get the error,and when it is not empty, I don't get any errors, so what is the problem here?
Your variable $total is currently only alive within the while block. You are exiting this block at the statement </tr><?php } ?><tr>. That means your variable is no longer allocated in code row <td align="center"><?php echo $total ?></td> below. To use this total variable below the while block, add the declaration and initialization of the variable above the while block, e.g:
<tr>
<?php $total = 0;
while($rows = mysqli_fetch_array($stmt)){ ?>
<tr>
<?php if($rows['total']!=0){ <-- Check this comparison (described below)
$total = $rows['total'];
}
else {
$total = "غير متوفر";
}
?>
<td align="center"><?php echo $rows['total_pay']?></td>
<td align="center"><?php echo $rows['date_of_pay']?></td>
<td align="center"><?php echo $name['project_name'] ?></td>
<td align="center"><!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
</tr>
<?php } ?>
<tr>
<td colspan="3">مجموع تكاليف العمال في مشروع <?php echo $name['project_name']?></td>
<td align="center"><?php echo $total ?></td>
</tr>
Hope this will work for you. The annotated line in code above will compare a string with an integer value. Please be aware that $rows['total'] will contain a value of type String. Your comparison is against an integer value of 0. PHP should not throw an error or warning but it could come to unwanted results in this if statement. (Further information: http://php.net/manual/de/language.types.type-juggling.php )
Try something like below.
echo isset($total)? $total: 0;
Here if we are getting any data from DB, then only it will enter into the while loop. Now inside while loop only $total is defining.
If we define it outside the while loop, the default value, will solve the issue
<tr> <?php
$total = "0";
while($rows = mysqli_fetch_array($stmt)){ $total = 0;
if($rows['total']!=0){
$total = $rows['total'];
}
//.....
//.....
} ?>
</tr>
<tr>
<td colspan="3">some text <?php echo $name['project_name']?></td>
<td align="center"><?php echo $total ?></td>
</tr>
Thank you
I have to display a table of 5 rows fetching from MySQL database using PHP. From the code below, if I have 3 rows in database, it will display only 3 rows. But, I need to display 5 rows with 2 empty rows along with 3 fetched rows. If there are no records found in the database, I should display 5 empty rows. I need your help on that.
Note: I am generating a report using PHP and MySQL. From the above method, I can fix the table height and so report will generate without any overlaps.
CODE:
<?php
$select= "select * from table where id=1";
$select2= mysql_query($select);
$select3= mysql_num_rows($select2);
$row_count = 1;
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr>
<?php $row_count++;
}?>
As you are already counting the $row_count variable, you can add simple while loop, like this:
<?
while($row_count < 5){
?>
<tr>
<td > </td>
<td > </td>
<td > </td>
<td > </td>
</tr>
<?php $row_count++;
}?>
What is ? Is it needed?
Also check the #paxdiablo's answer about limit 5 option for your query.
This should do the trick.
<?php
$select= "select * from table where id=1";
$select2= mysql_query($select);
$select3= mysql_num_rows($select2);
$row_count = 1;
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr>
<?php $row_count++;
}
if ($row_count < 5){
for ($i=1; $i <= (5-$row_count); $i++){
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<?php
}
}
?>
You appear to have two distinct problems (albeit related).
If you only want five rows even if your query returns twenty, you can either use the control variable $row_count to only output rows for the first five, or (preferably) just add limit 5 on to your query to get five rows or less.
The second problem is what to do if it returns less than five rows. In that case, use the control variable to output blank rows, something like adding the following to the end:
<?php
while ($row_count < 5) {
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<?php
$row_count++;
}
?>
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr> <?php }
if($select3<5){
for($i=1;$i<5-$select3;$i++){?>
<tr>
<td ></td>
<td ></td>
<td ></td>
<td ></td>
</tr>
<?php }
}
form.php
<div> Leaving From
<input type="text" class="from" name="source_point" id="source_point" title="Departure Place" required/>
</div>
<div class="right"><b>Going To
<input type="text" class="to" name="destination" id="destination" title="Arrival Place" required />
</div>
search.php
<table>
<tr>
<td>Sl No.</td>
<td>Bus Operator</td>
<td>Bus No. </td>
</tr>
<?php
$s=mysql_query("SELECT * FROM bus_detail where source_point='$_SESSION[source_point]' && destination_point='$_SESSION[destination]'");
while($row=mysql_fetch_array($s))
{
?>
<tr>
<td> </td>
<td><?php echo $row['bus_name'];?> </td>
<td><?php echo $row['bus_no'];?></td>
</tr>
<?php }?>
</table>
I want to display serial numbers from 1 according to the search results, in the empty <td> field, not any random number.. It should be serial numbers in proper format ...
It is as simple as adding a counter variable.
<?php
$s=mysql_query("SELECT * FROM bus_detail where source_point='$_SESSION[source_point]' && destination_point='$_SESSION[destination]'");
$counter = 0;
while($row=mysql_fetch_array($s))
{
?>
<tr>
<td><?php echo ++$counter; ?></td>
<td><?php echo $row['bus_name'];?> </td>
<td><?php echo $row['bus_no'];?></td>
</tr>
<?php }?>
You need to go through the basics if you couldn't figure this out.
You can add incremental value inside the loop
In your search.php, include
$i=1;
while($row=mysql_fetch_array($s))
{
?>
<tr>
<td><?php echo $i </td>
<td><?php echo $row['bus_name'];?> </td>
<td><?php echo $row['bus_no'];?></td>
</tr>
<?php $i++ }?>
</table>
user a count variable as initialize with 0 and increment it by 1 in each row display and you can print the serial no as this way.
<table>
<tr>
<td>Sl No.</td>
<td>Bus Operator</td>
<td>Bus No. </td>
</tr>
<?php
$count = 0;
$s=mysql_query("SELECT * FROM bus_detail where source_point='$_SESSION[source_point]' && destination_point='$_SESSION[destination]'");
while($row=mysql_fetch_array($s))
{
$count+=1;
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $row['bus_name'];?> </td>
<td><?php echo $row['bus_no'];?></td>
</tr>
<?php }?>
</table>