douwu0882 2015-01-19 02:09
浏览 19
已采纳

while while循环内部循环

I'm running a while loop to display all images under a path;

<?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>';
    }
?>

What I'd like to now happen is a maximum of 8 images to show and add a button at the bottom going from 1 to X, wrapping each 8 in a div I'll set to be hidden and set the links at the bottom as the tab switchers.

How do I run an additional while loop passing vars back and forth?

Almost there!

<?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
?>

The CSS

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

1条回答 默认 最新

  • dongpiao0731 2015-01-19 02:22
    关注

    You can resolve it with mod operator: %, if a number is divisible by 8, then you have 8 images. So you can do it like this:

    <?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
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)