inserting <br> after every third array item

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tobias
Forum Newbie
Posts: 4
Joined: Mon Aug 26, 2002 11:40 am

inserting <br> after every third array item

Post by tobias »

I'm new to this (of course)

I would like to add a break after every third item when I print my array.

I'm thinking this calls for a for() and an augment of some sort.

I've looked through the manual and this board for pointers, but maybe a bit too new to see exactly what I need and how to implement it

Thx
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

i.e.

Code: Select all

$c=0;
foreach($array as $entry)
&#123;
	print($entry);
	if( ++$c==3 ) 
	&#123;
		print('<br/>'); 
		$c=0;
	&#125;
&#125;
or

Code: Select all

$c = count($array);
for($i=0; $i!=count($c);$i++) 
&#123; 
   if( ($i % 3) == 0 && $i != 0) 
      print('<br/>'); 
   print($array&#1111;$i]); 
&#125;
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Code: Select all

$c = sizeof($arr);
for($i = 0; $i < $c ; ++$i)
    &#123;
        echo $arr&#1111;$i];
        (($i % 3 )== 0 ) ?  print("<br>\n") : print(" ");
    &#125;
re
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Code: Select all

(($i % 3 )== 0 )
what does % sign do?
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Modulus
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Oh yeah! Silly me... :oops:
tobias
Forum Newbie
Posts: 4
Joined: Mon Aug 26, 2002 11:40 am

It works!

Post by tobias »

The first one's the charm

It seems so obvious now.
Post Reply