Jump to content

Limit foreach to 3 results


unemployment

Recommended Posts

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
}

Link to comment
Share on other sites

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++;
}

 

 

Link to comment
Share on other sites

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++;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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++;
}

Link to comment
Share on other sites

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++;
}

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.