Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/255738-little-help-with-a-loop-please/
Share on other sites

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.

This picture is when i had the OR statement in their

table.jpg

 

 

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?

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> 

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>

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.

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

 

table2.jpg

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.

table3.jpg

 

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> 

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>

 

table4.jpg

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.