phpretard Posted October 8, 2009 Share Posted October 8, 2009 I am trying to set up a loop that goes up to 10 and changes the query so instead of $male1mQ=mysql_query("select gender, level from athletes where gender='1' and level='1' "); $male1=mysql_num_rows($male1Q); $male2mQ=mysql_query("select gender, level from athletes where gender='2' and level='2' "); $male2=mysql_num_rows($male2Q); over and over up to 10 ... I am trying to put this in a loop and I'm stuck. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/176962-loop/ Share on other sites More sharing options...
Bricktop Posted October 8, 2009 Share Posted October 8, 2009 Hi phpretard, Something like this should do the trick: $level = 1; while ($level <= 10) { $male.$levelmQ=mysql_query("select gender, level from athletes where gender='$level' and level='$level' "); $male.$level=mysql_num_rows($male.$levelQ); $level++; } Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/176962-loop/#findComment-933040 Share on other sites More sharing options...
Glenugie Posted October 8, 2009 Share Posted October 8, 2009 $Number = "1"; while ($Number!="11") { $MaleMQ['$Number']=mysql_query("select gender, level from athletes where gender='$Number' and level='$Number' "); $Male['$Number']=mysql_num_rows($MaleMQ['$Number']); $Number = $Number + "1"; } Possibly, I couldn't think of another solution for the query variable names, but there is probably one out there. Hope this helps ~Glenugie~ Quote Link to comment https://forums.phpfreaks.com/topic/176962-loop/#findComment-933047 Share on other sites More sharing options...
Maq Posted October 8, 2009 Share Posted October 8, 2009 Why don't you just use a single query with BETWEEN and if you need the number of rows for the values 1-10 you can just use COUNT and GROUP BY level or gender. Quote Link to comment https://forums.phpfreaks.com/topic/176962-loop/#findComment-933050 Share on other sites More sharing options...
phpretard Posted October 8, 2009 Author Share Posted October 8, 2009 Why don't you just use a single query with BETWEEN and if you need the number of rows for the values 1-10 you can just use COUNT and GROUP BY level or gender. That sounds like it will work...I didn't think of it thank you. I haven't used that type of query before though. I understand BETWEEN, COUNT, and GROUP but puting them together hurts my brain Do you have an example I can look at? Quote Link to comment https://forums.phpfreaks.com/topic/176962-loop/#findComment-933054 Share on other sites More sharing options...
phpretard Posted October 8, 2009 Author Share Posted October 8, 2009 Why does this get stuck? $level = 1; while ($level <= 10) { // if I "echo $level" it counts to 10??? $maleQ.$level=mysql_query("select gender, level from athletes where gender='$level' and level='$level' "); $male.$level=mysql_num_rows($maleQ.$level); $level++; } Quote Link to comment https://forums.phpfreaks.com/topic/176962-loop/#findComment-933059 Share on other sites More sharing options...
kickstart Posted October 8, 2009 Share Posted October 8, 2009 Hi Suspect these 2 lines are corrupting $level when you assign to $maleQ.$level / $male.$level. $maleQ.$level=mysql_query("select gender, level from athletes where gender='$level' and level='$level' "); $male.$level=mysql_num_rows($maleQ.$level); I presume you are trying to assign to a variable called $male1 / $male2 / etc. If so it would be far easier to use arrays. That said it would be FAR easier to use Maqs suggestion of a single query using between. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/176962-loop/#findComment-933078 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.