Jump to content

Loop Condition with if statment


fife

Recommended Posts

I have a while loop that works fine but when I add an if condition within the loop it does not echo the if condition.  I'm trying to echo an extra row in the table if the number of clubs returned is > 2

<?php 
	for($i = 0; ($ClubDetails1 =  mysql_fetch_assoc($ClubDetails)) && $i < 2; $i++){  ?>
  <tr>
    <th scope="col"></th>
    <th scope="col"><?php echo $ClubDetails1['name']; ?></th>
  <?php
    if ($i > 2) {
  echo "<th scope=\"col\"><a href=\"club_page.php\"> More </a> </th>";	
}
?>
  </tr>
  <?php } ?>

 

Link to comment
Share on other sites

I thought that might be the case.  The thing is im trying to limit the amount to 2 but at the same time if the total count is greater than 2 in the first place to echo a more button. Ive even tried;


<?php 
	for($i = 0; ($ClubDetails1 =  mysql_fetch_assoc($ClubDetails)) && $i < 2; $i++ ){  ?>
  <tr>
    <th scope="col"></th>
    <th scope="col"><?php echo $ClubDetails1['name']; ?></th>
  <?php
   for($i = 0; ($ClubDetails1 =  mysql_fetch_assoc($ClubDetails)) && $i  > 2; $i++ ) {
  echo "<th scope=\"col\"><a href=\"index.php\"> More </a> </th>";	
}

?>
  </tr>
  <?php   } ?>


 

But obviously its not working either.  What do I need to read about to do this or does anybody know of any examples I can learn from?

Link to comment
Share on other sites

I see.  I have now changed the code so the loop is kept separate.  Still its still not working.  I know there is less than 4 results from a count.  I now have an if statement and a separate query for the more button.  My if statement seems to be printing the more button even though the result of the query is less than 4.

<?php 

	for($i = 0; ($ClubDetails1 =  mysql_fetch_assoc($ClubDetails)) && $i < 2; $i++ ){  ?>
  <tr>
    <th scope="col"></th>
    <th scope="col"><?php echo $ClubDetails1['name']; ?></th>
  <?php
}
     	$qMyClubA = mysql_query("SELECT memberID FROM clubs WHERE memberID =".$User['memberID']."");
$num = mysql_fetch_array($qMyClubA);
 if (count($num)>4) {
"<th scope=\"col\"><a href=\"index.php\"> More </a> </th>";	
}
else{ echo" ";}
?>

 

Link to comment
Share on other sites

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.