Error when empty MySQL field even with conditions - php

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

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.

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

Force table to display 5 rows with or without data

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

How to create serial number

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>

How to get index/data of html table generated by foreach

I load a xml file and loop on this to generate an html table. On each line of the table, I have two icones update and delete.
When I click on the icone delete (for example), I want to get the index of the line or any information of the line in order to process delete the node in my xml file.
I try with echo $number by passing parameter to php function but the GET is empty in the php file.
Do you know how can I get it please ? Thank in advance.
<table>
<?php
foreach($participants as $participant)
{
$number = $participant->number;
$name = $participant->name;
$note = $participant->note;
$sexe = $participant->sexe;
$group = $participant->group;
$adjust = $participant->adjust;
?>
<tr>
<td align="center"><?php echo($number) ?></td>
<td align="left" style="padding-left:10px"><?php echo($name) ?></td>
<td align="center"><?php echo($note) ?></td>
<td align="center"><?php echo($sexe) ?></td>
<td align="center"><?php echo($group) ?></td>
<td></td>
<td>
<img src="images/migatiEditUser20x20.jpg">
<img src="images/migati_cancel16x16.png">
</td>
</tr>
<?php }?>
</table>
deleteParticipant.php
<?php
echo ($_GET['number']);
?>
Try this:
<table>
<?php
foreach($participants as $participant)
{
$number = $participant->number;
$name = $participant->name;
$note = $participant->note;
$sexe = $participant->sexe;
$group = $participant->group;
$adjust = $participant->adjust;
?>
<tr>
<td align="center"><?php echo $number; ?></td>
<td align="left" style="padding-left:10px"><?php echo $name; ?></td>
<td align="center"><?php echo $note; ?></td>
<td align="center"><?php echo $sexe; ?></td>
<td align="center"><?php echo $group; ?></td>
<td></td>
<td>
<img src="images/migatiEditUser20x20.jpg">
<img src="images/migati_cancel16x16.png">
</td>
</tr>
<?php }?>
</table>
and on your deleteParticipant.php use:
<?php echo $_GET['number']; ?>
You have to know how get works. Adjust the code as deleteParticipent.php?number= <?php echo $number; ?> then from that file. You will recieve it as $_GET['number']

Categories