简体   繁体   中英

More elegant solution to this nested loop, anyone?

I have a php procedure that scans a directory where its content is thumbnail images of pdf files. The thumbnails are then displayed in a table, and included in an iframe of a parent web page. Each thumbnail is itself a hyperlink when clicked will open the actual pdf file. To avoid having a horizontal scroll bar, 9 images in a table row is perfect. I wrote a nested loop that in effect acts like a word wrap, where it displays 9 images and then begins another row. The actual code is much more entailed, so I have it pared down to a bare minimum example. It seems almost counter intuitive on first glance, decrementing $i on the second line of the outer loop, but it works. I wonder if anyone has a more elegant solution?

$ary = array(1,2,3,4,5,6,7,8,9,10);

for ($i=1; $i<(count($ary)+1); $i++) {
    $i = $i-1;
    for($j=0; $j<9; $j++) {
        if ($i === count($ary)) break;
        echo ($ary[$i].",  ");
        $i+=1;
    }
    echo "<br>";
}

The completed code now where $ndx is the count of the array, $dir is the scanned directory containing the png images, and $rtDir is the directory where the pdf's are saved:

if ($ndx > 0) {
$tbl = '<div id="draggable" class="ui-widget-content">
        <ul>
        <table><tr>';
        /* place 9 images on one row */
        foreach ($myfiles as $index => $image) {
            $pdf  = basename($image, ".png");
            $pdf  = $pdf . ".pdf";
            $pdf  = $rtDir.$pdf;
            $tbl .= '<td>
                        <span class="zoom">
                            <a href="'.$pdf.'" target="_blank">
                                <li><img id="pdfthumb'.$index.'" class="myPhotos" alt="pdf'.$index.'" src="'.$dir.$image.'" ></li>
                            </a>
                        </span>
                    </td>';
            if ($index % 9 == 8) {
                /* end the current row and start a new one */
                $tbl.= "</tr></table><br><br><table style='margin-top:-40px'><tr>";
            }
        }
    $tbl .= "</tr></table></ul></div>";
    printf($tbl);
    unset($myfiles);
}

Thanks everyone for your suggestions.

So you want 9 images on each row? There are usually two logical choices:

Alternative 1: You use array_chunk() , eg like this:

$chunks = array_chunk($images, 9);
foreach ($chunks as $chunk) {
    foreach ($chunk as $image) {
        // image printing goes here
    }
    echo '<br'>;
}

Alternative 2: You use the modulo operator, eg like this:

foreach ($images as $index => $image) {
    // images printing goes here
    if ($index % 9 == 0) { // or 8, since it's a 0-index array... I don't remember
        echo '<br>';
    }
}

I mostly use the second version, myself, if I have to - or I ask one of our designers to make it fit to a proper width through css. Do note also that the second version won't work if your images array is associative.

$b = count( $ary ) - 1 ;
for( $i = 0 ; $i <= $b ; $i++ ) :
    echo $ary[$i] ;
    if( ( $i + 1 ) % 9 == 0 ) :
        echo "<br>" ;
    endif ;
endfor ;

like the above comment i like modulus but i also prefer the for loop, hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM