Page 1 of 1

Loop through an array

Posted: Fri Nov 03, 2006 10:11 pm
by tarja311
Hello all...

I know this is probably very simple but i just can't get my foot on it tonight. I have two strings in an array and i want these two to loop and print for an x amount of time. Right now i have more than two strings in the array to accomplish this task and it is definitely not to right way to go because i am working on a search engine and the elements in this array has to be printed out as the list grows. Right now my array looks like so :

Code: Select all

$stripe =  array("a", "b", "a", "b", "a", "b");
i want it to look something like this :

Code: Select all

$stripe =  array("a", "b");
and then just loop through that for an x amount of time. I tried looping the above with a for-loop but it would only print the two and quit, not keep going. I know i am overlooking something here...

thanks

-- tarja

Posted: Fri Nov 03, 2006 10:43 pm
by feyd
Is "x amount of time" number of iterations or number of seconds? Can you give us an example of the output you would like to see for a given "x" ?

Posted: Fri Nov 03, 2006 10:55 pm
by tarja311
Number of iterations.

The "a" and "b" in the line

Code: Select all

$stripe =  array("a", "b", "a", "b", "a", "b");
are different stripe colors in my .css file. "a" is white and the other is blue. I want these stripes to be outputted with my search results, and they must to be outputted over and over, as much as the results are printed. It is a little hard to explain but it should look something like this :

-----------------
result 1
-----------------
result 2
-----------------
result 3
-----------------

...ect

Posted: Fri Nov 03, 2006 10:58 pm
by n00b Saibot
are you after alternating row colors?

Posted: Fri Nov 03, 2006 11:01 pm
by tarja311
exactly !

Posted: Fri Nov 03, 2006 11:05 pm
by feyd
Search for zebra.

Posted: Fri Nov 03, 2006 11:06 pm
by n00b Saibot
a simple loop will do then won't it?

Code: Select all

for($i = 0; $i < $rows; $i++)
 echo '<td class="' . ($i % 2 == 0 ? 'white' : 'blue') . '">Contents</td>';

Posted: Sat Nov 04, 2006 5:21 pm
by tarja311
Thanks folks.

Posted: Sat Nov 04, 2006 5:58 pm
by Superman859
I'm new to PHP, but can you not just say:

Code: Select all

for ($i = 0; $i<iterations;$i++)
 { 
 array [] = "a";
  array [] = "b";
  }
Shouldn't that just loop through however many times you need and add values "a" and "b" to the array? I am unsure of the specific goal for you and your site, or if this is the best thing to do, but is this not a way to loop through adding strings to an array?

Posted: Sat Nov 04, 2006 10:59 pm
by Cameri
n00b Saibot wrote:a simple loop will do then won't it?

Code: Select all

for($i = 0; $i < $rows; $i++)
 echo '<td class="' . ($i % 2 == 0 ? 'white' : 'blue') . '">Contents</td>';
What about using XOR :P It will save you 0.00001 second!WOW xD
If you care, and for the hell of it:

Code: Select all

$t = 0;
while(...){
 echo '<td class="' . ( $t ? 'white' : 'blue') . '">Contents</td>';
  $t ^= 1;
}
:P