473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

question about select query

there is a report which displays the enquiries made by users in the below mentioned format.
the count column refers to how many number of times an enquiry was made for a particular city
Expand|Select|Wrap|Line Numbers
  1. count    city                price 
  2.  
  3. 2        sydney         $100
  4. 1        perth          $200
  5. 2        melbourne      $300
  6.  
the total value of the enquiries should be $1000 ( 2 X $100 + 1 X $200 + 2 X $300) presently i am able to add the price column which displays $600 actually it should be $1000

following is the code presently
Expand|Select|Wrap|Line Numbers
  1. mysql_select_db($database_connEnquiry, $connEnquiry);
  2.  
  3. $sql = " SELECT Count(*) as Counts, Title, City, Price, FROM `enquiry` WHERE date_format(en_date,'%Y-%m-%d') BETWEEN '" . $startDate . "' AND '" . $endDate . "' " . "
  4. Group By Title, City, Price Order By Counts Desc, Title Desc, mode, Price";
  5.  
  6. $rs_newEnquiries = mysql_query($sql, $connEnquiry) or die(dbError(query1));
  7.  
  8. $echoStr = "<table width='80%' cellpadding=1 cellspacing=1 border=0><tr bgcolor='#cccccc;'><th bgcolor='#cccccc'
  9. width=10>Count</th><th bgcolor='#cccccc' width=400>Destination</th><th bgcolor='#cccccc' width=100>Direct
  10. Enquiries</th><th bgcolor='#cccccc' width=100>Mode</th> <th bgcolor='#cccccc' width=50>Priced From</th>
  11. <th bgcolor='#cccccc' width=35>numAdults</th> <th bgcolor='#cccccc' width=35>numChildren</th> <th bgcolor='#cccccc' width=30>numInfants</th> <th align='center' 
  12.  
  13. bgcolor='#cccccc'>Story</th>  </tr>";
  14.  
  15.                 $total = 0; $totalprice = 0;
  16.  
  17. while ($row_rs_newEnquiries = mysql_fetch_assoc($rs_newEnquiries))
  18. {
  19.                                         $echoStr .= "
  20.                                         <tr bgcolor='#ebebeb' class='default'><td>".$row_rs_newEnquiries['Counts']."</td>
  21.                                         <td class='default'>&nbsp; ".$row_rs_newEnquiries['Title']."</td>
  22.                                         <td class='default'>&nbsp; ".$row_rs_newEnquiries['City']."</td>
  23.                                         <td class='default'>&nbsp; ".$row_rs_newEnquiries['Price']."</td>
  24.                                         </tr>";
  25.                                         $total = $total + $row_rs_newEnquiries['Counts'];
  26.  
  27. $totalprice = $totalprice + $row_rs_newEnquiries['Price'];
  28. }
  29.  
  30. $echoStr .= "<tr bgcolor='#cccccc;'><th align=\"left\" bgcolor='#cccccc'><b>".$total."</b></th>
  31. <th bgcolor='#cccccc'>Total</th>
  32.  
  33. <td bgcolor='#cccccc'>&nbsp;</td><td bgcolor='#cccccc'></td><td bgcolor='#cccccc'><b>$&nbsp;".$totalprice."</b> </td><td bgcolor='#cccccc'><b>".$totalnumadults."</b></td><td 
  34.  
  35. bgcolor='#cccccc'><b>".$totalnumchildren."</b></td><td bgcolor='#cccccc'><b>".$totalnuminfants."</b></td><td bgcolor='#cccccc'></td></tr></table>";
  36.  
With the above code i am able to add the value of the price and display the $ amount. however if the count for a city is for example 2 which means there were 2 enquiries for

example city sydney and the price is $100 then the total should be 2 X $100 which is $200 how can i change the above above code in a way that the $totalprice will be able to
1) multiply the numeric value in count column with the price column and similarly for all the rows
2) and add the $ amount in the price column and sum it up and be able to display in echo $totalprice;

please advice.

thanks
Jan 19 '08 #1
2 861
I'm being lazy and not bothering to fully read your code, so forgive me if I don't get this quite right..

[PHP]$total = 0; $totalprice = 0;//this isn't really necessary, though it wont hurt

while ($row_rs_newEnquiries = mysql_fetch_assoc($rs_newEnquiries))
{
$total_price_for_this_row = $row_rs_newEnquiries['Counts'] * $row_rs_newEnquiries['Price'];

$echoStr .= "
<tr bgcolor='#ebebeb' class='default'><td>".$row_rs_newEnquiries['Counts']."</td>
<td class='default'>&nbsp; ".$row_rs_newEnquiries['Title']."</td>
<td class='default'>&nbsp; ".$row_rs_newEnquiries['City']."</td>
<td class='default'>&nbsp; ".$row_rs_newEnquiries['Price']."</td>
<td class='default'>&nbsp; ". $total_price_for_this_row ."</td>
</tr>";

$totalprice =+ $total_price_for_this_row;//add this row's total to the overall total

$total = $total + $row_rs_newEnquiries['Counts'];//this isn't needed unless you want a total for the number of counts
}[/PHP]

Note the use of +=
$a +=$b; is like saying $a = $a+$b;

I hope that's what you were looking for
Jan 19 '08 #2
Atli
5,058 Expert 4TB
Hi runway27.

Please use [code] tags when posting your code examples. (See How to ask a question)

[code=php] ...PHP code goes here... [/code]

Thank you.
Moderator
Jan 19 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cwhite | last post by:
Hi I have another drop list question. I have a table like this: computer dell computer ibm computer hp
4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
4
by: Lauren Quantrell | last post by:
In my old MDB databases, I constructed a lot of "subQueries" to filter out records, then based a new query on the subQuery. This results in huge speed increases on large datasets. However... In...
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
10
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social...
5
by: ric_deez | last post by:
Hi there, I would like to create a simple search form to allow users to search for a job number based on a number of parameters. I think I understand how to use parameteres associated with Stored...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.