Jump to content


Photo

Group and Total some data [Solved]


  • Please log in to reply
5 replies to this topic

#1 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 12 October 2006 - 07:29 PM

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
<?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());


?>

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)



#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 12 October 2006 - 07:52 PM

I have used this in the past. You can change the fields you need.
<?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>';
}
?>

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

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 October 2006 - 07:56 PM

SELECT eventID, TicketType, SUM(Qty) as total
FROM TicketsSold 
GROUP BY eventID, TicketType

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 12 October 2006 - 08:43 PM

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:

<?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());


?>


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 October 2006 - 09:07 PM

Clauses in wrong sequence
<?php
$query = "SELECT EventID, TicketType, SUM(Qty) as total FROM TicketsSold 
WHERE EventID = '$event'
GROUP BY EventID, TicketType  ";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 13 October 2006 - 07:14 AM

Thanks Barand your a genius! that works excellently, also thanks to craygo for your input




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users