Loop through an array

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
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Loop through an array

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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" ?
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

are you after alternating row colors?
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

exactly !
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Search for zebra.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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>';
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

Thanks folks.
Superman859
Forum Commoner
Posts: 47
Joined: Sun Oct 29, 2006 10:22 am

Post 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?
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post 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
Post Reply