我如何修改这个php while循环?

时间:2015-06-30 16:24:38

标签: php html mysql

我试图从mysql表中动态显示一些图像。

这是我用PHP尝试它并为我工作的。

// Fetch all the records:
while ($stmt->fetch()) {

    $html  = "  <div class='row-fluid custom'>\n";
    $html .= "      <div class='span6'>\n";
    $html .= "      <img src='images/{$image}'>\n";
    $html .= "      </div>\n";                                                              
    $html .= "  </div>\n";

    //Add images to array
    $images[] = $html;              
}

这是PHP上面的标记:

<div class='row-fluid custom'>
    <div class='span6'>
        <img src='images/cond-1.png'>
    </div>                                                                  
</div>  
<div class='row-fluid custom'>
    <div class='span6'>
        <img src='images/cond-2.png'>
    </div>                                                                  
</div>  
<div class='row-fluid custom'>
    <div class='span6'>
        <img src='images/cond-3.png'>
    </div>                                                                  
</div>  
<div class='row-fluid custom'>
    <div class='span6'>
        <img src='images/cond-4.png'>
    </div>                                                                  
</div>

但我的问题是,当我要将Markup上面的内容修改为如下所示。

<div class="row-fluid custom">
    <div class="span6">
        <img src="images/cond-1.png">
    </div>                                  
    <div class="span6">
        <img src="images/cond-2.png">
    </div>                                  
</div>                              
<div class="row-fluid custom">
    <div class="span6">
        <img src="images/cond-3.png">
    </div>                                  
    <div class="span6">
        <img src="images/cond-4.png">
    </div>                                  
</div>

实际上,我需要在显示图像时将row-fluid DIV内的两个图像分组。

有人能告诉我如何使用php创建这样的标记吗?

希望有人可以帮助我。

谢谢。

3 个答案:

答案 0 :(得分:3)

仍然使用while循环

$i = 0;
while ($stmt->fetch()) {
    $html  = "";
    if($i % 2 == 0) $html  = "  <div class='row-fluid custom'>\n";
    $html .= "      <div class='span6'>\n";
    $html .= "      <img src='images/{$image}'>\n";
    $html .= "      </div>\n";                                                              
    if($i++ % 2 == 1) $html .= "  </div>\n";

    //Add images to array
    $images[] = $html;              
}

答案 1 :(得分:1)

您需要使用此modulus function中的example

$images = array('foo.jpg', 'bar.jpg','baz.jpg','glorp.jpg');

$html = '';

for($i = 0; $i < count($images); $i++){
    if($i % 2 == 0) {
         $html .= "  <div class='row-fluid custom'>\n"; 
         $html .= "      <div class='span6'>\n"; 
         $html .= "      <img src='images/{$images[$i]}'>\n"; 
         $html .= "      </div>\n";   
    } else {
        $html .= "      <div class='span6'>\n"; 
        $html .= "      <img src='images/{$images[$i]}'>\n"; 
        $html .= "      </div>\n";   
        $html .= "   </div>\n";
    }
}
echo $html;

使用模数2将允许您决定需要将哪个输出附加到$html变量。结果为0表示数字(行)可以被2整除,允许您输出div的开头。任何其他结果都允许您输出div的结尾。

答案 2 :(得分:1)

只需添加条件即可打开/关闭div

$open_div=true;
while ($stmt->fetch()) {
    if( $open_div )
         $html  = "  <div class='row-fluid custom'>\n";
    $html .= "      <div class='span6'>\n";
    $html .= "      <img src='images/{$image}'>\n";
    $html .= "      </div>\n";  
    if( $open_div = !$open_div )                                                            
        $html .= "  </div>\n";
    //Add images to array
    $images[] = $html;              
}

作为旁注,您的代码会引入许多white-spaces,这些ArrayFormula()会占总页面大小并增加页面的下载时间。