fife Posted February 19, 2011 Share Posted February 19, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228219-loop-condition-with-if-statment/ Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2011 Share Posted February 19, 2011 The loop only runs while $i < 2, therefore $i will never be > 2. Quote Link to comment https://forums.phpfreaks.com/topic/228219-loop-condition-with-if-statment/#findComment-1176874 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/228219-loop-condition-with-if-statment/#findComment-1176876 Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2011 Share Posted February 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228219-loop-condition-with-if-statment/#findComment-1176887 Share on other sites More sharing options...
fife Posted February 19, 2011 Author Share Posted February 19, 2011 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" ";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/228219-loop-condition-with-if-statment/#findComment-1176893 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.