simeonC Posted June 18, 2013 Share Posted June 18, 2013 Im trying to loop a variable to extract each customer out the database however it just repeats the same customer any help would be greatly appreciated. $completed_orders=mysql_query("SELECT * FROM repair_orders JOIN (serviced_vechicles,Customers) ON repair_orders.car_id=serviced_vechicles.car_id AND Customers.customer_id=serviced_vechicles.customer_id WHERE repair_status='0' ORDER BY customer_name DESC"); $existingCustomers=mysql_num_rows($completed_orders); echo $existingCustomers; $fetch_complete=mysql_fetch_array($completed_orders); $customer=$fetch_complete; $requested_customers = '<a href="">'; '<tr> <td>'.$customer['customer_name'].'</td> <td>'.$customer['year'].'</td> <td>'.$customer['make'].'</td>< td>'.$customer['model'].'</td> <td>'.$customer['date'].'<td> </tr>'; '</a>'; How do I loop through ^^^^^ $requested_customers Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/ Share on other sites More sharing options...
kicken Posted June 18, 2013 Share Posted June 18, 2013 (edited) You use a while loop around your mysql_fetch_array code. while ($customer = mysql_fetch_array($completed_orders)){ $requested_customers = '<a href=""> <tr> <td>'.$customer['customer_name'].'</td> <td>'.$customer['year'].'</td> <td>'.$customer['make'].'</td> <td>'.$customer['model'].'</td> <td>'.$customer['date'].'<td> </tr> </a>'; //Do something with $requested_customers } Note that your HTML is not valid either, you cannot put an <a> tag around a table row. You'd need to link each cell separately. Edited June 18, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436612 Share on other sites More sharing options...
simeonC Posted June 18, 2013 Author Share Posted June 18, 2013 I ended up linking each cell seperately that such a bother hopefully we can get that updated.. however I tried the while loop and the do while loop I am still getting 1 row output.. I used the mysql_num_rows function and it counts two rows for the result set. So now what do I do. Should I be using the mysqli extension. I am really lost Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436616 Share on other sites More sharing options...
kicken Posted June 18, 2013 Share Posted June 18, 2013 Are generating your output from inside the while loop's body, or after it? If you want output for each row, you need to generate that output inside the loop body (ie, where my //Do something comment is) Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436627 Share on other sites More sharing options...
simeonC Posted June 18, 2013 Author Share Posted June 18, 2013 (edited) its inside the loop body.. Now i recieve output but the two rows that are generated are exactly the same.. but when queried in phpmyadmin I get two different rows Edited June 18, 2013 by simeonC Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436631 Share on other sites More sharing options...
simeonC Posted June 18, 2013 Author Share Posted June 18, 2013 Still having issues with my while{} loop and do{} while() loop I want it to run an output of different row for each result set sent back from the mysql database. However it is remaining stagnent with same result even though mysql_num_rows function shows two result sets set back. Now I am lost here is the code again please help me. <?php session_start();?> <?php $con=mysql_connect('localhost','root','root'); $db_select=mysql_select_db('total',$con); $completed_orders=mysql_query("SELECT * FROM repair_orders JOIN (serviced_vechicles,Customers) ON repair_orders.car_id=serviced_vechicles.car_id AND Customers.customer_id=serviced_vechicles.customer_id WHERE repair_status='0' ORDER BY customer_name DESC"); $existingCustomers=mysql_num_rows($completed_orders); if ($existingCustomers<1){ $message= 'You do not have any complete vehicles in your shop';}else{ $fetch_complete=mysql_fetch_array($completed_orders); do{ $requested_customers = '<tr> <td>'.$customer['customer_name'].'</td> <td>'.$customer['year'].'</td> <td>'.$customer['make'].'</td> <td>'.$customer['model'].'</td> <td>'.$customer['date'].'<td> </tr>'; //Do something with $requested_customers }while ($customer = mysql_fetch_array($completed_orders)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436667 Share on other sites More sharing options...
kicken Posted June 18, 2013 Share Posted June 18, 2013 You use a while loop, not a do/while loop. Start with the code I gave you above and then extend it. You do not need your mysql_num_rows line or that initial $fetch_customer line. The while loop would begin just after your mysql_query call. Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436669 Share on other sites More sharing options...
ginerjm Posted June 18, 2013 Share Posted June 18, 2013 while ($completed_customers = MySQL_fetch_array($completed_orders) { (build your heml) } echo </table> echo your accumulated html BTW - in your loop you are re-initializing $requested_customers every time, so you are probably only seeing the very last customer anyway. s/b "$requested_customers .= blah blah blah; Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436670 Share on other sites More sharing options...
simeonC Posted June 18, 2013 Author Share Posted June 18, 2013 Dude I hate to be a bother I have tried the variabe still gives me 1 output. may i post entire code here for you to take a look. please i do need help.. Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436671 Share on other sites More sharing options...
Solution ginerjm Posted June 18, 2013 Solution Share Posted June 18, 2013 That is definitely the lack of using "dot equals" as you build your output string. Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436672 Share on other sites More sharing options...
simeonC Posted June 18, 2013 Author Share Posted June 18, 2013 Thank you I love this forum I used the dot equals before but I kept re-initializing the variable. as suchVVVVVVV while ($customer = mysql_fetch_array($completed_orders)){ $requested_customers = '<tr>'; $requested_customers .='<td>'.$customer['customer_name'].'</td>'; $requested_customers .='<td>'.$customer['year'].'</td>'; $requested_customers .='<td>'.$customer['make'].'</td>'; $requested_customers .='<td>'.$customer['model'].'</td>'; $requested_customers .='<td>'.$customer['date'].'<td>'; $requested_customers .='</tr>'; //Do something with $requested_customers } VVVCorrect way for anyone with similiar problem novice mistake use dot equal from beginningVVV while ($customer = mysql_fetch_array($completed_orders)){ $requested_customers .= '<tr>'; $requested_customers .='<td>'.$customer['customer_name'].'</td>'; $requested_customers .='<td>'.$customer['year'].'</td>'; $requested_customers .='<td>'.$customer['make'].'</td>'; $requested_customers .='<td>'.$customer['model'].'</td>'; $requested_customers .='<td>'.$customer['date'].'<td>'; $requested_customers .='</tr>'; //Do something with $requested_customers } Quote Link to comment https://forums.phpfreaks.com/topic/279311-looping-variable/#findComment-1436676 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.