156418 Posted October 12, 2006 Share Posted October 12, 2006 I have a table in my database called TicketsSold.in that I have thefFields: Refrence, OrderID, EventID, TicketType and QtyI 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) Link to comment https://forums.phpfreaks.com/topic/23786-group-and-total-some-data-solved/ Share on other sites More sharing options...
craygo Posted October 12, 2006 Share Posted October 12, 2006 I have used this in the past. You can change the fields you need.[code]<?php// Print Headersprint '<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 Totalif ($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 Nameprint '<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 colorif ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF";} else { $bgcolor = "#E0E0E0";}// Print Database Detailsprint '<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 messageprint '<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 itRay Link to comment https://forums.phpfreaks.com/topic/23786-group-and-total-some-data-solved/#findComment-108043 Share on other sites More sharing options...
Barand Posted October 12, 2006 Share Posted October 12, 2006 [code]SELECT eventID, TicketType, SUM(Qty) as totalFROM TicketsSold GROUP BY eventID, TicketType[/code] Link to comment https://forums.phpfreaks.com/topic/23786-group-and-total-some-data-solved/#findComment-108046 Share on other sites More sharing options...
156418 Posted October 12, 2006 Author Share Posted October 12, 2006 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] Link to comment https://forums.phpfreaks.com/topic/23786-group-and-total-some-data-solved/#findComment-108075 Share on other sites More sharing options...
Barand Posted October 12, 2006 Share Posted October 12, 2006 Clauses in wrong sequence[code]<?php$query = "SELECT EventID, TicketType, SUM(Qty) as total FROM TicketsSold WHERE EventID = '$event'GROUP BY EventID, TicketType ";?>[/code] Link to comment https://forums.phpfreaks.com/topic/23786-group-and-total-some-data-solved/#findComment-108083 Share on other sites More sharing options...
156418 Posted October 13, 2006 Author Share Posted October 13, 2006 Thanks Barand your a genius! that works excellently, also thanks to craygo for your input Link to comment https://forums.phpfreaks.com/topic/23786-group-and-total-some-data-solved/#findComment-108208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.