Jump to content

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
https://forums.phpfreaks.com/topic/234123-limit-foreach-to-3-results/
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++;
}

 

 

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

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

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

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

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.