Forum Moderators: coopster

Message Too Old, No Replies

Simplifying code

         

bivium

4:46 am on Sep 12, 2004 (gmt 0)

10+ Year Member



Hi! I have a question, what could I do to simplify this code? I'm new at PHP. I hope someone can help me:

<?php

for ($i=1; $i <= 14; $i++) {

$f = $i/4;

if (($f == "1") or ($f == "2") or ($f == "3")){

print "<br>";

} else {

print "<a href=cell_pics/$i.jpg><img src=cell_pics/th/$i.jpg border=0></a>&nbsp;&nbsp;";

}

}

?>

what I want to simplyfy is the first condition of the if statement. right now I only have few pictures, but I'll add about 50+ images, and I don't want to add the ($f == "4"), ($f == "5"), ($f == "6")... hope someone can help me, thanks:
bivium

Timotheos

6:25 am on Sep 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi bivium,

If all you want to do is get every fourth one then do something like this...

<?php

for ($i=1; $i <= 14; $i++) {
if ($i%4 == 0){
print "<br>";
} else {
print "<a href=cell_pics/$i.jpg><img src=cell_pics/th/$i.jpg border=0></a>&nbsp;&nbsp;";
}
}

?>

What I've done here is use the modulus operator [us4.php.net] which returns the remainder of $i divided by 4. When the remainder equals zero then you got it!

Tim

bivium

3:04 pm on Sep 12, 2004 (gmt 0)

10+ Year Member



Thanks! that's precisely what I wanted, and I completly forgot about taht operator =)! well, thank you again, works perfectly.