Sum up query result - mysql

I have reservation and postflight table. I have to do a query in the reservation table and look for the data in the postflight table.
$query="select * from reservation where date(fdate) between '$datefrom' and '$dateto' and status in ('Flown') $aircraft order by $sort";
$result=mysql_query($query) or die(mysql_error());
echo "<div class='box'><table class='hovertable'>
<th>Flight Date</th>
<th>Client Name</th>
<th>Group Code</th>
<th>Aircraft</th>
<th>Block Time</th>
<th>Waiting Time</th>
<th>Charter Fee</th>
<th>Take-off and Landing Fee</th>
<th>Waiting Time Fee</th>
<th>Other Charges</th>
<th>Sub-Total</th>
<th>Value-added Tax</th>
<th>Total Service Invoice Amount</th>
<th>Reservation No.</th>
</tr>";
while($row=mysql_fetch_array($result))
{
$rvno=$row['reservno'];
$yr=(string) date("Y");
$rn=$yr."-".$rvno;
$a=mysql_query("select *, (fdf + fce + aef + hf + sfp) as 'tcharge' from postflight where reservno='$rvno'") or die(mysql_error());
//$e=mysql_fetch_array($a);
while($b=mysql_fetch_array($a))
{
echo"<tr><td>".$row['fdate']."</td><td>".$b['cliename']."</td><td>".$row['grpcode']."</td><td>".$row['acode']."</td><td>".$row['btime']."</td><td>".$b['wtime']."</td><td>".$b['total_cfee']."</td><td>".$b['total_tol']."</td><td>".$b['total_wtfee']."</td><td>".$b['tcharge']."</td><td>".$b['sub_total']."</td><td>".$b['vat']."</td><td>".$b['total_service_invoice_amt']."</td><td>".$rn."</td></tr>";
}
}
Before I close the </table>, I would like to add another row where it sums up all the required fields to be summed up based on the outputted data of the query above. Something like,
echo "<tr><td colspan='6'></td><td><b>sum</b></td><td><b>sum</b></td><td><b>sum</b></td><td><b>sum</b></td><td><b>sum</b></td><td><b>sum/b></td><td><b>sum</b></td><td></td></tr>";
But I'm not sure how to do it. Please help. Thanks.

you could do something like this:
$sum = 0;
while($b=mysql_fetch_array($a))
{
//your codes
$sum += $fields //fields you want to sum up
echo "<tr><td> 'Sum:'. $sum ... </td></tr>";
}
echo "</table>";

Related

MySQL INNER JOIN returns a row but from the front end,it shows no rows

I have this code as my SQL script
SELECT
lib_use_details.book_code,
book_details.book_name,
lib_use_details.date_out,
lib_use_details.exp_date_in
FROM
lib_use_details
INNER JOIN
book_details ON lib_use_details.book_code = book_details.book_code
WHERE
lib_use_details.stu_details = 0001
When I execute this code directly on the database, it returns a 1 row as shown by this image
However my front-end displays nothing as shown by this
Why is this result conveyed from the db to the frontend?
This is the code that displays on the front end
$querysql = $conn->prepare("SELECT lib_use_details.book_code, `book_details.book_name, lib_use_details.date_out ,lib_use_details.exp_date_in `FROM lib_use_details INNER JOIN book_details ON lib_use_details.book_code` `=book_details.book_code WHERE lib_use_details.stu_details = 0001");
$querysql->setFetchMode(PDO::FETCH_ASSOC);
$querysql->execute();
$result=$querysql->fetchAll();
echo '
<thead>
<tr>
<th>Book ID</th>
<th>Book Title</th>
<th>Date Borrowed</th>
<th>Expected Return Date</th>
<th>Actual Return Date</th>
</tr>
</thead>
';
foreach ($result as $row) {
$field2= $row['term'];
$field3=$row['amount'];
$field4= $row['bal'];
$field5=$row['mode'];
$field6= $row['date_received'];
echo '
<tbody><tr>
<td>'.$field2.'</td>
<td>'.$field3.'</td>
<td>'.$field4.'</td>
<td>'.$field5.'</td>
<td>'.$field6.'</td>
</tr></tbody>
';
}
echo ' <tfoot>
<tr>
<th>Book ID</th>
<th>Book Title</th>
<th>Date Borrowed</th>
<th>Expected Return Date</th>
<th>Actual Return Date</th>
</tr>
</tfoot>';
Thanks

SQL fetch in tables

First of all. I apologize for the poor English. I'm also sorry for the novice level of this question.
I would like to put my SQL results in a table, and add a new row for every result.
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<p><b>".$results['FNAME']."</b> " .$results['LNAME']." ".$results['AGE']."</p>";
}
}
So I would like [FNAME] in a own TD, [LNAME] in it's on TD and the same with [AGE].
Thank you in advance!
Just put <tr> around each row of data, <td> around each column, and put everything inside <table>.
if (mysql_num_rows($raw_results) > 0) {
echo "<table><tr><th>First Name</th><th>LAst Name</th><th>Age</th></tr>";
while ($results = mysql_fetch_assoc($raw_results) {
echo "<tr><td>{$results['FNAME']}</td><td>{$results['LNAME']}</td><td>{$results['AGE']}</td></tr>";
}
echo "</table>";
}
use this code :
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<tr><td>" . $results['FNAME'] . "</td><td>" . $results['LNAME'] . "</td><td>" .$results['AGE']."</td></tr>";
}
}
?>
</tbody>
</table>

Highlight a row in table html which has a value exceed the avg

I have a table which is looks like this.
What I want to do is, I want to Highlight (Yellow COLOR) which row has USAGE PER 1K Higher than the AVERAGE. The result should looks like this.
Here is my code to make a row been hightlight when exceed value of average, but I cannot change the value if($row['duh']>'0.77') to the if($row['duh']>'$avg') because it will hightlight all the rows.
$no = 1;
while ($row = mysqli_fetch_array($result))
{
$month1=strtotime($row['month']);
$month=date('m-Y',$month1);
$store_name=$row['store_name'];
$netamt1=$row['netamt'];
$netamt11=number_format($netamt1,2);
$usage1=$row['monusage'];
$usage11=number_format($usage1,2);
$answer=$usage1/$netamt1*1000;
$answer1=number_format($answer,2);
$total+=$answer1;
$avg=$total/$no;
$duh = number_format($row['duh'],2);
echo "<tr>";
echo "<td>".$no."</td>";
echo "<td>".$store_name."</td>";
echo "<td>".$date2111."</td>";
echo "<td>".$netamt11."</td>";
echo "<td>".$usage11."</td>";
if($row['duh']>'0.77') {// 0.77 i cannot change to $avg, because it will hightlight all the rows become yellow
echo "<td style='background-color: #FFFF00;'>".$duh."</td>";}
else {
echo "<td>".$duh."</td>";
}
echo "</tr>";
$no++;
}?>
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th><?=($total)?></th>
</tr>
<tr>
<th colspan="5">AVERAGE</th>
<th><?=number_format($avg,4)?></th>
</tr>
</tfoot>
You can add avegage as another column in query result and compare the values against it, e.g.:
SELECT no, outlet, date, sales, usage, usage_per_1k, AVG(usage_per_1k) AS avg
FROM table
WHERE condition;
AND then, add the following to highlight:
if($row['duh']>$row['avg']) {
hightlight all the rows become yellow
echo "<td style='background-color: #FFFF00;'>".$duh."</td>";
}
After keep trying, now i figure out how to hightlight a row depending on the condition average, if Higher than average, the row will Yellow, but I make it RED now. If Lower than average, the color will not change. Im using script in <body> tag, and add class in <tr> tag, then do function.
$no = 1;
while ($row = mysqli_fetch_array($result))
{
$month1=strtotime($row['month']);
$month=date('m-Y',$month1);
$store_name=$row['store_name'];
$netamt1=$row['netamt'];
$netamt11=number_format($netamt1,2);
$usage1=$row['monusage'];
$usage11=number_format($usage1,2);
$answer=$usage1/$netamt1*1000;
$answer1=number_format($answer,2);
$duh = number_format($row['duh'],2);
$total+=$duh;
$avg=$total/$no;
echo "<tr class='y_n';>";
echo "<td>".$no."</td>";
echo "<td>".$store_name."</td>";
echo "<td>".$date2111."</td>";
echo "<td>".$netamt11."</td>";
echo "<td>".$usage11."</td>";
$no++;
echo "<td>".$duh."</td>";
echo "</tr>";
} ?>
<script type="text/javascript">
$('#table_id tr.y_n td').each(function() {
if ($(this).text() >=<?=number_format($avg,2)?>) {
$(this).parent().css('background-color', '#fc9c9c');
}
else
$(this).parent().css('background-color', '#e5f5ff');
});
</script>
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th><?=($total)?></th>
</tr>
<tr>
<th colspan="5">AVERAGE</th>
<th><?=number_format($avg,4)?></th></tr>
<tr>
<th colspan="6"><center><?php echo $actionstatus?></center></th>
</tr>
</tfoot>
</table>
Here is the Result :

Distinct with join in codeigniter mysql

How can I use Distinct with join in CodeIgniter, I'm trying to fetch customer's name and registered category, but my problem is whiles fetching the data I'm getting data in repetation because of sub-category, I've used distinct but then I'm getting Ambiguous field error, how can I solve this problem.
My View
<table class="table table-hover example1">
<thead>
<tr>
<th>SNo.</th>
<th>Business Name</th>
<th>Category Name</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php $i=0;
foreach($value as $row){ ?>
<tr>
<td><?php echo ++$i;?></td>
<td><?php echo $row->Bussiness_Name;?></td>
<td><?php echo $row->Category_Name;?></td>
<td onClick="RestoreVendor(<?php echo $row->Business_Id; ?>,<?php echo $row->Trash;?>,'<?php echo $row->Bussiness_Name;?>')" class="btn btn-primary">Remove</td>
</tr>
<?php }?>
</tbody>
</table>
My Controller:
public function removecategory()
{
$this->load->model('VendorModel');
$data = $this->VendorModel->remove_category();
$this->load->view('admin/remove_cat', array('value'=>$data));
}
My Model
public function remove_category()
{
$this->db->select('*');
$this->db->distinct('Category_Id');
$this->db->from('business_mapping');
$this->db->join('business_profile_details', 'business_profile_details.Business_Id = business_mapping.Business_Id');
$this->db->join('category_master', 'category_master.Category_Id = business_mapping.Category_Id');
$query = $this->db->get();
return $query->result();
}
result image
You can use below mentioned query.
$query = $this->db->group_by('category_master.Category_Id,business_profile_details.Business_Id');
Please try above, It will help you.
you can use
$this->db->distinct();
Some times distinct not works then you can use
$this->db->group_by('business_mapping.unique_coloumn_Name');

Export from MYSQL to HTML

I am new to MYSQL and i am trying to export my database to html, and put the data into specific html tables. I have looked around the site, but i cant get my code to work, can someone help me, and tell me if this code looks right ?
<?php
$con=mysqli_connect("xxx","xxx","xxx","xxx","xxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM xxx");
while($row = mysqli_fetch_array($result))
{
<tr>
<th bgcolor="#3281c6">" . $row["PO_nummer"]. "</th>
<th bgcolor="#3281c6">" . $row["Varenummer"]. "</th>
<th bgcolor="#3281c6">" . $row["Produkt_beskrivelse_"]. "</th>
<th bgcolor="#3281c6">" . $row["Antal"]. "</th>
}
mysqli_close($con);
?>
You seem to be expecting an associative array and not a regular (numbered) array, in this case try:
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
// code
}
Try and echo or print the html
echo "
<th bgcolor="#3281c6">" . $row["PO_nummer"]. " </th>
<th bgcolor="#3281c6">" . $row["Varenummer"]. "</th>
<th bgcolor="#3281c6">" . $row["Produkt_beskrivelse_"]. "</th>
<th bgcolor="#3281c6">" . $row["Antal"]. "</th>";
And use single quotes here bgcolor='#3281c6'