Jump to content

limit array to 3


shorty3

Recommended Posts

ive coded this

 

          <? 

$query_friends=mysql_query("SELECT * FROM friends WHERE username='$viewuser' AND type='Friend'");


	$rows=mysql_num_rows($query_friends);

if ($rows == "0"){ echo "<center>No friends</center>"; }
while($dip=mysql_fetch_object($query_friends)){
echo "  <a href='profile.php?viewuser=$dip->person'>$dip->person</a>,";
	  }
	  ?>

 

but i want it to have a limit of 3 friends in a row and then a <br> and so on

 

example what its doing now username, username, username, username.................

 

 

what i want it to do is

username, username, username

username, username, username

username, username, username

 

 

and also how can i have it add up all the friends so i can have example: friends 83

 

somthing like

 <?php echo "$rows";?>

Link to comment
https://forums.phpfreaks.com/topic/201500-limit-array-to-3/
Share on other sites

you need to set a variable to start with before your while loop, check if it is divisible by 3, then increment

 

<?
$rows=mysql_num_rows($query_friends);

if ($rows == "0"){ echo "<center>No friends</center>"; }

$friend = 0;
while($dip=mysql_fetch_object($query_friends))
{
echo ($friend % 3 == 0)? "<br/>" : ""; // checks if friend number is a multiple of three if yes echo <br/> if not echo nothing
echo "  <a href='profile.php?viewuser=$dip->person'>$dip->person</a>,"; // echo link to friend profile
$friend++; // increase friend count	
}
?>

 

That should work i think (not tested)

Link to comment
https://forums.phpfreaks.com/topic/201500-limit-array-to-3/#findComment-1057288
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.