简体   繁体   English

有人对这个嵌套循环有更好的解决方案吗?

[英]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. 我有一个php程序,它扫描目录,目录中的内容是pdf文件的缩略图。 The thumbnails are then displayed in a table, and included in an iframe of a parent web page. 缩略图随后显示在表格中,并包含在父网页的iframe中。 Each thumbnail is itself a hyperlink when clicked will open the actual pdf file. 每个缩略图本身都是一个超链接,当单击时将打开实际的pdf文件。 To avoid having a horizontal scroll bar, 9 images in a table row is perfect. 为了避免出现水平滚动条,表格行中的9张图像是完美的。 I wrote a nested loop that in effect acts like a word wrap, where it displays 9 images and then begins another row. 我写了一个嵌套循环,实际上就像一个自动换行,在其中显示9张图像,然后开始另一行。 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,但它可以工作。 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: 现在完成的代码,其中$ ndx是数组的计数,$ dir是包含png图像的扫描目录,而$ rtDir是保存pdf的目录:

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? 因此,您希望每行9张图像吗? There are usually two logical choices: 通常有两个逻辑选择:

Alternative 1: You use array_chunk() , eg like this: 备选方案1:您使用array_chunk() ,例如:

$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: 备选方案2:您使用模运算符,例如:

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. 如果需要的话,我自己主要使用第二个版本,或者我要求我们的一位设计师通过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. 像上面的评论一样,我喜欢模数,但我也喜欢for循环,希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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