while while循环内部循环

时间:2015-01-19 02:09:33

标签: php

我正在运行一个while循环来显示路径下的所有图像;

<?php
    $files = glob("../Desktop/IMG/BananzaNews/Thumbs/*.*");
    for ($i=0; $i<count($files); $i++)
    {
        $root = "http://www.rafflebananza.com/";
        $imagePath = $files[$i];
        $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $imagePath);
        $imageName = str_replace("../Desktop/IMG/BananzaNews/Thumbs/", "", $withoutExt);
        echo '<div class="UploadedImgs">
                <div class="ImgName">'.$imageName.'</div>
                <div class="IMG">
                    <img src="'.$root.$imagePath.'" alt="Random image" />
                </div>
            </div>';
    }
?>

我现在要发生的事情是最多显示8张图像,并在底部添加一个从1到X的按钮,将每个8包裹在一个div中,我将设置为隐藏将标签切换器设置在底部的链接。

如何运行额外的while循环来回传递变量?

几乎就在那里!

<?php
    $files = glob("../Desktop/IMG/BananzaNews/Thumbs/*.*");
    echo '<div class="Wrapper">'; //First tab
    $t = 0;
    for ($i=0; $i<count($files); $i++)
    {
        $root = "http://www.rafflebananza.com/";
        $imagePath = $files[$i];
        $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $imagePath);
        $imageName = str_replace("../Desktop/IMG/BananzaNews/Thumbs/", "", $withoutExt);

        if( $i % 8 == 0 ) {
            $t = $t + 1;
            echo '<div id="Tab_'.$t.'" class="Tabs"><h1>Tab_'.$t.'</h1>'; //hidden class
        }

        echo '<div class="UploadedImgs">
                <div class="ImgName">'.$imageName.'</div>
                <div class="IMG">
                    <img src="'.$root.$imagePath.'" alt="'.$imageName.'" />
                </div>
            </div>';

        if( $i > 0 && $i % 7 == 0 ) {
            echo '</div>';
        }
        if( $i % 8 == 0 ) {
            echo '<a href="'.$t.'" class="tab-switcher">'.$t.'</a>';
        }
    }

    echo '</div>'; //closes last div
?>

CSS

.Wrapper .Tabs:nth-child(n+2) {
    display:none;
}

1 个答案:

答案 0 :(得分:2)

您可以使用 mod运算符解决问题:%,如果一个数字可以被8整除,那么您有8个图像。所以你可以这样做:

<?php
    $files = glob("../Desktop/IMG/BananzaNews/Thumbs/*.*");

    echo '<div class="UploadedImgs">'; //First tab
    for ($i=0; $i<count($files); $i++)
    {
        $root = "http://www.rafflebananza.com/";
        $imagePath = $files[$i];
        $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $imagePath);
        $imageName = str_replace("../Desktop/IMG/BananzaNews/Thumbs/", "", $withoutExt);

        if( $i % 8 == 0 )
            echo '</div><div class="UploadedImgs hidden">'; //hidden class

        echo '<div class="ImgName">'.$imageName.'</div>
                <div class="IMG">
                    <img src="'.$root.$imagePath.'" alt="Random image" />
                </div>
            </div>';

        if($i % 8 == 0)
            echo '<a href="#" class="tab-switcher">Change tab</a>'; //link to change tab
    }

    echo '</div>'; //closes last div
?>