views:

226

answers:

4

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";
?>
A: 

(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";
?>
najmeddine
returns blank screen
well, I think there was a missing ';' line 25 [fixed]. Did you get any error message? How is error_reporting (in your php.ini) set?
najmeddine
EXACTLY WHAT I WAS LOOKING FOR!!! THANKS A MILLION!
A: 

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.

erenon
doesn't work - it groups some but does not return all of my records.
@Tylee: you have to group by the products as well.
erenon
A: 

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.

ian
this is a running table based on week of year so I don't want to specify every week or date.
So you want to select say week 1 of the year week 2 of the year ect?You should be able to figure out that range and then use it in the query. Maybe with a LIKE. I use this query in a page I just made."SELECT * FROM `schedule_items` WHERE `start` LIKE '$ymd %'"Where `start` is a full DATETIME field and $ymd is a YYYY-MM-DD value and % is a wild card so it finds all the values that match the YYYY-MM-DD I think you could probably apply a range to this somehow..SELECT SUM (sales) FROM `table_name` WHERE `date` <= $start_date_of_week AND `date` >= $end_date_of_week
ian
A: 

You can get those sums straight by using GROUP BY WITH ROLLUP.

raspi