Forum Moderators: coopster

Message Too Old, No Replies

skipping files

listing every OTHER file in a dir

         

webmasters101

2:11 am on Dec 18, 2003 (gmt 0)



Hey, this is my first post at webmasterworld and I was hoping for a little help (Please go easy on me; I'm a newbie :(...)

Anyways, what I was wondering is how to list alternating files...
if you have these files in a dir
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
how would show every other one
1.jpg
3.jpg
5.jpg

could one of you guys reverse engineer this script to show me where/how it lists only every other file? thanks

[edited by: jatar_k at 2:17 am (utc) on Dec. 18, 2003]
[edit reason] no urls thanks [/edit]

jatar_k

2:19 am on Dec 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld webmasters101,

You could simply use a flag in the display loop

if ($myflag == 1) echo $filname;

then at the end of the display loop switch it

if ($myflag == 1) $myflag = 0;
else $myflag = 1;

or something along those lines

coopster

11:14 am on Dec 19, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I often use modulus in cases such as this:

$evenrow = 1; // initialize prior to loop
Begin loop...
if (++$evenrow % 2) print $filename;
...end loop

Just another way to do the same thing ;)


Off topic tip:
This really comes in handy when you are displaying data from a table and want to alter line color on every other row using stylesheets for readability:

<style type="text/css">tr.evenrow {background: #F6F6F6;}</style>
$table_data = '';
$evenrow = 1;
while (retrieving rows from table) {
$table_data .= '<tr';
if (++$evenrow % 2) $table_data .= ' class="evenrow"';
$table_data .= '>';
}
print $table_data;

PCInk

11:32 am on Dec 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I saw a neat solution to this kind of problem once and thought it was rather clever (and obvious when you think about it!):

$printstatus = 1;

foreach ....
{
if($printstatus == 1) { print ....; }

$printstatus = 3 - $printstatus;

}

Simple maths, but $printstatus will alternate between 1 and 2 with that simple equation. Of course, you cannot get it to set it to zero (which isn't the most efficient for assembly language), but for most things it is very simple.