如何在下面的代码中插入循环

时间:2015-01-21 17:21:17

标签: php

我有以下代码:

<div class="content">
                <h1>Fotoalbum rybníka <span>Fotky roka 2013/2015</span></h1>
                <div id="rg-gallery" class="rg-gallery">
                    <div class="rg-thumbs">
                        <!-- Elastislide Carousel Thumbnail Viewer -->
                        <div class="es-carousel-wrapper">
                            <div class="es-nav">
                                <span class="es-nav-prev">Previous</span>
                                <span class="es-nav-next">Next</span>
                            </div>
                            <div class="es-carousel">
                                <ul>
                                    <li><a href="#"><img src="images/thumbs/1.jpg" data-large="images/1.jpg" alt="image01" data-description="Kapitálne úlovky - Štuka" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/2.jpg" data-large="images/2.jpg" alt="image02" data-description="Kapitálne úlovky - Kapor" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/3.jpg" data-large="images/3.jpg" alt="image03" data-description="Kapitálne úlovky - Kapor" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/4.jpg" data-large="images/4.jpg" alt="image04" data-description="Násada - Amur" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/5.jpg" data-large="images/5.jpg" alt="image05" data-description="Násada - Amur" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/6.jpg" data-large="images/6.jpg" alt="image06" data-description="Úlovky rybárov" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/7.jpg" data-large="images/7.jpg" alt="image07" data-description="Násada - Kapor" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/8.jpg" data-large="images/8.jpg" alt="image08" data-description="Násada - Kapor" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/9.jpg" data-large="images/9.jpg" alt="image09" data-description="Západ slnka z prostredia rybníka" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/10.jpg" data-large="images/10.jpg" alt="image10" data-description="Rybolov" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/11.jpg" data-large="images/11.jpg" alt="image11" data-description="Rybolov" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/12.jpg" data-large="images/12.jpg" alt="image12" data-description="Správcovia rybníka" /></a></li>
                                                                        <li><a href="#"><img src="images/thumbs/13.jpg" data-large="images/13.jpg" alt="image13" data-description="Úlovky rybárov - 2,5kg šťuka" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/14.jpg" data-large="images/14.jpg" alt="image14" data-description="Úlovky rybárov - 5kg kapor" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/15.jpg" data-large="images/15.jpg" alt="image14" data-description="Úlovok 8-ročného rybára - 6,5kg šťuka" /></a></li>
                                </ul>
                            </div>
                        </div>
                        <!-- End Elastislide Carousel Thumbnail Viewer -->
                    </div><!-- rg-thumbs -->
                </div><!-- rg-gallery -->
                <p class="sub"></a></p>

此代码代表我页面上的galery。我想问一下如何在那里插入循环,所以我不需要一直写<li><a href="#"><img src="images/thumbs/1.jpg" ...。图像索引总是递增+1。

在c ++中它看起来像这样:

for(int i = 0; i < numImages; i++)
    LoadImage("images\thumbs\" + i + ".jpg");

3 个答案:

答案 0 :(得分:1)

在php中

$totalImages = 10;
for ($i = 0; $i < $totalImages; $i++)
    echo '<img src="images/thumbs/' . $i . '.jpg/>';

答案 1 :(得分:1)

ul

之间尝试这个PHP代码
    <ul>
           <?php
                $totimg = 15;
                for ($i=1; $i<=$totimg; $i++) {
                   echo '<li><a href="#"><img src="images/thumbs/'.$i.'.jpg" data-large="images/'.$i.'.jpg" alt="image14" data-description="Úlovok 8-ročného rybára - 6,5kg šťuka" /></a></li>';
                }
           ?>        
    </ul>

答案 2 :(得分:0)

假设您正在编写PHP

 <div class="es-carousel">
     <ul>
        <?php
            $descriptions = array ( 'Desc1', 'Desc2', 'Desc3', ... );
            $numImages = count($descriptions);
            for ($i=0; $i<$numImages; i++) 
               echo '<li><a href="#"><img src="images/thumbs/'.$i.'.jpg" data-large="images/'.$i.'.jpg" alt="image0'.$i.'" data-description="'.$descriptions[$i].'" /></a></li>';
         ?>
     </ul>
 </div>