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
https://forums.phpfreaks.com/topic/228219-loop-condition-with-if-statment/
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?

The problem you'll encounter is if any row has more columns than the others, then all of the others will need to have a <td> with a colspan= attribute added to bring the total to that of the row with the most columns for the markup to remain valid.

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" ";}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.