Re: adding a <br> tag in the middle of a loop

From: Date: Thu, 19 Oct 2000 14:05:03 +0000
Subject: Re: adding a <br> tag in the middle of a loop
Groups: php.general 
Request: Send a blank email to php-general+get-21162@lists.php.net to get a copy of this message
Dear Rodney (& Matt), That's the right idea, but not too flexible. It won't work if you want to change it to breaking every fourth iteration, say. It works because reducing integers modulo 3 goes 0,1,2,0,1,2,0,1,2,... The only one of these which is bigger than 1 is 2. But reducing modulo 4 goes 0,1,2,3,0,1,2,3,.... So you would get two line breaks, then two nonbreaks, then two linebreaks... What is IMHO better: reduce integers starting with 1 modulo 4 and you get 1,2,3,0,1,2,3,0,1,2,3,0,... Coded up: <?php $modulus = 3; for ($i = 0; $i < $number; $i++) { echo '<input type="text" name="variable[]" size="15">'; if (($i + 1) % $modulus == 0) // break every $modulus rows echo "<br>\n"; } ?> Later, Matt ---------------------------------------------------------------- Matthew Leingang http://www.math.rutgers.edu/ Rutgers University leingang@math.rutgers.edu Department of Mathematics "This signature needs no quote." On Wed, 18 Oct 2000, Matt McClanahan wrote: > On Wed, 18 Oct 2000, Rodney Davis wrote: > > > I am trying to output a given number of <input> tags > > based on a variable with the following code: > > > > <?php > > for ($i = 0; $i < $number; $i++) > > { > > echo "<input type=\"text\" name=\"variable[]\" > > size=\"15\">"; > > } > > ?> > > > > But, if $number is a large number I get a huge string of > > <input> tages. I would like to break these every three > > iterations but I have no idea how to do this. Does > > anyone know how to do this? > > Well, one option would be to check $i % 3, and if it's greater than one, > put in your line break. > > > Matt > > > [ Part 23: "Included Message" ] >

« previous php.general (#21162) next »