Looping the product based on the package - php

Product_Package
------------------------------------------------------------------------------------
code size price image description
------------------------------------------------------------------------------------
0001 2kg 20 0012.jpg description0012
0001 4kg 40 0014.jpg description0014
0001 6kg 60 0016.jpg description0016
0001 8kg 80 0018.jpg description0018
0002 3kg 30 0023.jpg description0023
0002 5kg 50 0025.jpg description0025
0002 7kg 70 0027.jpg description0027
0002 8kg 80 0028.jpg description0028
0003 1kg 10 0031.jpg description0031
0003 2kg 20 0032.jpg description0032
0003 6kg 60 0036.jpg description0036
0003 9kg 90 0039.jpg description0039
Product_List
------------------------------
code name
------------------------------
0001 FR
0002 GR
0003 WR
0004 BR
And then I try to get the data with this code
$results = $mysqli->query
("
SELECT product_package.size,
product_package.price,
product_package.description,
product_package.image,
product_list.code,
product_list.name
FROM product_package
LEFT JOIN product_list ON product_list.code = product_package.code"
);
$orders = array();
$html = '';
if ($results) {
while($obj = $results->fetch_object()) {
$orders[$obj->code][$obj->name] = array(
'name' => $obj->name,
'price' => $obj->price,
'size' => $obj->size,
'description' => $obj->description,
'image' => $obj->image);
}
$html .= '<table width="70%"><tr>';
$html .= '<td>id</td>';
$html .= '<td>name</td>';
$html .= '<td>package_size</td>';
$html .= '<td>price</td>';
$html .= '<td>image</td>';
foreach ($orders AS $order_id => $order) {
$orderCount = count($order);
$html .= '<tbody>';
$html .= '<tr>';
$html .= '<td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['size'] . '</td>';
$html .= '<td>' . $data['price'] . '</td>';
$html .= '<td>'. $data['description'] . '</td>';
$html .= '<td>' . $data['image'] . '</td>';
$row++;
}
}
$html .= '</tr>';
$html .= '<tbody>';
$html .= '</table>';
}
echo $html;
What I want the result is like this:
------------------------------------------------------------------------------------
code name package size price image
------------------------------------------------------------------------------------
2kg 20 0012.jpg
0001 FR 4kg 40 0014.jpg
6kg 60 0016.jpg
8kg 80 0018.jpg
3kg 30 0023.jpg
0002 GR 5kg 50 0025.jpg
7kg 70 0027.jpg
8kg 80 0028.jpg
1kg 10 0031.jpg
0003 WR 2kg 20 0032.jpg
6kg 60 0036.jpg
9kg 90 0039.jpg
However the php code above give me this result:
------------------------------------------------------------------------------------
id name package_size price descripttion image
------------------------------------------------------------------------------------
0001 FR 2kg 20 description0012 0012.jpg
0002 GR 4kg 40 description0024 0024.jpg
0003 WR 6kg 60 description0036 0036.jpg
0004 BR 8kg 80 description0048 0048.jpg
90 description0039 0039.jpg
Can anybody help me out with this? Thanks in advance.

You need to use group by and group concate functions of mysql please refer below sample query.
select id,group_concat(package_size) as package, group_concat(price) as Price, group_concat(image) as Image from mytbl group by code

Related

PHP MYSQL while loop till same value than break and sum, than do the same with next value

I try to loop all my rows from MySQL, and I got it. The problem is I want to case them, I think its the correct expression.
For example: MYSQL
+----+----------+-------------+------------+
| id |abholdatum| fahrer | preis |
+----+----------+-------------+------------+
| 6 |14 11 15 | martin| 22 |
| 7 |14 11 15 | david | 25 |
| 8 |31 12 15 | david | 22 |
| 10|29 12 15 | martin| 23 |
| 12|30 12 15 | david | 29 |
+----+----------+-------------+------------+
now I get this:
ID Abholdatum Fahrer Preis
7 14 November 2015 david 25
8 31 Dezember 2015 david 22
12 30 Dezember 2015 david 29
6 14 November 2015 martin 22
10 29 Dezember 2015 martin 23
and I actually want this:
ID Abholdatum Fahrer Preis
--------------------------------------
David
7 14 November 2015 david 25
8 31 Dezember 2015 david 22
12 30 Dezember 2015 david 29
--------------------------------------
Summ 76
--------------------------------------
--------------------------------------
Martin
6 14 November 2015 martin 22
10 29 Dezember 2015 martin 23
--------------------------------------
Summ 45
--------------------------------------
and this is my simple php script:
$sql = mysqli_query($dbc, "select * from fahrten ORDER BY fahrer ASC");
while ($row = mysqli_fetch_array($sql)){
echo "<tr>"
. "<td>" . $row['id'] ."</td>"
. "<td>" . $row['abholdatum'] ."</td>"
. "<td>" . $row['fahrer'] . "</td>"
. "<td>" . $row['preis'] . "</td>"
. "</tr>";
}
echo "</table>"
. "</div>";
use this code
$sql = mysqli_query($dbc, "select * from fahrten ORDER BY fahrer ASC");
$fahrer = array();
while ($row = mysqli_fetch_array($sql)){
if (!in_array($row['fahrer'], $fahrer))
{
$fahrer[] = $row['fahrer'];
}
}
foreach($fahrer as $value){
echo "<tr>". $value ."</tr>";
$sum=0;
while ($row1 = mysqli_fetch_array($sql)){
if($value = $row1['fahrer']){
echo "<tr>"
. "<td>" . $row1['id'] ."</td>"
. "<td>" . $row1['abholdatum'] ."</td>"
. "<td>" . $row1['fahrer'] . "</td>"
. "<td>" . $row1['preis'] . "</td>"
. "</tr>";
$sum=$sum+$row1['preis'];
}
}
echo "Summ = ".$sum; }
echo "</table>"
. "</div>";
I tested this, It should work:
$new = 1;
$sum = 0;
$last_fahrer = 'initial';
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
if($last_fahrer != $row['fahrer'] && $last_fahrer != 'initial') {
echo "<tr>"
. '<td colspan="3">Summ</td>'
. "<td>" . $sum . "</td>"
. "</tr>";
$sum = 0;
$new = 1;
}
if($new == 1) {
echo "<tr>"
. '<td colspan="4">' . ucwords($row["fahrer"])
. '</td>'
. '</tr>';
$new = 0;
}
echo "<tr>"
. "<td>" . $row['id'] ."</td>"
. "<td>" . $row['abholdatum'] ."</td>"
. "<td>" . $row['fahrer'] . "</td>"
. "<td>" . $row['preis'] . "</td>"
. "</tr>";
$last_fahrer = $row['fahrer'];
$sum = $sum + $row['preis'];
}
echo "<tr>"
. '<td colspan="3">Summ</td>'
. "<td>" . $sum . "</td>"
. "</tr>"
. "</table>"
. "</div>";

How to sum each column in MySQL table with PHP

I want to display the total of each column inside the table. Here is my code.
$Tab_info['Width'] = "600" ;
$Col_det = array(
'Name' => array( 'Name',
'Aging Create Order ( <= 5 )',
'Aging Create Order ( > 5 )',
'Total'),
'Size' => array( '200' , '200' , '200' , '100' ),
'Type' => array('sortable-text fd-column-', 'sortable-numeric fd-column-','sortable-numeric fd-column-','sortable-numeric fd-column-'));
$Delim_String = '<table id="Summary" class="sortable-onload-0-normal rowstyle-alt no-arrow" style="width:'.$Tab_info['Width'].'px;background-color:#FFFFFF;">';
$Delim_String .= '<thead><tr>';
for ($Col_count = 0; $Col_count < sizeof($Col_det['Name']) ; $Col_count++) {
$Delim_String .= '<th style="-moz-user-select: none;" class="'.$Col_det['Type'][$Col_count].$Col_count.'" width="'.$Col_det['Size'][$Col_count].'">';
$Delim_String .= '<a title="Sort on '.$Col_det['Name'][$Col_count].'" href="#">'.$Col_det['Name'][$Col_count].'</a>';
$Delim_String .= '</th>';
}
$Delim_String .= '</tr>
</thead>
<tbody>
';
//===================Summary Aging Create Order End===========================
$vSQL = "SELECT u.Nama as Name,
SUM( CASE WHEN t.`AgingCreateOrder` <= 5 THEN 1 ELSE 0 END) 'Aging Less Than or Equal 5',
SUM( CASE WHEN t.`AgingCreateOrder` > 5 THEN 1 ELSE 0 END) 'Aging More Than 5',
COUNT(*) as Total
FROM `oct_usergroup` u
JOIN `oct_tickets` t
ON (t.`Creator_SubType`= u.`Id`)
WHERE
t.Date_Created >= (CURDATE() -INTERVAL 49 DAY)
AND u.Nama IS NOT NULL
AND t.AgingCreateOrder between '1' and '1999'
AND t.Status = 1
GROUP BY u.`Nama`";
$result = mysql_query($vSQL)or die($vSQL."<br/><br/>".mysql_error());
while($row = mysql_fetch_array ($result))
{
$Delim_String .= '
<tr>
<td>'.$row['Name'].'</td>
<td>'.$row['Aging Less Than or Equal 5'].'</td>
<td>'.$row['Aging More Than 5'].'</td>
<td>'.$row['Total'].'</td>
</tr>';
}
$Delim_String .= "</tbody></table>";
$table1 = $Delim_String;
unset($Delim_String);
//===================Summary Aging Update Address start===========================
$Tab_info['Width'] = "600" ;
$Col_det = array(
'Name' => array( 'Name',
'Aging Update Address ( <= 5 )',
'Aging Update Address ( > 5 )',
'Total'),
'Size' => array( '200' , '200' , '200' , '100' ),
'Type' => array('sortable-text fd-column-', 'sortable-numeric fd-column-','sortable-numeric fd-column-','sortable-numeric fd-column-')
);
$Delim_String = '<table id="Summary" class="sortable-onload-0-normal rowstyle-alt no-arrow" style="width:'.$Tab_info['Width'].'px;background-color:#FFFFFF;">
';
$Delim_String .= '<thead>
<tr>
';
for ($Col_count = 0; $Col_count < sizeof($Col_det['Name']) ; $Col_count++) {
$Delim_String .= '
<th style="-moz-user-select: none;" class="'.$Col_det['Type'][$Col_count].$Col_count.'" width="'.$Col_det['Size'][$Col_count].'">';
$Delim_String .= '<a title="Sort on '.$Col_det['Name'][$Col_count].'" href="#">'.$Col_det['Name'][$Col_count].'</a>';
$Delim_String .= '</th>
';
}
$Delim_String .= '</tr>
</thead>
<tbody>
';
I want it to show like this.
====================================================================
| Name | Aging Create Order <=5 | Aging Create Order >5 | Total |
---------------------------------------------------------------------
| Consumer | 159 | 80 | 239 |
---------------------------------------------------------------------
| CSO | 20 | 50 | 70 |
---------------------------------------------------------------------
| Total | 179 | 130 | 309 |
=====================================================================
The last row is the one to be added.
Try with this
$result = mysql_query($vSQL)or die($vSQL."<br/><br/>".mysql_error());
$gt_less = 0;
$gt_more = 0;
$gt_total = 0;
while($row = mysql_fetch_array ($result))
{
$gt_less += $row['Aging Less Than or Equal 5'];
$gt_more += $row['Aging More Than 5'];
$gt_total += $row['Total'];
$Delim_String .= '
<tr>
<td>'.$row['Name'].'</td>
<td>'.$row['Aging Less Than or Equal 5'].'</td>
<td>'.$row['Aging More Than 5'].'</td>
<td>'.$row['Total'].'</td>
</tr>';
}
$Delim_String .= '
<tr>
<td>Grand Total</td>
<td>'.$gt_less.'</td>
<td>'.$gt_more.'</td>
<td>'.$gt_total.'</td>
</tr>';
$Delim_String .= "</tbody></table>";

SELECT SUM data rank and Resource id #9 error PHP

I have a website in which logged in members are stored under a table called ‘users’
‘users’ table
users_sales_guild_id | users_first_name | users_surname
555 | Jane | Smith
333 | John | Smith
111 | Mike | Myers
The users have sales data in a 'sales_points' field which is in a separate table called ‘sales_list’.
‘sales_list’ table
sales_id | users_sales_guild_id | sales_points | sales_entry_date
1 | 555 | 50 | 2013-02-31 00:00:00
2 | 333 | 30 | 2013-02-31 00:00:00
3 | 111 | 10 | 2013-02-31 00:00:00
4 | 555 | 50 | 2013-03-31 00:00:00
5 | 333 | 30 | 2013-03-31 00:00:00
6 | 111 | 10 | 2013-03-31 00:00:00
Essentially what I am trying to do is a query which:
A. Calculates the total amount of 'sales_points' for each user from 'sales_list'
B. Lists 100 users with the user having the most points at the top, then ranking the next highest below and so on...
C. Im not after a ranking number, just the order itself.
With the code below I am getting 100 users printing ok, however I am getting a ‘Resource id #9’ message in the ’Total Points’ column when it prints. Can anyone help?
What I am trying to print
Jane Smith | 100
John Smith | 60
Mike Myers | 20
Code:
<?php
require_once ('config.inc.php');
$page_title = '';
include ('header.html');
if (!isset($_SESSION['users_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
// Add the page.
$url .= 'login.php';
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
}?>
<h1>Rankings</h1>
<?php require_once ('database.php'); // Connect to the database.
$total = mysql_query("SELECT SUM(sales_points) FROM sales_list,users WHERE sales_list.users_sales_guild_id = users.users_sales_guild_id
AND sales_entry_date
BETWEEN '2013-10-01 00:00:00' AND '2013-11-30 23:59:59'
" );
$query = "SELECT us.users_id, us.dealership_id, us.users_sales_guild_id, us.users_first_name, us.users_surname, us.users_type,
de.dealership_id, de.users_dealer_name, de.class , de.region, de.state, de.users_dealer_code_id, de.users_dealer_code_new_id, de.users_model, de.pma
FROM users AS us, dealerships AS de
WHERE us.dealership_id = de.dealership_id
ORDER BY ’$total’ DESC
LIMIT 100";
$result = #mysql_query ($query);
// Table header.
echo '<table width="680"cellpadding="5" cellspacing="1" style="font-size:12px;">
<tr class="orangehead">
<td align="center"><b>Member</b></td>
<td align="center"><b>Title</b></td>
<td align="center"><b>Dealer</b></td>
<td align="center"><b>Category</a></b></td>
<td align="center"><b>Dealer Code</a></b></td>
<td align="center"><b>Total Points</a></b></td>
</tr>';
// Fetch and print all the records. echo '<td align="left"><strong>' . $row['sp_invoice_no'] . '</strong></td> ';
$bg = '#ffffff'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#eaeced' ? '#ffffff' : '#eaeced'); // Switch the background color. New: ' . $row['users_sales_guild_new_id'] . '
// $entries = floor($row['sp_entry_amount']/200);
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left"><strong>' . $row['users_first_name'] . ' ' . $row['users_surname'] . '</strong></td> ';
echo '<td align="center">' . $row['users_type'] . ' </td>';
echo '<td align="center"> ' . $row['users_dealer_name'] . ' </td>';
echo '<td align="center"> ' . $row['class'] . ' </td>';
echo '<td align="center"> ' . $row['users_sales_guild_id'] . ' </td>';
echo '<td align="center"> ' . $total . '</td>';
echo '</tr>
';
}
echo '</table>';
mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
include ('footer.html'); // Include the HTML footer.
?>
You don't fetch you first result. Fetch it as an array (mysql_fetch_array()) for example.
// Execute query
$total_query = mysql_query("SELECT SUM(sales_points)
FROM sales_list, users
WHERE sales_list.users_sales_guild_id = users.users_sales_guild_id
AND sales_entry_date
BETWEEN '2013-10-01 00:00:00' AND '2013-11-30 23:59:59'");
// Fetch result
$total = mysql_fetch_array($total_query);
You echo this:
$total = mysql_query("SELECT SUM(sales_points) FROM sales_list,users WHERE sales_list.users_sales_guild_id = users.users_sales_guild_id
AND sales_entry_date
BETWEEN '2013-10-01 00:00:00' AND '2013-11-30 23:59:59'
" );
The variable $total contains the result of the query, which is a Resource. You should use mysql_fetch_assoc or a similar function to extract the result itself. It's best to give the SUM(sales_points) an alias, i.e. AS total_sales_points so you can fetch is easily.
By the way, the mysql_*-extension is soon-to-be deprecated. Good alternatives include MySQLi and PDO.
$query = mysql_query("SELECT SUM(sales_points) as points FROM sales_list,users WHERE sales_list.users_sales_guild_id = users.users_sales_guild_id
AND sales_entry_date
BETWEEN '2013-10-01 00:00:00' AND '2013-11-30 23:59:59'
" );
$row = mysql_fetch_object($query);
$total = $row->points;
You are echoing the mysql resource, you first need to fetch the results, then you can echo them.

MYSQL and PHP group results by field data

I am trying to create reports based on data from a log in the database that looks like:
id | student | type | marks
1 23494 CAT1 50
2 23495 CAT1 20
3 23494 CAT2 35
4 23495 MIDTERM 40
My select statement so far looks like this:
$res = #mysqli_query ($dbc, "SELECT id, student, type, GROUP_CONCAT(marks) AS mark, GROUP_CONCAT(type) AS types FROM log WHERE class = '1' AND term = '2' GROUP BY student DESC");
// Fetch and print all the records....<br>
while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
echo '<tr>
<td align="left">'. $row['student'] .'</td>';
//$exams = split(",", $row['exams']); // 4,3,1,2
$marks = split(",", $row['mark']); // 10,20,40,50
foreach( $marks as $mark ) {
echo '
<td align="left">' . $mark . '</td>
';
}
echo '</tr>';
} //End LOOP
//Then i end table
So far the data displays like so:
STUDENT | CAT1 | CAT2 | MIDTERM
23494 50 35
23495 20 40
The problem is that the code is not arranging 'marks' according to 'type' (look at MIDTERM output for id 4 and corresponding display).
Question:
How do i display the results by student, followed by marks in the appropriate cell/group like so:?
STUDENT | CAT1 | CAT2 | MIDTERM
23494 50 35
23495 20 40
Thanks in Advance Guys.
First, try to keep logic away from layout. It's generally good practice to first gather the data you need, and then display it.
Using GROUP_CONCAT can make things more complicated, since you do not know how many results you will get for each student, nor will you be able to easily identify which marks are of what type.
With that in mind I've created a solution. You'll still need to extend the query of course.
$query = 'SELECT student, type, marks FROM log';
$res = mysqli_query($query);
$studentMarks = array();
while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC))
{
$studentMarks[$row['student']][$row['type']] = $row['marks'];
}
// Now $studentMarks should look like:
// $studentMarks = array(
// 23494 => array('CAT1' => 50, 'CAT2' => 35)
// , 23495 => array('CAT1' => 20, 'MIDTERM' => 40)
// );
echo '<table><thead><tr>';
echo '<td>Student</td><td>CAT1</td><td>CAT2</td><td>MIDTERM</td>';
echo '</tr></thead><tbody>';
foreach($studentMarks as $studentId => $marks)
{
echo '<tr>';
echo '<td>', $studentId, '</td>';
echo '<td>', (isset($marks['CAT1']) ? $marks['CAT1'] : ' '), '</td>';
echo '<td>', (isset($marks['CAT2']) ? $marks['CAT2'] : ' '), '</td>';
echo '<td>', (isset($marks['MIDTERM']) ? $marks['MIDTERM'] : ' '), '</td>';
echo '</tr>';
}
echo '</tbody></table>';

sum in query / subquery

Table has the following info:
date |vendor|sales|percent|
--------------------------
2009-03-03| 10 |13.50| 1.30 |
2009-03-10| 10 |42.50| 4.25 |
2009-03-03| 21 |23.50| 2.30 |
2009-03-10| 21 |32.50| 3.25 |
2009-03-03| 18 |53.50| 5.30 |
2009-03-10| 18 |44.50| 4.45 |
I want it to sort into separate tables depending on date as follows:
date |vendor|sales|percent|
--------------------------
2009-03-03| 10 |13.50| 1.30 |
2009-03-03| 18 |53.50| 5.30 |
2009-03-03| 21 |23.50| 2.30 |
date |vendor|sales|percent|
--------------------------
2009-03-10| 10 |42.50| 4.25 |
2009-03-10| 18 |44.50| 4.45 |
2009-03-10| 21 |32.50| 3.25 |
I can get this done but I cannot get it to give me the totals for each separate table like:
date |vendor|sales|percent|
--------------------------
2009-03-03| 10 |13.50| 1.30 |
2009-03-03| 18 |53.50| 5.30 |
2009-03-03| 21 |23.50| 2.30 |
Total Sales for 2009-03-03 = $90.50
Total percent for 2009-03-03 = $8.90
date |vendor|sales|percent|
--------------------------
2009-03-10| 10 |42.50| 4.25 |
2009-03-10| 18 |44.50| 4.45 |
2009-03-10| 21 |32.50| 3.25 |
Total Sales for 2009-03-03 = $119.50
Total percent for 2009-03-03 = $11.95
I can get the totals for all but not for individual tables.
Here is my code:
<?php
$con = mysql_connect("localhost", $dbUser, $dbPassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("beans", $con);
$result = mysql_query("SELECT * FROM Deposits WHERE market = '4' ORDER BY eventdate, vendor ASC") or die(mysql_error());
$dateChk = 0;
while($row = mysql_fetch_array($result))
{
$date = $row["eventdate"];
$liclass = $row["vendor"];
$url = $row["trxid"];
$amountdep = $row["amount"];
$depcheck = $row["checkno"];
$deposit_Total = $deposit_Total + $amountdep;
$deposit_3Total = $deposit_3Total + $depcheck;
$deposit_3 = $amountdep / 100;
$dep_percent = $deposit_3 * 3;
$depper_Total = $depper_Total + $dep_percent;
$week = date("W", db_date_to_timestamp($date));
$year = date("Y", db_date_to_timestamp($date));
If($dateChk != $week)
{
echo "<table class=\"adverts\" width=\%100\" cellpadding=\"4\">\n";
echo "<tr><th>Date</th><th>Vendor</th><th>Total Sales</th><th>3% Due</th><th>Week</th></tr>\n";
echo "<tr>";
echo "<td>$date</td>\n";
echo "<td>$liclass</td>\n";
echo "<td>$ $amountdep</td>\n";
echo "<td>$ $depcheck</td>\n";
echo "<td>$week</td>\n";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>$date</td>\n";
echo "<td>$liclass</td>\n";
echo "<td>$ $amountdep</td>\n";
echo "<td>$ $depcheck</td>\n";
echo "<td>$week</td>\n";
echo "</tr>";
}
$dateChk = $week;
}
echo "</table>\n";
echo "<p><b>Total reported Market Sales are $ " . $deposit_Total . "</b></p>\n";
echo "<p><b>3 percent of Total reported Market Sales are $ " . $deposit_3Total . "</b></p>\n";
?>
(EDIT: sorry when I posted I saw that you need it in the query!)
$dateChk = 0;
$firstRow = 1;
while($row = mysql_fetch_array($result))
{
$date = $row["eventdate"];
$liclass = $row["vendor"];
$url = $row["trxid"];
$amountdep = $row["amount"];
$depcheck = $row["checkno"];
$deposit_Total = $deposit_Total + $amountdep;
$deposit_3Total = $deposit_3Total + $depcheck;
$deposit_3 = $amountdep / 100;
$dep_percent = $deposit_3 * 3;
$depper_Total = $depper_Total + $dep_percent;
$week = date("W", db_date_to_timestamp($date));
$year = date("Y", db_date_to_timestamp($date));
If($dateChk != $week)
{
if($firstRow == 0)
{
echo "</table>\n";
echo "<p><b>Total reported Market Sales are $ " . $deposit_week_Total . "</b></p>\n";
echo "<p><b>3 percent of Total reported Market Sales are $ " . $deposit_week_3Total . "</b></p>\n";
$deposit_week_Total = 0;
$deposit_week_3Total = 0;
}else
{
$firstRow = 0;
}
echo "<table class=\"adverts\" width=\%100\" cellpadding=\"4\">\n";
echo "<tr><th>Date</th><th>Vendor</th><th>Total Sales</th><th>3% Due</th><th>Week</th></tr>\n";
echo "<tr>";
echo "<td>$date</td>\n";
echo "<td>$liclass</td>\n";
echo "<td>$ $amountdep</td>\n";
echo "<td>$ $depcheck</td>\n";
echo "<td>$week</td>\n";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>$date</td>\n";
echo "<td>$liclass</td>\n";
echo "<td>$ $amountdep</td>\n";
echo "<td>$ $depcheck</td>\n";
echo "<td>$week</td>\n";
echo "</tr>";
}
$dateChk = $week;
$deposit_week_Total = $deposit_week_Total + $amountdep;
$deposit_week_3Total = $deposit_week_3Total + $depcheck;
}
echo "</table>\n";
echo "<p><b>Total reported Market Sales are $ " . $deposit_Total . "</b></p>\n";
echo "<p><b>3 percent of Total reported Market Sales are $ " . $deposit_3Total . "</b></p>\n";
?>
SQL
"SELECT date, SUM(sales) FROM Deposits WHERE market = '4' GROUP BY date"
Istead of sum(sales) you have to use your own calculations. I suggest to put them into a model.
Would this help?
http://www.plus2net.com/sql_tutorial/sql_sum.php
SELECT SUM (sales) FROM `table_name` WHERE `date` = '$date_you_want'
From my below comment:
SELECT SUM (sales) FROM `table_name` WHERE `date` <= '$start_date_of_week' AND `date` >= '$end_date_of_week'
So basically WHERE date is less than and greater than the two dates that describe the start and stop of the week you want...
Or... I think you could also add the days to the value in the mysql
SELECT SUM (sales) FROM table_name WHERE date LIKE '$yyyy-mm-dd %' ...DATE_ADD code for adding days.. or INTERVAL.
You can get those sums straight by using GROUP BY WITH ROLLUP.

Categories