Page 1 of 1

inserting <br> after every third array item

Posted: Mon Aug 26, 2002 11:40 am
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

Posted: Mon Aug 26, 2002 12:03 pm
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;

Posted: Mon Aug 26, 2002 12:13 pm
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

Posted: Mon Aug 26, 2002 2:26 pm
by Takuma

Code: Select all

(($i % 3 )== 0 )
what does % sign do?

Posted: Mon Aug 26, 2002 2:30 pm
by Johnm
Modulus

Posted: Mon Aug 26, 2002 2:43 pm
by Takuma
Oh yeah! Silly me... :oops:

It works!

Posted: Mon Aug 26, 2002 3:35 pm
by tobias
The first one's the charm

It seems so obvious now.