unemployment Posted April 19, 2011 Share Posted April 19, 2011 How can I limit my foreach to displaying three results? In addition, how can I make it say... if results == 1 do this if results == 2 do this and if results == 3 do this foreach($otherfans as $other) { $displayName = ucwords($other['firstname'].' '.$other['lastname']); ?> <a href="../u/<?php echo $other['username']; ?>"><?php echo $displayName; ?></a> <?php } Quote Link to comment https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/ Share on other sites More sharing options...
dcro2 Posted April 19, 2011 Share Posted April 19, 2011 I suppose you could keep count... $i = 0; foreach($otherfans as $other) { if($i==3) break; $i++; I think you're looking for a switch statement. Quote Link to comment https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/#findComment-1203324 Share on other sites More sharing options...
blacknight Posted April 19, 2011 Share Posted April 19, 2011 and to bring it all togeather $i=0; foreach($otherfans as $other) { if ($i==3) break: $displayName = ucwords($other['firstname'].' '.$other['lastname']); echo '<a href="../u/'.$other['username'].'">'.$displayName.'</a>'; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/#findComment-1203327 Share on other sites More sharing options...
unemployment Posted April 19, 2011 Author Share Posted April 19, 2011 I suppose you could keep count... $i = 0; foreach($otherfans as $other) { if($i==3) break; $i++; I think you're looking for a switch statement. Yes, you put my on the right track but I need it to say... if the array has two people make it say john and jim and if the array has three people make it say john, jim and sally. What logic am I missing? $i = 0; foreach($otherfans as $other) { $displayName = ucwords($other['firstname'].' '.$other['lastname']); if ($i == 0) { ?> <a href="../u/<?php echo $other['username']; ?>"><?php echo $displayName; ?></a> <?php } else if ($i == 1) { ?> <a href="../u/<?php echo $other['username']; ?>"><?php echo $displayName; ?></a> <?php } elseif ($i == 2) { ?> and <a href="../u/<?php echo $other['username']; ?>"><?php echo $displayName; ?></a> <?php } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/#findComment-1203329 Share on other sites More sharing options...
dcro2 Posted April 19, 2011 Share Posted April 19, 2011 Should've said that from the beginning $max = 3; $i = 1; foreach($otherfans as $other) { if($i > $max) break; if($i > 1) echo ', '; if($i == $max || $i == count($otherfans)) echo 'and '; $displayName = ucwords($other['firstname'].' '.$other['lastname']); echo '<a href="../u/'.$other['username'].'">'.$displayName.'</a>'; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/#findComment-1203384 Share on other sites More sharing options...
unemployment Posted April 19, 2011 Author Share Posted April 19, 2011 Should've said that from the beginning $max = 3; $i = 1; foreach($otherfans as $other) { if($i > $max) break; if($i > 1) echo ', '; if($i == $max || $i == count($otherfans)) echo 'and '; $displayName = ucwords($other['firstname'].' '.$other['lastname']); echo '<a href="../u/'.$other['username'].'">'.$displayName.'</a>'; $i++; } This code isn't working properly. Maybe I need to be a little more clear. Here at the result possibilities. Tom is a fan of Microsoft along with Dan Hunting. Tom is a fan of Microsoft along with Dan Hunting and Ben Gordon. Tom is a fan of Microsoft along with Dan Hunting, Ben Gordon and Roger Klotz. Tom is a fan of Microsoft along with Dan Hunting, Ben Gordon and Roger Klotz with 21 others. Please help me achieve this... Quote Link to comment https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/#findComment-1203462 Share on other sites More sharing options...
dcro2 Posted April 19, 2011 Share Posted April 19, 2011 What exactly do you mean by not working properly? I don't know where the first part comes from so that's up to you. $max = 3; $i = 1; foreach($otherfans as $other) { if($i > $max) break; if($i > 1) echo ', '; if($i == $max || $i == count($otherfans)) echo 'and '; $displayName = ucwords($other['firstname'].' '.$other['lastname']); echo '<a href="../u/'.$other['username'].'">'.$displayName.'</a>'; if($i == $max && count($otherfans) > $i) { $others = count($otherfans) - $i; echo ' with '.$others.' other'.($others>1 ? 's' : ''); } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/#findComment-1203466 Share on other sites More sharing options...
Zane Posted April 19, 2011 Share Posted April 19, 2011 Like has been mentioned already, you need a count to limit the otherfans. $count = 0; $alongWith = null; foreach($otherfans as $other) { $displayName = ucwords($other['firstname'].' '.$other['lastname']); $thisLink = "$displayName"; if($count == 0) $alongWith = $thisLink; if($count == 1) $alongWith .= " and " . $thisLink; if($count == 2) { $alongWith = str_replace("and", ", " $alongWith) . " and " . $thisLink . " with " . (count($otherfans) -3) . " others"; break; } $count++; } Quote Link to comment https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/#findComment-1203474 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.