Jump to content

Archived

This topic is now archived and is closed to further replies.

156418

Group and Total some data [Solved]

Recommended Posts

I have a table in my database called TicketsSold.

in that I have thefFields: Refrence, OrderID, EventID, TicketType and Qty

I can put together a query no problem which can list the tickets sold for a certain event.

i.e [code]<?php
$event = mysql_real_escape_string(trim(stripslashes($_POST['event'])));
$query = "SELECT * FROM TicketsSold WHERE EventID = '$event'";

$results = mysql_query($query) or die(mysql_error());


?>[/code]

But, is there a way that I can instead of listing eveything, total up everything into groups.

For example have it list by EventID then total the amount sold in each ticket type, Adult, Child, Senior, Family and Present.


Many thanks for any help.
(if this would be better in the MySQL Forum then sorry, as I was unaware what it would come under)

Share this post


Link to post
Share on other sites
I have used this in the past. You can change the fields you need.
[code]<?php
// Print Headers
print '<table width="650" align="center" cellspacing=0 cellpadding=0>';
print '<tr>';
print '<td class=reportheads colspan=4 height=50 valign=middle align=center>Daily Bookings for '.$bd.'</td>';
print '</tr>';
print '<tr bgcolor="F98F5B">';
print '<td width=300 align=center>Customer Name</td>';
print '<td width=75 align=center>Cust Type</td>';
print '<td width=125 align=center>Job Number</td>';
print '<td width=150 align=center>Totals</td>';
print '</tr>';
print '</table>';
// Set initial group values
$lastrep = '';
$stot = 0;
$gtot = 0;
// Query database
$dbook = "SELECT SUM(orderdetails.QtyOrdered*orderdetails.UnitPrice) AS totals, fname, cname, ctype, summary.orderid AS jobnumb ";
$dbook .= "FROM summary, orderdetails ";
$dbook .= "WHERE summary.orddate = '$bdate' AND summary.orderid = orderdetails.OrderID ";
$dbook .= "GROUP BY fname, jobnumb";
  $dbookres = mysql_query($dbook);
  $num_rows = mysql_num_rows($dbookres);
if($num_rows > 1){
// Set initial row color
$bgcolor = "FFFFFF";
    while ($dbrows = @mysql_fetch_array($dbookres)){
// Print Group Total
if ($dbrows['fname'] != $lastrep) {
        if ($lastrep != '') {
              print '<tr bgcolor="#C65EE4"><td colspan=3 align=right>'.$lastrep.'\'s Totals:</td><td align=right>$'.number_format($stot, 2).'</td></table>';
              $stot = 0;
}
// Print Group Name
print '<table width="650" align="center" cellspacing=0 cellpadding=0>';
print '<tr bgcolor=83EA44>';
print '<td colspan=4 align=left><strong>'.$dbrows['fname'].'\'s Sales</strong></td>';
print '</tr>';
print '</table>';
}
// Alternate row color
if ($bgcolor == "#E0E0E0"){
  $bgcolor = "#FFFFFF";
} else {
  $bgcolor = "#E0E0E0";
}
// Print Database Details
print '<table width=650 align=center cellspacing=0>';
print '<tr bgcolor='.$bgcolor.'>';
print '<td width=300 align=center>'.$dbrows['cname'].'</td>';
print '<td width=75 align=center>'.$dbrows['ctype'].'</td>';
print '<td width=125 align=center>'.$dbrows['jobnumb'].'</td>';
print '<td width=150 align=right>$'.number_format($dbrows['totals'], 2).'</td>';
print '</tr>';
// Reset group values
$stot += $dbrows['totals'];
$gtot += $dbrows['totals'];
$lastrep = $dbrows['fname'];
}
// Print final SubTotals
  print '<tr bgcolor="#C65EE4"><td colspan=3 align=right>'.$lastrep.'\'s Totals:</td><td align=right>$'.number_format($stot, 2).'</td></tr></table>';
  print '<table width=650 align=center cellspacing=0 cellpadding=0>';
// Print Grand Total
  print '<tr bgcolor="#35D3D9">';
  print '<td width=500 align=right>Total Sales:</td>';
  print '<td width=150 align=right>$'.number_format($gtot, 2).'</td>';
  print '</tr>';
  print '</table>';
} else {
// Print No data message
print '<table width=650 align=center>';
print '<tr>';
print '<td align=center><strong>NO BOOKINGS FOR '.$bd.'</strong></td>';
print '</tr>';
print '</table>';
}
?>[/code]

I hope you can figure it out. Didn't want to spend alot of time substituting your data in. But will help if you need it

Ray

Share this post


Link to post
Share on other sites
[code]
SELECT eventID, TicketType, SUM(Qty) as total
FROM TicketsSold
GROUP BY eventID, TicketType
[/code]

Share this post


Link to post
Share on other sites
Thanks Barand, that looks nice and simple, but I'm getting an error

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE EventID = '21'' at line 2"

This is my query: All of the fields are correct:

[code]<?php
$event = mysql_real_escape_string(trim(stripslashes($_POST['event'])));
$query = "SELECT EventID, TicketType, SUM(Qty) as total FROM TicketsSold
GROUP BY EventID, TicketType  WHERE EventID = '$event'";

$results = mysql_query($query) or die(mysql_error());


?>[/code]

Share this post


Link to post
Share on other sites
Clauses in wrong sequence
[code]<?php
$query = "SELECT EventID, TicketType, SUM(Qty) as total FROM TicketsSold
WHERE EventID = '$event'
GROUP BY EventID, TicketType  ";
?>
[/code]

Share this post


Link to post
Share on other sites
Thanks Barand your a genius! that works excellently, also thanks to craygo for your input

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.