robert_gsfame Posted October 27, 2009 Share Posted October 27, 2009 guys, i got confused with Looping...this is the case I have 3 stocks, let say "Stock A", "Stock B", "Stock C" and the owner of each stock A --> Mr.James stock B --> Mr.Barry Stock C --> Mr. James So A & B belong to Mr. James...and C belong to Mr Barry, now i want to display every stocks that belongs to Mr James and Mr Barry separately How to do it using While?? really confused with this..thx in advance Quote Link to comment https://forums.phpfreaks.com/topic/179157-confused-with-loop/ Share on other sites More sharing options...
trq Posted October 27, 2009 Share Posted October 27, 2009 Post your problematic code. Quote Link to comment https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945204 Share on other sites More sharing options...
robert_gsfame Posted October 27, 2009 Author Share Posted October 27, 2009 <?php $query="SELECT * FROM table'"; $myquery=mysql_query($query); $num=mysql_num_rows($myquery); if(!empty($num)){ while($find=mysql_fetch_assoc($myquery)){ $stock = $find['stock']; $owner = $find['owner']; --------------PROBLEM STARTS HERE } I want to have this as result after ECHO Record of stocks of Mr. Barry ---------------------------- Stock B Record of stocks of Mr. James ---------------------------- Stock A Stock C Quote Link to comment https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945207 Share on other sites More sharing options...
sasa Posted October 27, 2009 Share Posted October 27, 2009 try <?php $query="SELECT * FROM table ORDER BY `owner`'"; $myquery=mysql_query($query); $num=mysql_num_rows($myquery); $cur_name = ''; if(!empty($num)){ while($find=mysql_fetch_assoc($myquery)){ $stock = $find['stock']; $owner = $find['owner']; if ($cur_name != $owner){ $cur_name = $owner; echo "Record of stocks of $owner<br />\n"; echo "________________________________<br />\n"; } echo "Stock $stock<br />\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945209 Share on other sites More sharing options...
robert_gsfame Posted October 27, 2009 Author Share Posted October 27, 2009 it didn't work Quote Link to comment https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945311 Share on other sites More sharing options...
Adam Posted October 27, 2009 Share Posted October 27, 2009 ... how did it not work? Did you get an error? Blank screen? Quote Link to comment https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945313 Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2009 Share Posted October 27, 2009 There is an extra single-quote at the end of your original query that is breaking the syntax of the sql. Quote Link to comment https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945319 Share on other sites More sharing options...
robert_gsfame Posted October 27, 2009 Author Share Posted October 27, 2009 yes, blank screen! i tried my code and it works <?php $query="SELECT DISTINCT owner FROM table'"; $myquery=mysql_query($query); $num=mysql_num_rows($myquery); if(!empty($num)){ while($find=mysql_fetch_assoc($myquery)){ $owner= $find['owner']; echo $owner; echo "<br>"; $query2="SELECT * FROM table WHERE owner='$owner'"; $myquery2=mysql_query($query2); $num2=mysql_num_rows($myquery2); if($num2>1){ while($find2=mysql_fetch_assoc($myquery2)){ $stock=$find2['stock']; echo $stock; echo "<br>";}}else if($num2==1){ $find3=mysql_fetch_array($myquery2); $stock2=$find3['stock']; echo $stock2; } } } how's the code? Quote Link to comment https://forums.phpfreaks.com/topic/179157-confused-with-loop/#findComment-945376 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.