PHP Issue with Order by Date in Array - php

I have a little script on which I am working where I can generate income and expense reports in a nice table, however, I am currently facing some issues with the sort order. Here is my current code:
<div class="panel-body">
<div class="col-md-12">
<?php
$fdate=$_POST['fromdate'];
$tdate=$_POST['todate'];
$rtype=$_POST['requesttype'];
?>
<?php
$source1 = $_POST['fromdate'];
$date1 = new DateTime($source1);
$source2 = $_POST['todate'];
$date2 = new DateTime($source2);
?>
<h5 align="center" style="color:#30a5ff">Expense Report from <?php echo $date1->format('d/m/Y')?> to <?php echo $date2->format('d/m/Y')?></h5>
<hr />
<!-- table start--->
<table class="table table-bordered mg-b-0">
<thead>
<tr>
<th>Date</th>
<th>Description</th>
<th>Category</th>
<th style="text-align:right;">Cash In</th>
<th style="text-align:right;">Cash Out</th>
<th style="text-align:right;background: yellow;">Balance THB</th>
</tr>
</thead>
<?php
$total_e = 0;
$userid=$_SESSION['detsuid'];
$ret=mysqli_query($con,"select * from tblexpense where (ExpenseDate BETWEEN '$fdate' and '$tdate') && (ExpenseType='Expense') && UserId='$userid' ORDER BY ExpenseDate ASC");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
$total_e = $total_e+$row['ExpenseCost']
?>
<tbody>
<tr>
<td><?php echo date('d/m/Y', strtotime($row["ExpenseDate"]));?></td>
<td><?php echo $row['ExpenseItem'];?></td>
<td><?php echo $row['ExpenseCat'];?></td>
<td></td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
</tr>
<?php
$cnt=$cnt+1;
}?>
<?php
$total_i =0;
$userid=$_SESSION['detsuid'];
$ret=mysqli_query($con,"select * from tblexpense where (ExpenseDate BETWEEN '$fdate' and '$tdate') && (ExpenseType='Income') && UserId='$userid' ORDER BY ExpenseDate ASC");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
$total_i = $total_i+$row['ExpenseCost'];
?>
<tbody>
<tr>
<td><?php echo date('d/m/Y', strtotime($row["ExpenseDate"]));?></td>
<td><?php echo $row['ExpenseItem'];?></td>
<td></td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
<td></td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
</tr>
<?php
$cnt=$cnt+1;
}?>
<tr>
<th colspan="2" style="text-align:left">Grand Total</th>
<th></th>
<th style="text-align:right;"><?php echo number_format($total_i, 2);?> THB</th>
<th style="text-align:right;"><?php echo number_format($total_e, 2);?> THB</th>
<td style="text-align:right; font-weight:bold; background: yellow;"> <?php $ret=mysqli_query($con,"SELECT ExpenseType, SUM(ExpenseCost) FROM tblexpense where (ExpenseDate BETWEEN '$fdate' and '$tdate') && (ExpenseType='Expense') && UserId='$userid' ORDER BY ExpenseDate GROUP BY ExpenseType");
$e_sum = 0;
while ($row=mysqli_fetch_array($ret)) {
//$e_sum = number_format($row['SUM(ExpenseCost)'], 2);
$e_sum = $row['SUM(ExpenseCost)'];
}
?>
<?php $ret=mysqli_query($con,"SELECT ExpenseType, SUM(ExpenseCost) FROM tblexpense where (ExpenseDate BETWEEN '$fdate' and '$tdate') && (ExpenseType='Income') && UserId='$userid'ORDER BY ExpenseDate GROUP BY ExpenseType");
$i_sum = 0;
while ($row=mysqli_fetch_array($ret)) {
//$i_sum = number_format($row['SUM(ExpenseCost)'], 2);
$i_sum = $row['SUM(ExpenseCost)'];
}
echo number_format($i_sum-$e_sum,2);
?> THB</td>
</th>
</tr>
</tbody>
</table>
<!-- table end--->
</div>
</div>
Here a screenshot of the output:
Essentially the output and the values are correct, however, the 2 items I've marked in red (which are Cash out) are not sorted by date as my other values for expenses but add to the top. My ideal goal is to have them all sorted seamlessly by ExpenseDate. What's the best way to have this achieved? Some expert help would be greatly appreciated.

You should merge your two queries into one, and then generate the table output based on the value of ExpenseType e.g.
$ret=mysqli_query($con,"select *
from tblexpense
where (ExpenseDate BETWEEN '$fdate' and '$tdate')
and (ExpenseType='Expense' or ExpenseType='Income')
and UserId='$userid'
ORDER BY ExpenseDate ASC");
then in your loop:
<tr>
<td><?php echo date('d/m/Y', strtotime($row["ExpenseDate"]));?></td>
<td><?php echo $row['ExpenseItem'];?></td>
<td><?php echo $row['ExpenseType'] = 'Expense' ? $row['ExpenseCat'] : '';?></td>
<td style="text-align:right;"><?php echo $row['ExpenseType'] = 'Expense' ? '' : number_format($row['ExpenseCost'], 2); ?> THB</td>
<td style="text-align:right;"><?php echo $row['ExpenseType'] = 'Expense' ? number_format($row['ExpenseCost'], 2) : '';?> THB</td>
<td style="text-align:right;"><?php echo number_format($row['ExpenseCost'], 2);?> THB</td>
</tr>

Related

Send HTML formated mail using PHPMailer

I need to send emails with the content of a PHP page, but I am having trouble, as far as I could do is creating static format in HTML but if tomorrow the data has more rows the email will be incomplete,
I will share part of my mail.php
//The query
$wipAgingQuery = $dbconnection->query("
SELECT
CAST(datein AS DATE) AS [Date_In],
COUNT(lab) AS [Count_of_Jobs],
ROUND(SUM(COUNT(lab)) OVER (ORDER BY datein DESC) * 100.0 / SUM(COUNT(lab)) OVER (),2) AS [Cumulative]
FROM [DailyWIP].[dbo].[WIP_Daily_Load]
WHERE location = 'USA' AND dateload BETWEEN CONCAT(CAST(GETDATE() AS DATE),' 11:30:00.000') AND CONCAT(CAST(GETDATE() AS DATE),' 14:00:00.000')
GROUP BY datein ORDER BY datein DESC;");
$jobInWip = $wipAgingQuery->fetchAll(PDO::FETCH_OBJ);
//Assign Query Result in Variable
foreach ($jobInWip as $jobInWip) {
$wipDateIn = $jobInWip -> Date_In;
$wipCount = $jobInWip -> Count_of_Jobs;
$wipCumulative = $jobInWip -> Lab_Cumulative;
$totalJobCount = $totalJobCount + $wipCount;
}
//HTML inside the Mailer Body
<div class='row'>
<div class='col-12'>
<h1>Current Day WIP: Aging</h1>
<table class='table table-bordered'>
<thead class='thead-dark'>
<tr>
<th>Date In</th>
<th>Count of Jobs</th>
<th>Lab Cumulative</th>
</tr>
</thead>
<tbody>
<tr>
<td>$wipDateIn</td>
<td><p class='text-right'>$wipCount</p></td>
<td><p class='text-right'>$wipCumulative</td>
</tr>
<tr>
<td>Grand Total:</td>
<td><p class='text-right'>$totalJobCount</p></td>
</tr>
</tbody>
</table>
</div>
</div>
This is the original wipdata.php page where I can manage that problem since the table is being created by a foreach depending on the results of the query, but I don't know how to do something similar in the mail.php
<div class="row">
<div class="col-12">
<h1>Current Day WIP: Aging</h1>
<br>
<table class="table table-bordered">
<thead class="thead-dark">
<tr>
<th>Date In</th>
<th>Count of Jobs</th>
<th>Lab Cumulative</th>
</tr>
</thead>
<tbody>
<?php foreach ($jobInWip as $jobInWip) { ?>
<tr>
<td><?php echo $jobInWip->Date_In ?></td>
<td><?php echo '<p class="text-right">'. number_format($jobInWip->Count_of_Jobs) .'</p>' ?></td>
<td><?php echo '<p class="text-right">'. number_format($jobInWip->Lab_Cumulative, 2) . ' %' ?></td>
</tr>
<?php $totalJobCount = $totalJobCount + $jobInWip->Count_of_Jobs;
} ?>
<tr>
<td><?php echo 'Grand Total: ' ?></td>
<td><?php echo '<p class="text-right">'. number_format($totalJobCount).'</p>'; ?></td>
</tr>
</tbody>
</table>
</div>
</div>
Oh I just found a way around this, I am not sure if this is the best way to solve it but it worked.
foreach ($jobInWip as $jobInWip) {
$wipDateIn = $jobInWip -> Date_In;
$wipCount = $jobInWip -> Count_of_Jobs;
$wipCumulative = $jobInWip -> Lab_Cumulative;
$totalJobCount = $totalJobCount + $wipCount;
$phpmailer-> Body .="<tr>
<td>$wipDateIn</td>
<td><p class='text-right'>$wipCount</p></td>
<td><p class='text-right'>$wipCumulative</td>
</tr>";}

DataTables - PHP while loop issue can't find the last result and add new TR

I am using DataTables and I want to add new TR at the end of while loop.
I know we can add <tfoot></tfoot>, but I don't want to add '' because I am filtering data with custom Ajax.
I have tried below code but it's not working:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
Also, when I use <tfoot></tfoot> it's not printable.
Probably your problem is in $numRows which is not defined.
So you can try this:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
$numRows = mysqli_num_rows($Itesres);
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>

Generate multiple table (For viewing)

I would like to ask on how to make my table to appear as following:
But I only manage to get my table to appear like this:
As you can see, the table is separated according to the different year or semester of when they stored their stuff. It is basically a history storage of that particular person. Unfortunately, I don't know how to make the generated table to attach together for the one with same year and semester instead of separating it. Even the numbering is affected. Below is the code that I have so far:
<?php
include("connect.php");
include("header.php");
if(isset($_GET['invstuview_bag'], $_GET['invstuview_name']))
{
$get_id = $_GET['invstuview_bag'];
$get_name = $_GET['invstuview_name'];
?>
<br/>
<ul class="breadcrumb">
<li class="breadcrumb-item">View History</li>
<li class="breadcrumb-item-active">Baggage Detail History</a></li>
</ul>
<div id="cssword">Baggage Detail History For <?php echo $get_name; ?>(<?php echo $get_id; ?>)</div>
<div class="container" style="width:70%;">
<div class="table-responsive">
<?php
$sql_join = "SELECT student.*,location.*, inventory.*, baggage.*
FROM
student,location, inventory, baggage
WHERE
student.student_id = inventory.invstu_id AND
baggage.baggage_id = inventory.invbag_id AND
location.location_id = inventory.invloc_id AND
invstu_id = '$get_id'
ORDER BY inventory_id";
$result_join= mysqli_query($connect,$sql_join);
$prev_year = "";
$prev_sem = 0;
$get_year = "";
$get_sem = 0;
while($row_join=mysqli_fetch_assoc($result_join))
{
$counter = 1;
$prev_year = $row_join["invstu_year"];
$prev_sem = $row_join["invstu_sem"];
//if the data is of same year and semester
if(($prev_year!="") && ($prev_sem!=0) && ($prev_year == $get_year) && ($prev_sem == $get_sem))
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
//if data is not of same year or semester
else
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
</br></br>
Room: <?php echo $row_join["invstu_room"]; ?><br/>
Year: <?php echo $row_join["invstu_year"]; ?><br/>
Semester: <?php echo $row_join["invstu_sem"]; ?><br/>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
}
?>
</div>
<div align="right">
<button type="button" name="back" class="btn btn-success" id="back" style="width:200px; display:inline-block; margin-top:2%; margin-bottom:1%; background-color: #4CAF50" onclick="window.location.href='view.php'">Back</button>
</div>
</div>
<?php
}
?>
Any ideas is highly appreciated.
Move these out of the while loop:
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
echo'</table>';

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

Getting the option value without using submit button

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();
}
})

Categories