Shadowing Posted January 25, 2012 Share Posted January 25, 2012 Hey guys, I'm trying to make this loop 5 times reguardless if there is less then 5 rows being retrieved from the data base. The reason for this is so it returns empty html rows so i can CSS color the rows. Otherwise the rows wont get echo as empty. <tr class="odd"> <th>Time Sent</th> <?php $x = 0; $travel3 = "SELECT * FROM travel WHERE attacker_id= '".($_SESSION['user_id'])."' AND stance= 2 ORDER BY sent_time DESC LIMIT 5"; $travel2 = mysql_query($travel3) or trigger_error("SQL", E_USER_ERROR); while ($travel1 = mysql_fetch_assoc($travel2) && $x < 5) { $sent_time = $travel1['sent_time']; echo "<td>$sent_time </td>"; ++$x; }?> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/ Share on other sites More sharing options...
dzelenika Posted January 25, 2012 Share Posted January 25, 2012 while ($travel1 = mysql_fetch_assoc($travel2) || $x < 5) { Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1310977 Share on other sites More sharing options...
Shadowing Posted January 25, 2012 Author Share Posted January 25, 2012 Thanks dzelenika that solved that issue kinda. That cause it to keep repeating 5 times. Not sure how im going to figure out how to get it to produce blank <tr> after it grabs less then 5 from the data base. Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1310989 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2012 Share Posted January 25, 2012 Make your while(){} loop only for the mysql data. Increment the $x variable inside the while(){} loop. Add another while loop after the end of the first while loop that loops until $x is == 5. The second while loop will output anything you want to finish the row. Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1310992 Share on other sites More sharing options...
dzelenika Posted January 25, 2012 Share Posted January 25, 2012 I'm not sure that I understand you but try to modify your echo to this echo "<td>$sent_time </td>"; Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1310995 Share on other sites More sharing options...
Shadowing Posted January 25, 2012 Author Share Posted January 25, 2012 This picture is when i had the OR statement in their I kinda got what you mean. idk if my 2nd loop isnt working in this or if my html is off on finishing the table lol. <tr class="odd"> <th>Time Sent</th> <?php while ($travel1 = mysql_fetch_assoc($travel2)) { $sent_time = $travel1['sent_time']; echo "<td>$$sent_time</td>"; } $x = 0; while ($x == 5) { ?> <tr><td></td> <?php ++$x; } ?> </tr> shouldnt both my while loop brackets be at the end? Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311004 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2012 Share Posted January 25, 2012 ...that loops until $x is == 5. No one said to loop while $x is == 5. Those are logically two different things. You might want to loop while $x is < 5 Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311006 Share on other sites More sharing options...
Shadowing Posted January 25, 2012 Author Share Posted January 25, 2012 lol my bad. ok i got it looping now but its in a contineous loop. I dont even see how thats a continous loop. hmm. other then that problem. How do I get the first loop to update x also? cause it would have to add in how many it updated already right? <tr class="odd"> <th>Time Sent</th> <?php $x = 0; while ($travel1 = mysql_fetch_assoc($travel2)) { $sent_time = $travel1['sent_time']; echo "<td>$$sent_time</td>"; } while ($x < 5) { ?> <tr><td></td> <?php ++$x; } ?> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311010 Share on other sites More sharing options...
Shadowing Posted January 25, 2012 Author Share Posted January 25, 2012 Alright im getting much closer I got it to repeat once <tr class="odd"> <th>Time Sent</th> <?php $x = 0; $travel3 = "SELECT * FROM travel WHERE attacker_id= '".($_SESSION['user_id'])."' AND stance= 2 ORDER BY sent_time DESC LIMIT 5"; $travel2 = mysql_query($travel3) or trigger_error("SQL", E_USER_ERROR); while ($travel1 = mysql_fetch_assoc($travel2)) { $sent_time = $travel1['sent_time']; echo "<td>$sent_time </td>"; if($x < 5) { ?> <td></td> <?php ++$x; }}?> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311012 Share on other sites More sharing options...
Shadowing Posted January 25, 2012 Author Share Posted January 25, 2012 nice i got it working now, thanks a bunch PFMaBiSmAd just had to change the if back to WHILE i just did the if statement to make sure the html was correct first. What is wierd i swear i tried the exact code i am using right now and it had me in a continous loop but obviously something was differant. I hate when continously loops happend cause then I cant see whats going on. Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311013 Share on other sites More sharing options...
Shadowing Posted January 25, 2012 Author Share Posted January 25, 2012 ahh i spoke to soon. so far i have it repeating but its just repeating 5 times its not adding onto the x for each row i pull from the data base. Another thing is that the 2nd record I pull from the data base adds it after the blank rows Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311015 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2012 Share Posted January 25, 2012 so far i have it repeating but its just repeating 5 times its not adding onto the x for each row i pull from the data base. Another thing is that the 2nd record I pull from the data base adds it after the blank rows ^^^ That whole statement means zero to us because we are not standing right next to you and so don't know what the 'it' is that you keep mentioning. What ever 'it' is, please don't put SELECT queries inside of loops. You will end up with a game that performs so poorly that only about 5 concurrent people can play it before it slows down to the point that no one will want to play it. Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311022 Share on other sites More sharing options...
Shadowing Posted January 25, 2012 Author Share Posted January 25, 2012 Thanks PFMaBiSmAd ok i moved the bracket out of the loop. idk why it wasnt working earlier. maybe it was cause I didnt have the html right at the time. yah i dont have any loops inside loops anywhere in my game I was wondering that when i was doing it cause i know you mention that to me before in the past. The below is the return for the picture above. so right now its echoing 5 blanks reguardless of how many rows im fetching from the database. What has me puzzled if I update X inside the first loop my 2nd loop wont read the update right since its inside the brackets of the first loop? <tr class="odd"> <th>Time Sent</th> <?php $x = 0; $travel3 = "SELECT * FROM travel WHERE attacker_id= '".($_SESSION['user_id'])."' AND stance= 2 ORDER BY sent_time DESC LIMIT 5"; $travel2 = mysql_query($travel3) or trigger_error("SQL", E_USER_ERROR); while ($travel1 = mysql_fetch_assoc($travel2)) { $sent_time = $travel1['sent_time']; $time = ($sent_time + $time_offset); echo "<td>$user_offset</td>"; } while($x < 5) { ?> <td></td> <?php ++$x; }?> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311029 Share on other sites More sharing options...
Shadowing Posted January 25, 2012 Author Share Posted January 25, 2012 Alright I figured it out. I think im just tired. I just had to add a 2nd ++$x; makes sense. <tr class="odd"> <th>Time Sent</th> <?php $x = 0; $travel3 = "SELECT * FROM travel WHERE attacker_id= '".($_SESSION['user_id'])."' AND stance= 2 ORDER BY sent_time DESC LIMIT 5"; $travel2 = mysql_query($travel3) or trigger_error("SQL", E_USER_ERROR); while ($travel1 = mysql_fetch_assoc($travel2)) { $sent_time = $travel1['sent_time']; echo "<td>$ $sent_time </td>"; ++$x; } while($x < 5) { ?> <td></td> <?php ++$x; }?> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/#findComment-1311051 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.