Getting the option value without using submit button - php

Good day. Im trying to have some functionality on my project, but im stuck with this.
I want to display my database depending on what the user select
Here is my html code:
<table>
<tr><td>Select Request Status:</td>
<td><select class="form-control" name="status" style="width:200px;">
<option value="1">Pending Request</option>
<option value ="2">Requests On-Process</option>
<option value="3">Unassigned Requests</option>
<option>All Requests</option>
</select></td>
</tr>
</table>
And here is my php code:
if ($status = $_SESSION['status'] == 'Pending Request')
{
$query = "select * from tblrequest where status='Pending Request'";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table">
<tr>
<th>Request ID</th>
<th>Requestor</th>
<th>Department</th>
<th>Category</th>
<th>Assigned to</th>
<th>Status</th>
<th>Created Date</th>
<th>Created Time</th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody class="table table-hover">
<tr>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
}?>
Im a newbie to php so please help me.

Can you try this,
HTML:
<form method="post">
<table>
<tr><td>Select Request Status:</td>
<td>
<?php $Statuses = array("0"=>"All Requests", "1"=>"Pending Request", "2"=>"Requests On-Process", "3"=>"Unassigned Requests" );?>
<select class="form-control" name="status" style="width:200px;" onchange="this.form.submit();">
<?php foreach($Statuses as $key=>$status):?>
<?php
$selected ="";
$statusPost = $_POST['status'];
if($key == $statusPost): $selected =" selected"; endif;
?>
<option value="<?php echo $key;?>" <?php echo $selected;?>><?php echo $status;?></option>
<?php endforeach;?>
</select></td>
</tr>
</table>
</form>
PHP:
if (isset($_POST['status'])){
$status= $_POST['status'];
$Where ='';
if($status!='0'){
$Where =" where status='".$Statuses[$status]."' ;
}
}
$query = "select * from tblrequest $Where";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table">
<tr>
<th>Request ID</th>
<th>Requestor</th>
<th>Department</th>
<th>Category</th>
<th>Assigned to</th>
<th>Status</th>
<th>Created Date</th>
<th>Created Time</th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody class="table table-hover">
<tr>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
?>

put select dropdown in a form with id,action,method etc.
give id to a select element(say "mySelect")
Now, in jquery,
$("#mySelect").on("change",function(){
if($("#mySelect").val() != ""))
{
$("#form1").submit();
}
})

Related

instant delete when the button is cliked

im trying to delete from a datatable when i click the remove button the data will be removed for a quick action in the admin side but my code dose not work i tried to fix it but i don't see any problem in the code here is my code
<div class="table-responsive">
<table id="datas" class="table table-striped table-bordered fixed" style="width:100%">
<thead style="color:black;" >
<th>id</th>
<th>Company Name</th>
<th>Product Name</th>
<th>Weight</th>
<th>Price Per Gram</th>
<th>Quantity</th>
<th>Type</th>
<th>Category</th>
<th>Product Price</th>
<th>Image</th>
<th>Total Price</th>
<th class="text-center">Actions</th>
</thead>
<?php
$get = mysqli_query($conn,"SELECT * FROM stock;");
?>
<tbody>
<?php
while ($row=mysqli_fetch_array($get)) {
$id=$row['id'];
$company=$row['company_name'];
$name=$row['product_name'];
$weight=$row['weight'];
$price_per_gram=$row['price_per_gram'];
$quantity=$row['quantity'];
$type=$row['type'];
$category=$row['category'];
$price=$row['product_price'];
$img=$row['img'];
$total=$row['total_price'];
?>
<tr>
<td ><?php echo $id;?></td>
<td><?php echo $company;?></td>
<td><?php echo $name;?></td>
<td><?php echo $weight;?> g</td>
<td><?php echo $price_per_gram;?> $</td>
<td><?php echo $quantity;?></td>
<td><?php echo $type;?></td>
<td><?php echo $category;?></td>
<td><?php echo $price;?></td>
<td>
<img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
</td>
<td><?php echo $total;?></td>
<td style="width: 20px"> Edit
<button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
if (isset($_POST['delete'])) {
$delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." ");
header("location:viewstcok.php");
} ?>
i give the delete button a value when clicked so it will delete with that value but it did not seems to work can any one help me
you have to placed table inside <form></form> tag
<div class="table-responsive">
<form>
<table id="datas" class="table table-striped table-bordered fixed" style="width:100%">
<thead style="color:black;" >
<th>id</th>
<th>Company Name</th>
<th>Product Name</th>
<th>Weight</th>
<th>Price Per Gram</th>
<th>Quantity</th>
<th>Type</th>
<th>Category</th>
<th>Product Price</th>
<th>Image</th>
<th>Total Price</th>
<th class="text-center">Actions</th>
</thead>
<?php
$get = mysqli_query($conn,"SELECT * FROM stock;");
?>
<tbody>
<?php
while ($row=mysqli_fetch_array($get)) {
$id=$row['id'];
$company=$row['company_name'];
$name=$row['product_name'];
$weight=$row['weight'];
$price_per_gram=$row['price_per_gram'];
$quantity=$row['quantity'];
$type=$row['type'];
$category=$row['category'];
$price=$row['product_price'];
$img=$row['img'];
$total=$row['total_price'];
?>
<tr>
<td ><?php echo $id;?></td>
<td><?php echo $company;?></td>
<td><?php echo $name;?></td>
<td><?php echo $weight;?> g</td>
<td><?php echo $price_per_gram;?> $</td>
<td><?php echo $quantity;?></td>
<td><?php echo $type;?></td>
<td><?php echo $category;?></td>
<td><?php echo $price;?></td>
<td>
<img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
</td>
<td><?php echo $total;?></td>
<td style="width: 20px"> Edit
<button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." ");
header("location:viewstcok.php");
} ?>

Table inside nested Table

I am having a nested table with a while loop, I want to add one more nested table in the same row:
Now I want to add one more nested table as each cd contains more than one data like below:
My code is as follows
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td><?php echo $rowcd['well_no'] ?></td>
<td><?php echo $rowcd['well_name'] ?></td>
<td>
<table border="1" width="100%">
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
/* I want to add one more nested table here*/
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
I tried some thing like this,after my second while loop
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
<td>
<table>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<tr>
<td><?php echo $rowl['logs'] ?></td>
</tr>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
but the result was messed up. I am confused, where I want to end my while loop, I think.
Finally i got as i wish, and i am sharing the code as below
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div class="container">
<table border="1" align="center" border-collapse="collapse">
<thead>
<tr >
<th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th width="100">CD No:</th>
<th width="150">Logs</th>
<th width="100">Bottom Depth</th>
<th width="100">Top Depth</th>
<th width="100">Date of Log</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td align="center"><?php echo $rowcd['well_no'] ?></td>
<td align="center"><?php echo $rowcd['well_name'] ?></td>
<td colspan="5">
<table rules="all">
<tr>
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<td width="100" align="center"><?php echo $rowcd['cd_no'] ?></td>
<td colspan="4">
<table rules="all">
<tr>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<td width="155"><?php echo $rowl['logs'] ?></td>
<td width="105" align="center"><?php echo $rowl['bottom'] ?></td>
<td width="100" align="center"><?php echo $rowl['top'] ?></td>
<td width="100" align="right"><?php echo $rowl['date'] ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>
</td>
<?php
}
}
?>
</tr>
</table>
I hope this is what you meant as per your data table shown above
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>well</td>
<td>name</td>
<td>
<table border="1" width="100%">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
</table>
</td>
<td>
<table border="1" width="100%">
<tr>
<td>Log1</td>
</tr>
<tr>
<td>Log2</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

Put the "$message" inside the table in codeigniter

I have a problem i want to put the $message inside the table that is a equivalent as a "Search not found".
Here is my picture outside the table $message = Search not found
View:
<div class="z table-responsive" >
<table class=" table table-hover" >
<thead >
<tr >
<th>ID Number</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
<th>Sex</th>
</tr>
</thead>
<?php if ( isset($message) ){
echo $message;
} else{ foreach($results as $row){
?>
<tbody>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
Option
</td>
</tr>
</tbody>
<?php }} ?>
</tbody>
</table>
</div>
You are using wrong end tags.
This will print Search not found inside the table with colspan 5 and center aligned
Note: Most of time we check where foreach value is empty ($results). But in your case you are checking some thing else ($message).
Changes
<tr> <!-- Changed -->
<td colspan="5" align="center"><?php echo $message; ?> </td>
</tr>
if ( isset($message) ){ is changed to if (!empty($message)){
Final code
<div class="z table-responsive" >
<table class=" table table-hover" >
<thead >
<tr >
<th>ID Number</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
<th>Sex</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($message)) # improved
{
?>
<tr> # Changed
<td colspan="5" align="center"><?php echo $message; ?> </td>
</tr>
<?php
}
else
{
foreach($results as $row)
{
?>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
<a href="<?php echo site_url('viewstudentinalpha/viewspecific/'.$row->Id) ?>" class="btn btn-info " style="font-size: 18px;" type="submit" name="submit" role="button">
Option
</a>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
Edit 01
In Controller
function search_keyword()
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$keyword = $this->input->post('keyword');
$data['results'] = $this->model_adminlogin->search($keyword);
$this->load->view('result_view',$data);
}
In View
Alter your if like this.
Copy all codes i have added in above for your view. and only change this few lines.
<?php
if (!empty($results)) # Change
{
?>
<tr> # Change
<td colspan="5" align="center"> Search not found </td> # Change
</tr>
<?php
}
else
{
This isn't an issue with CodeIgniter so much as your code. What you are trying to do with the code is to put the message between the <thead> and <tbody>, and not in any table tags.
This is the same as trying to put it between rows in a table, and therefore gets interpreted as not being part of the table, and drawn outside of the table as a result.
If you put the message into the <tbody>, it will go in the right place, so you can do:
<?php if ( isset($message) ){
echo "<tbody><tr><td colspan='5'>" . $message . "</td></tr></tbody>";
}....
try this
<tbody>
<?php
if ( isset($result )
// or if (!isset($message)) //depend on your code
{
foreach($results as $row){
?>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
Option
</td>
</tr>
<?php }}
else
//or elseif(isset($message)) //depend on your code
{
echo $message; ?>
}
</tbody>
</table>
</div>

status change on button click php mysql PDO

I am trying to change the package status as received in MySQL table, i guess the action is not performing well, can someone please spot the error, i am pasting the code below.
When i am putting the action code inside the while loop, it changes the status to Received for all the records. But when i am putting it outside the while loop, nothing happens.
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='submit' name='submit' value='Mark As Recieved' />
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$custemail));
header('Location:cust_orders.php');
}
}
?>
</tbody>
</table>
Please find the corrected code with the proper output.
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(1, $pid);
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='hidden' name='pid' value='$pid'>
<input class='btn btn-inverse' type='submit' name='submit' value='Mark As Recieved'><i class='icon-refresh icon-white'></i></input>
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE package_id=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$_POST['pid']));
header('Location:cust_orders.php');
}
?>
</tbody>
</table>
You need to define email, try this:
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?>
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<form action='#' method='post' name='updatestatus'>
<input type='submit' name='submit' value='Mark As Recieved' />
<input type='hidden' name='cust_email' value='<?php echo $custemail; ?>' />
</form>
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
$q->execute(array($status,$_POST['cust_email']));
header('Location:cust_orders.php');
}
}
?>
</tbody>
If you want to do multiple update by one submit. For this you need create one form for all items, like this:
<form action='#' method='post' name='updatestatus'>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Customer Email</th>
<th>Shipping Company</th>
<th>Delivery Date</th>
<th>Tracking ID</th>
<th>Destination Address</th>
<th>Destination ZIP</th>
<th>Mark As Recieved</th>
</tr>
</thead>
<tbody>
<?php
require('config.php');
$conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
$sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";
$q = $conn->prepare($sql);
$q->execute();
$q->bindColumn(2, $custemail);
$q->bindColumn(3, $shipcompany);
$q->bindColumn(4, $deliverydate);
$q->bindColumn(5, $trackingid);
$q->bindColumn(6, $destaddress);
$q->bindColumn(7, $destzip);
$q->bindColumn(8, $status);
while($q->fetch()){
?
<tr class="odd gradeX">
<td><?php echo $custemail ; ?></td>
<td><?php echo $shipcompany; ?></td>
<td><?php echo $deliverydate; ?></td>
<td><?php echo $trackingid; ?></td>
<td><?php echo $destaddress; ?></td>
<td><?php echo $destzip; ?></td>
<td>
<?php
if($status == "Pending") {
echo "
<input type='checkbox' name='cust_email[]' value='<?php echo $custemail; ?>' />
";
}
else {
echo "Recieved";
}
}
?>
</td>
</tr>
<tr class="odd gradeX">
<td colspan="7">
<input type='submit' name='submit' value='Mark As Recieved' />
</td>
<tr>
</tbody></table></form>
<?php
$status = "Recieved";
if(isset($_POST['submit'])){
while($q->fetch()) {
$sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
$q = $conn->prepare($sql);
foreach($_POST['cust_email'] as $cust_email)
$q->execute(array($status,$cust_email));
header('Location:cust_orders.php');
}
}
?>

Calculate the total price from a foreach generated table in php

I have the following php/html code :
<div id="demo">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
<tr>
<th>Medicine Name</th>
<th>Batch Number</th>
<th>Total Quantity</th>
<th>Expiry Date(s)</th>
<th>Selling Price</th>
<th> Total Price</th>
<th>Issue</th>
</tr>
</thead>
<tbody>
<?php foreach ($prescription as $prescribed): ?>
<tr class="odd gradeX">
<td><?php echo $prescribed['commodity_name']; ?></td>
<td ><?php echo $prescribed['batch_no']; ?></td>
<td><?php echo $prescribed['total_quantity']; ?></td>
<td><?php echo $prescribed['expiry_date']; ?></td>
<td ><?php echo $prescribed['selling_price']; ?></td>
<td><?php
$total_quantity = $prescribed['total_quantity'];
$selling_price = $prescribed['selling_price'];
$total_quantity_float = floatval($total_quantity);
$selling_price_float = floatval($selling_price);
$total_price = $total_quantity_float*$selling_price_float;
echo $total_price;
?></td>
<td>
<a class="issue" href="#types" id="issue">Issue</a>
<input type="hidden" name="batch_no" id="batch_no" value="<?php echo $prescribed['batch_no']; ?>"/>
</td>
<!-- <td> <a id="issue1" class="issue1" href="#types">Issue</a> </td>-->
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
I would like to get the total sum of the total_price variable which is displayed(total_price variable) as a row on the table. This should show the total price for all the commodities, how can I do this best?
try this
<?php
$total_price_sum = 0;
foreach ($prescription as $prescribed){
..
..
$total_price_sum = $total_price_sum + $total_price;
}
echo $total_price_sum;
?>
You can do this by adding to a variable each time you loop. This can be done as follows:
<?php
$grand_total = 0;
foreach ($prescription as $prescribed){
?>
..... all the HTML bits .....
<?
$grand_total = $grand_total + $total_price;
}
echo $grand_total;
?>
Hope that helps.
Regards,
Ralfe

Categories