Php循环调试不希望结果

时间:2017-07-21 09:46:22

标签: php html for-loop if-statement while-loop

我需要有人帮助我完成我的循环..

我想创建一个有4个缩略图图像的机架,每4个图像想要开始一个新的...就像我用html一样: enter image description here

我有我的while循环来从我的数据库中获取所有图像:

$query = "sp_supplier_info 0";      
$select_all_brochures = sqlsrv_query($con, $query);     
while($row = sqlsrv_fetch_array($select_all_brochures)) {
   $brochures = json_decode($row['brochures'], true);
}

然后我创建一个for循环,这样我就可以从该brochures数组中获取所有数据:

$a = count($brochures); 
for($i=0;$i<$a;$i++){  
    $bro_link = $brochures[$i]['href'];
    $bro_name = $brochures[$i]['name'];
    $thumb    = $brochures[$i]['thumbnail'];
    echo "<ul> //starts list
    <li class='popup'> //lists item I need 4
    <a href='{$bro_link}' target='_blank' title='{$bro_name}'><img alt='{$bro_name}' src='{$thumb}'></a>
    <span class='popuptext'>
    <div class='close'><i class='fa fa-times'></i></div>
    <div class='title ng-binding'>{$bro_name}</div>
    <button class='download' href='{$bro_link}'>Download</button>
    </span>
    </li>     //item                  
    </ul>
    //start rack
    <div class='rack hidden-xs'>
    <div class='top'>
    <div class='surface'></div>
    </div>
    <div class='bottom'></div>"; //end rack                 
}

但我得到的结果是:enter image description here

所以,如果我没有错,我需要4 <li class='popup'>然后我需要回复</ul> //start rack <div class='rack hidden-xs'> <div class='top'> <div class='surface'></div> </div> <div class='bottom'></div>来创建机架,然后从第一个<ul>重新开始,然后继续4项。

我尝试在if($i % 4 == 0)ul之前创建一个If语句<li>,但我得到了相同的结果。

有人可以帮忙吗?

这是我的完整代码:

    <div class="shelf"> 
    <?php
    $query = "sp_supplier_info 0";      
    $select_all_brochures = sqlsrv_query($con, $query);     
    while($row = sqlsrv_fetch_array($select_all_brochures)) {
        $brochures = json_decode($row['brochures'], true); 
        $a = count($brochures); 
        for($i=0;$i<$a;$i++){  
            $bro_link = $brochures[$i]['href'];
            $bro_name = $brochures[$i]['name'];
            $thumb    = $brochures[$i]['thumbnail'];
            echo "<ul>
            <li class='popup'>
            <a href='{$bro_link}' target='_blank' title='{$bro_name}'><img alt='{$bro_name}' src='{$thumb}'></a>
            <span class='popuptext'>
            <div class='close'><i class='fa fa-times'></i></div>
            <div class='title ng-binding'>{$bro_name}</div>
            <button class='download' href='{$bro_link}'>Download</button>
            </span>
            </li>                       
            </ul>
            <div class='rack hidden-xs'>
            <div class='top'>
            <div class='surface'></div>
            </div>
            <div class='bottom'></div>";                  
        }
    }     
    ?>
    </div>

1 个答案:

答案 0 :(得分:1)

你使用模运算符在正确的轨道上。

哟需要:

  • <ul>时的回声$i%4 == 0(即每4次迭代,从第一次开始)
  • 每次迭代时回显<li>...</li>
  • </ul><div....$i%4 == 3时回复$i == $a-1(即每4次迭代,从第4次开始,到最后一次之后)

以下是代码:

<div class="shelf"> 
<?php
$query = "sp_supplier_info 0";      
$select_all_brochures = sqlsrv_query($con, $query);     
while($row = sqlsrv_fetch_array($select_all_brochures)) {
    $brochures = json_decode($row['brochures'], true); 
    $a = count($brochures); 
    for($i=0;$i<$a;$i++){  
        $bro_link = $brochures[$i]['href'];
        $bro_name = $brochures[$i]['name'];
        $thumb    = $brochures[$i]['thumbnail'];
        if ($i%4 == 0)
            echo "<ul>";
        echo "<li class='popup'>
        <a href='{$bro_link}' target='_blank' title='{$bro_name}'><img alt='{$bro_name}' src='{$thumb}'></a>
        <span class='popuptext'>
        <div class='close'><i class='fa fa-times'></i></div>
        <div class='title ng-binding'>{$bro_name}</div>
        <button class='download' href='{$bro_link}'>Download</button>
        </span>
        </li>";
        if ($i%4 == 3 || $i == $a-1)
            echo "</ul>
            <div class='rack hidden-xs'>
            <div class='top'>
            <div class='surface'></div>
            </div>
            <div class='bottom'></div>";                  
    }
}     
?>
</div>