Hide Row if value in checkbox greater than zero - php

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.

Related

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

Displaying table from MySQL and link to delete each row from database

I've stucked a bit when it comes to a small piece of my PHP code. The role of this script is to display whole table from mysql plus adding to each row a hyperlink to delete such row.
<?php
$connection=mysql_connect('localhost','root','') or die(mysql_error());
error
mysql_select_db('localhost_db',$connection) or die(mysql_error());
$query=mysql_query("SELECT * FROM cms_contest") or die(mysql_error());
if(mysql_num_rows($query)>0):
?>
<table width="100%" border="0">
<tr style="font-weight:bold;">
<td align="center">Id</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Email</td>
<td align="center">Phone</td>
<td align="center">Answer</td>
<td align="center">Remove</td>
</tr>
<?php
while($row=mysql_fetch_object($query)):?>
<tr>
<td align="center"><?php echo $row->ID; //row id ?></td>
<td align="center"><?php echo $row->name; // row first name ?></td>
<td align="center"><?php echo $row->surname; //row las tname ?></td>
<td align="center"><?php echo $row->email; //row created time ?></td>
<td align="center"><?php echo $row->phone_number; //row created time ?></td>
<td align="center"><?php echo $row->contest_answer; //row created time ?></td>
<td align="center">REMOVE</td>
</tr>
<?php endwhile;?>
</table>
<?php
else: ?>
<h3>No Results found.</h3>
<?php endif; ?>
It would be perfect if there appear a short javascript popup with question like: are you sure you want to remove this entry? OK / Cancel.
I have no clue how to do it.. thanks for any tips!
Simple/stupid method:
<td>nuke me</td>
But if a web spider or a browser link pre-fetch tool gets loose on this page, you'll be nuking ALL of your records.
Somewhat better:
<td>
<form method="post" action="deleteme.php">
<input type="hidden" name="id" value="<?php echo $row->ID ?>" />
<input type="submit" value="Nuke me" />
</form></td>
And then there's various options involving radio buttons/checkboxes, or JS to trap the click-on-the-link etc...
But, in the end, they ALL boil down to "you have to sent the ID of the row back to the server". How you go about that is up to you... just don't use the plain "click here" version.
Indeed there are several different ways to do this. One way I like is to wrap the entire table in a form that submits to the delete script, and use a button for each row with the row ID as its value.
<form method="post" action="delete.php">
<table width="100%" border="0">
<tr style="font-weight:bold;">
<td align="center">Id</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Email</td>
<td align="center">Phone</td>
<td align="center">Answer</td>
<td align="center">Remove</td>
</tr>
<?php while($row=mysql_fetch_object($query)): ?>
<tr>
<td align="center"><?php echo $row->ID; //row id ?></td>
<td align="center"><?php echo $row->name; // row first name ?></td>
<td align="center"><?php echo $row->surname; //row las tname ?></td>
<td align="center"><?php echo $row->email; //row created time ?></td>
<td align="center"><?php echo $row->phone_number; //row created time ?></td>
<td align="center"><?php echo $row->contest_answer; //row created time ?></td>
<td align="center">
<button name="delete-id" type="submit" value="<?php echo $row->ID; ?>">
REMOVE
</button>
</td>
</tr>
<?php endwhile;?>
</table>
</form>
There are also many different ways to confirm the deletion. The simplest I know of on the client side is to add onclick="return confirm('Are you sure?');" to the delete button.
click event:
DELETE
<script type="text(javascript">
function confirmDelete(id){
if(confirm('sure u want to delete entry with id: ' + id + '?')){
window.location.href = "ursite.php?id="+id+"&delete=true";
}
}
</script>
PHP
if(isset($_GET['delete'])){
$stmt = "DELETE FROM cms_contest WHERE ID = ".$_GET['id'];
mysql_query($stmt);
}
it's the worst way to do it.
I recommend you to look at:
jQuery $.post()jQuery $.ajax()
I hope it will help you.

MySQL query - IF statement to generate two different rows

I have some issues with below script, essentially what I'm trying to achieve is to grab different product and prices and generate a table which works fine. However, some of the products do have an extra charge, in this case the product will use three rows (price, extra charge and total sum). I'm trying to get the IF statement to work as follows: if the extra charge = 0 then it should only make a single row in the table, if more then 0 it should produce the 3 row version.
Someone have any idea what I'm doing wrong? Thanks in advance!
<?php
$page=basename($_SERVER['PHP_SELF']); /* Returns PHP File Name */
$page_name=str_replace(".php","",$page);
mysql_connect(localhost,$dbuser,$dbpass);
#mysql_select_db($database) or die( "Unable to select database");
$query= ("SELECT * FROM table e
JOIN validdate1 r ON e.datevalid1=r.id
JOIN validdate2 d ON e.datevalid2=d.id
WHERE productpage='$page_name'
ORDER BY productname,price2");
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();?>
<table>
<thead>
<tr>
<th class="headerdesc">Description</th>
<th class="headerprice1 rates1">2015</th>
<th class="headerprice2 rates2">2016</th>
</tr>
</thead>
<?php $i=0;while ($i < $num)
{
$productname = mysql_result($result,$i,"productname");
$price1=mysql_result($result,$i,"price1");
$price2=mysql_result($result,$i,"price2");
$extracharge1=mysql_result($result,$i,"extracharge1");
$extracharge2=mysql_result($result,$i,"extracharge2");
$daterange1=mysql_result($result,$i,"daterange1");
$daterange2=mysql_result($result,$i,"daterange2");
if ($extracharge1 > "0") {
echo " <tr>
<td class="desc"><?php echo $productname; ?></td>
<td class="price1 rates1">$<? echo $price1; ?></td>
<td class="price2 rates2">$<? echo $price2; ?></td>
</tr>
<tr>
<td class="extra">Extra Charge**</td>
<td class="price1 rates1">$<? echo $extracharge1; ?></td>
<td class="price2 rates2">$<? echo $extracharge2; ?></td>
</tr>
<tr class="lastrow">
<td class="totalprice"><strong>Total price</strong></td>
<td class="total rates1"><strong>$<? echo $price1+$extracharge1; ?></strong></td>
<td class="total rates2"><strong>$<? echo $price2+$extracharge2; ?></strong></td>
</tr>";
} else {
echo " <tr class="lastrow">
<td class="extra"><?php echo $productname; ?></td>
<td class="price1 rates1">$<? echo $price1; ?></td>
<td class="price2 rates2">$<? echo $price2; ?></td>
</tr>";
}
?>
<?php $i++;}?>
</table>
You have several error in code, change it like this, on the top below query
$num=mysql_numrows($result); will be
$num=mysql_num_rows($result);
don't close the connection since you are still performing queries below
//mysql_close(); comment it out and move it to bottom
and here you need this
if (mysql_num_rows($extracharge1) > 0 )
You are comparing string with a resource in your code
note: Don't use mysql_* functions its deprecated, use PDO or mysqli_*

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

Categories