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

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

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

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.