Force table to display 5 rows with or without data - php

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 }
}

Related

Hide Row if value in checkbox greater than zero

I have a table that is using calculated fields coming from 3 different tables in 2 distinct databases. I am trying to use a checkbox to hide the rows that have the calculated field greater than zero or students who owe money. I have added a checkbox with a value of zero.
<form id="formFilter" name="formFilter" method="post" action="">
<p>
<input name="selView" type="checkbox" id="selView" onChange="formFilter.submit()"
value="0" <?php echo (isset($_POST['selView'])?'checked':'');?> />
view only students with a balance<br />
</p>
</form>
I need to evaluate $balancez and if $balancez is greater than zero, when I click the checkbox I want the rows of students who do not have a number greater than zero to hide. This way I can only view the students who have a balance owed.
<table width="800" border="0" class="TFtable sortable">
<tr>
<td scope="row"><strong>Students</strong> (<?php echo $totalRows_Recordset1 ?>)</td>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Division</strong></td>
<td align="center"><strong>AP Fee</strong></td>
<td align="center"><strong>Credit Card Payment</strong></td>
<td align="center"><strong>Store Payment</strong></td>
<td align="center"><strong>AP Balance</strong></td>
</tr>
<?php do { ?>
<tr>
<td scope="row"><?php echo $row_Recordset1['Student']; ?></td>
<td align="center"><?php echo $row_Recordset1['ID']; ?></td>
<td align="center"><?php echo $row_Recordset1['Division']; ?></td>
<td align="center"><?php echo "$".$row_Recordset1['Total']; ?></td>
<td align="center"><?php
mysql_select_db($database_cc, $cc);
$studentid = $row_Recordset1['ID'];
$query_Recordset_CC = 'SELECT credit_card_activity.Student_ID, SUM(credit_card_activity.CC_Amount) AS total_cc2, credit_card_activity.Ref_ID, credit_card_activity.Settle_Date FROM credit_card_activity WHERE credit_card_activity.Ref_ID LIKE "AP%" AND credit_card_activity.Student_ID = "'.$studentid.'" GROUP BY credit_card_activity.Student_ID';
$Recordset_CC = mysql_query($query_Recordset_CC, $cc) or die(mysql_error());
$row_Recordset_CC = mysql_fetch_assoc($Recordset_CC);
$totalRows_Recordset_CC = mysql_num_rows($Recordset_CC);
$total_cc =-1*(0+$row_Recordset_CC['total_cc2']);
echo ($total_cc < 0 ? "($".abs($total_cc).")" : "$".$total_cc);
?></td>
<td align="center"><?php
mysql_select_db($database_cc, $cc);
$studentid = $row_Recordset1['ID'];
$query_Recordset_Store = "SELECT checkout.student_id, SUM(`transaction`.total_amount) AS total_store2 FROM checkout, `transaction`, school_store WHERE `transaction`.activity_id=school_store.Tag AND `transaction`.transaction_id=checkout.transaction_id AND (`transaction`.refund_status='0' OR `transaction`.refund_status='1') AND school_store.Activity LIKE 'AP Fee%' AND checkout.student_id='".$studentid."' AND checkout.payment_type NOT IN ('Cash Refund') GROUP BY checkout.student_id";
$Recordset_Store = mysql_query($query_Recordset_Store, $cc) or die(mysql_error());
$row_Recordset_Store = mysql_fetch_assoc($Recordset_Store);
$totalRows_Recordset_Store = mysql_num_rows($Recordset_Store);
$total_store = -1*(0+$row_Recordset_Store['total_store2']);
echo ($total_store < 0 ? "($".abs($total_store).")" : "$".$total_store);
?></td>
<td align="center"><?php
$ap_fees=$row_Recordset1['Total'];
$ap_cc=$row_Recordset_CC['total_cc2'];
$ap_store=$row_Recordset_Store['total_store2'];
$paid=$ap_cc + $ap_store;
$balancez=($ap_fees - $paid);
echo ($balancez < 0 ? "($".$balancez.")" : "$".$balancez);
?>
</td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
?>
</table>
What makes this task difficult for me is that it is not based on a mysql query but on a calculated value ($balancez) based on other queries so I can't use Having. Can someone lead me to a jquery that can evaluate $balancez of each row and hide or display the row if value from checkbox is met? I am also open to any method that will solve this issue. Many thanks.

Change only one value of an array returned by sql

I have a payment schedule table where I pull data from the database. The query result fetches four records because there are four payment plans in my table. The code below works fine. The only change I need is here
<td align="left" style="padding-left:5px;">
<?php echo $rr['plan_duration']?>
</td>
I am struggling to put an IF condition for the echo statement I want to see the contents of the array first and then decide what to echo. If the value of $rr['plan_duration'] is 1490 then echo 149 else echo the actual value of $rr['plan_duration'] I am facing issues with mixing html with php as far as the syntax is concerned. Please help me implement this condition. Thanks.
Here is the full working code:
<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");
while($rr=mysql_fetch_array($result))
{
?>
<tr height="30px">
<td align="left" style="padding-left:5px;" class="red_text">
<?php echo $rr['plan_name']?>
</td>
<td align="left" style="padding-left:5px;">
<?php echo $rr['plan_contacts']?>
</td>
<td align="left" style="padding-left:5px;">
Unlimited
</td>
<td align="left" style="padding-left:5px;">
<?php echo $rr['video']?>
</td>
<td align="left" style="padding-left:5px;">
<?php echo $rr['plan_duration']?>
</td>
<td align="left" style="padding-left:5px;">
Rs.
<?php echo $rr['plan_amount']?>
</td>
<td align="left" style="padding-left:5px;">
<a href="pay.php?plan=<?php echo $rr['plan_name']?>">Pay Now
</a>
</td>
</tr>
PS: I understand the limitation and disadvantages of mysql and I am going to covert it to mysqli
You can insert an entire PHP block inside each td element. Create a function that does the converting from 1490 to 149, let's call it convert() in this example
<td align="left" style="padding-left:5px;">
<?php
if($rr['plan_duration'] == 1490)
{
echo convert($rr['plan_duration'])
}
else
{
echo $rr['plan_duration'];
}
?>
</td>
You can also use the ? conditional to reduce the amount of code:
<td align="left" style="padding-left:5px;">
<?php echo ($rr['plan_duration'] == 1490) ? convert($rr['plan_duration']) : $rr['plan_duration'];
</td>
Note: Besides using mysqli instead of mysql I strongly advice you to use Prepared Statements too
Inside your while loop you can just use an if statement.
<td align="left" style="padding-left:5px;">
<?php if ($rr['plan_duration'] == 1490) {
echo 149 ;
} else {
echo $rr['plan_duration'];
} ?>
</td>
I rewrote your code a bit to make it a bit better to read. I added a shorthand if statement. Take a look:
<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");
$results = array();
while($record = mysql_fetch_array($result)) {
$results[] = $record;
}
?>
<?php foreach ($results as $rr): ?>
<tr height="30px">
<td align="left" style="padding-left:5px;" class="red_text"><?= $rr['plan_name']; ?></td>
<td align="left" style="padding-left:5px;"><?= $rr['plan_contacts']; ?></td>
<td align="left" style="padding-left:5px;">Unlimited</td>
<td align="left" style="padding-left:5px;"><?= $rr['video']; ?></td>
<td align="left" style="padding-left:5px;"><?= ($rr['plan_duration'] == '1490') ? '149' : $rr['plan_duration']; ?></td>
<td align="left" style="padding-left:5px;">Rs. <?= $rr['plan_amount']; ?></td>
<td align="left" style="padding-left:5px;">Pay Now</td>
</tr>
<?php endforeach; ?>
<?php
$result = mysql_query("SELECT * from memship_plan where status='1' order by plan_amount DESC");
while($rr=mysql_fetch_array($result)) {
?>
<td align="left" style="padding-left:5px;">
<?php if($rr['plan_duration']=='1490') {
echo "149";
} else {
echo $rr['plan_duration'];
}
?>
</td>
<?php } ?>

Error when empty MySQL field even with conditions

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

Displaying the sum just in one row

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";

How to show records from database in html table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm selecting records from database and showing it as
<table>
<tr>
<td align="center" >Name</td>
<td align="center" >Contact</td>
</tr>
<?php
foreach($rowset as $row)
{
?>
<tr>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
</tr>
<?php
}
?>
</table>
It is displayed as
| Name | Contact |
a 9956874525
b 9856426569
c 9865989657
d 8956789565
e 8956879899
f 8985656465
Here each record is shown in separate <tr>
But now i've to show the records in such a way that ,two records are shown in a single <tr>
So my design should be
| Name | Contact | | Name | Contact |
a 9956874525 b 9856426569
c 9865989657 d 8956789565
e 8956879899 f 8985656465
What changes should i do in above code? Please help
You need create an counter to count how many times you have printed the data.
Maybe like this (not tested yet):
<?php
$i = 0;
foreach($rowset as $row)
{
if($i % 2 == 0) echo "<tr>";
?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php
if($i % 2 == 0) echo "</tr>"; $i++;
}
?>
modify the code like this
<table>
<tr>
<td align="center" >Name</td>
<td align="center" >Contact</td>
</tr>
<?php
$i=1;
foreach($rowset as $row)
{
if($i%2==1) {
?>
<tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php if($i%2==0) { ?>
</tr>
<?php } ?>
<?php
$i++; }
?>
You can apply the loop conditionally on the basis of indexes.
<table>
<tr>
<td align="center">Name</td>
<td align="center">Contact</td>
<td align="center">Name</td>
<td align="center">Contact</td>
</tr>
<tr>
<?php
foreach($rowset as $i=>$row) {
if($i!=0 && $i%2 == 0) {
?>
</tr><tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php } ?>
</tr>
</table>
Try using a for loop instead
$num_contacts = sizeof($rowset);
for($i = 0; $i < $num_contacts; $i++)
{
if($i%2 == 0)
echo "<tr>";
?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php
if($i%2 == 0)
echo "</tr>";
}?>
Its the appropriate way..
<table>
<tr><td></td><td></td><td></td><td></td><td></td></tr>
<tr>
<?php
foreach($rowset as $i=>$row) {
if($i!=0 && $i%2 == 0) {
?>
</tr><tr>
<?php } ?>
<td align="left"><?php echo $row['name'];?></td>
<td align="left"><?php echo $row['contact'];?></td>
<?php } ?>
</tr>
</table>
First connect to database using: $con = mysql_connect(host,username,password);
Select Database : mysql_select_db(dbname,$con);
Write a query: $qselect = mysql_query("select * from mytable");
Now Let's start displaying in a HTML Table:
<table>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<?php
while($result = mysql_fetch_array($qselect))
{
extract($result);
?>
<tr>
<td><?= $name?></td>
<td><?= $addr?></td>
</tr>
<?php
}
?>
</table>
Note: $name and $addr are field names from mysql table.

Categories