Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I modify this php while loop?

Tags:

html

php

mysql

I am trying to display some images dynamically from a mysql table.

This is what I tried it with PHP and its working for me.

// 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;              
}

And this is the markup from above 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>

But my problem is, when I am going to modify above Markup to like below.

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

Actually I need to group two images inside row-fluid DIVs when displaying my images.

Can anybody tell me how I create like that markup using php?

Hope somebody may help me out.

Thank you.

like image 738
user3733831 Avatar asked Feb 21 '26 03:02

user3733831


1 Answers

Still use while loop

$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;              
}
like image 64
splash58 Avatar answered Feb 23 '26 16:02

splash58



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!