3

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.

3 Answers 3

3

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;              
}
Sign up to request clarification or add additional context in comments.

Comments

1

simply add a condition to open/close 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;              
}

As a side-note, your code introduces many white-spaces that add up to the total page size and increase download time of the page.

Comments

0

You need to use the modulus function as in this 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;

Using modulus against 2 will allow you to decide which output needs to be appended to the $html variable. A result of 0 indicates the number (row) is divisible by 2, allowing you to output the beginning of the div. Any other result allows you to output the end of the div.

2 Comments

for loop or `while' loop do I need to use?
You should use a for loop. You can fetchAll() into am array and then loop through the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.