lobfredd Posted May 6, 2012 Share Posted May 6, 2012 I am trying to create a order history page. I want it to look something like this: Ordernumber: 12 Products: Product 1 Product 2 Product 3 Product 4 Total: xx Ordernumber: 13 Products: Product 5 Product 6 Total: xx and so on. I have a recordset which get the ordernumbers from my database and i have another recordset which get the products (this recordset has a WHERE clause which is used to get the products associated with its ordernumber. mysql_select_db($database_lol, $lol); $query_ono = "SELECT DISTINCT ordre.ono FROM ordre WHERE ordre.bruker='{$_SESSION['MM_Username']}'"; $ono = mysql_query($query_ono, $lol) or die(mysql_error()); $row_ono = mysql_fetch_assoc($ono); $totalRows_ono = mysql_num_rows($ono); mysql_select_db($database_lol, $lol); $query_history = "SELECT ordre.vare FROM ordre WHERE ordre.ono='{$row_ono['ono']}'"; $history = mysql_query($query_history, $lol) or die(mysql_error()); $row_history = mysql_fetch_assoc($history); $totalRows_history = mysql_num_rows($history); This is the recordsets. this is my table: +-------+---------------------+------+ | ID | vare | ono | +-------+---------------------+------+ | 1 | Product 1 | 12 | | 2 | Product 2 | 12 | | 3 | Product 3 | 12 | | 4 | Product 4 | 12 | | 5 | Product 5 | 13 | | 6 | Product 6 | 13 | +-------+---------------------+------+ So my question is: How do i do this? list all the records i mean. Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 6, 2012 Share Posted May 6, 2012 You only want ONE query. You will then check for changes in the order number when processing the results and output the necessary content. You show a total in your example output but did not provide the field name that this would come from. So, I used a field called 'cost' in the query. Modify it as needed mysql_select_db($database_lol, $lol); $query = "SELECT ordre.ono, ordre.vare, order.cost FROM ordre WHERE ordre.bruker='{$_SESSION['MM_Username']}' ORDER BY ono"; $result = mysql_query($query_ono, $lol) or die(mysql_error()); $current_order_no = false while($row = mysql_fetch_assoc($result)) { //Check if this is a new order if($current_order_no !== $row['ono']) { if($current_order_no !== false) { //Display previous total echo "Total: {$total}<br><br>\n"; } $total = 0; $current_order_no = $row['ono']; echo "Ordernumber: {$row['ono']}<br>\n"; echo "Products:<br>\n"; } echo " {$row['ono']}<br>\n"; $total += $row['cost']; } Quote Link to comment Share on other sites More sharing options...
lobfredd Posted May 6, 2012 Author Share Posted May 6, 2012 Could you explain a little more please? im not very into coding yet. while($row = mysql_fetch_assoc($result)) DW states that there is a syntax error on this line. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2012 Share Posted May 6, 2012 DW states that there is a syntax error on this line There's nothing wrong with that line of php code. DW has never been very server-side code aware and frankly, you should not be dependent on your programming editor for the actual php syntax. What sort of problem did you get when you RAN that php code on your server? Quote Link to comment Share on other sites More sharing options...
lobfredd Posted May 6, 2012 Author Share Posted May 6, 2012 HTTP-feil 500 (Internal Server Error) This is the error i got Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 6, 2012 Share Posted May 6, 2012 Could you explain a little more please? im not very into coding yet. while($row = mysql_fetch_assoc($result)) DW states that there is a syntax error on this line. Many times syntax errors occur on some line before the reported error. It is just the line that the parser can no longer continue that it shows the line number. In this case the actual error was on the immediately preceding line where I forgot to end with a semi-colon. None of the code I provided is difficult/advanced. It is pretty basic logic. If you want more information I would ask tat you put some time into trying to understand it then ask specific questions about what you don't understand. Quote Link to comment Share on other sites More sharing options...
lobfredd Posted May 6, 2012 Author Share Posted May 6, 2012 Thanks, works great now! However, the last record's total is not displayed at all Here is a pic: it is always the last one, if i sort it the other way then it is still til one at the bottom. EDIT: just noticed that if there is only one record, then the total is gone too. Any suggestions? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 6, 2012 Share Posted May 6, 2012 Add this after the while loop echo "Total: {$total}<br><br>\n"; Quote Link to comment Share on other sites More sharing options...
lobfredd Posted May 6, 2012 Author Share Posted May 6, 2012 Thank you for everything! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.