Jump to content

row alternating problem


justinh

Recommended Posts

Okay, im displaying a directory, and i want to alternate between divs for each file in the folder.. everything works, but its displaying each file twice.. =/

 

heres the code

 

<?php

$d = dir($path);
$icon = '';
$rowcount = 1;

while (false !== ($entry = $d->read())) {

    if ( substr($entry, 0, 1)=='.' ) continue;


    $size = filesize($path.'/'.$entry);
    $humansize = humansize($size);


    $dotpos = strrpos($entry, '.');

    if ($dotpos) {
        $ext = substr($entry, $dotpos+1);
        if ($ext === 'jpeg' || $ext === 'gif' || $ext === 'png') {
            $icon = "<img src='$entry' style='width: 48px; height: auto; vertical-align: text-top;' alt='icon' title='$entry' />";
        }
    }
    while (false !== ($entry = $d->read())){
    if($rowcount % 2 == 0){
    print "<div id=roweven><a href='/stbs/testing/$entry'>$entry</a> ($humansize) $icon</div>\n";
    ++$rowcount;
    }
    if($rowcount % 2 == 1){
    print "<div id=rowodd><a href='/stbs/testing/$entry'>$entry</a> ($humansize) $icon</div>\n";
    ++$rowcount;
    }
    }
    

    $icon= '';
}

$d->close();

?>

Link to comment
https://forums.phpfreaks.com/topic/137211-row-alternating-problem/
Share on other sites

you are incrementing inside your IF, so both IFs will be true:

    while (false !== ($entry = $d->read())){
      if($rowcount % 2){
        print "<div id=rowodd><a href='/stbs/testing/$entry'>$entry</a> ($humansize) $icon</div>\n";
      }else{
        print "<div id=roweven><a href='/stbs/testing/$entry'>$entry</a> ($humansize) $icon</div>\n";
      }
      ++$rowcount;
    }

 

edit: also, and ID attribute needs to be unique for the entire page. if you want to re-use CSS styles, use CLASS instead...

thanks fixed it, same div id's seem to be working just fine.

 

they may work...but they violate XHTML/CSS standards

 

To add to that, using a browser that conforms to standards, such as FireFox, this (I think this is true) will display different results due to the ID. IE would probably display it correctly since it violates the standards.

 

Best coding practices is to conform to the standards, even though it "may work" later down the line it may not work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.