1

I have a database that stores information of an item including an image source path, descriptions etc. Currently the data is static, as shown in the code snippet below. Basically I need to output a new 'thumbnail' for each row of data in the table. I think this is done by a for loop, however I'm not sure how to implement this. Thank you.

<div class='panel-heading' contenteditable='false'>Your items for sale</div>
<div class='panel-body'>
    <div class='row'>
        <div class='col-md-4'>
            <div class='thumbnail'>
                <img alt='300x200' src='http://lorempixel.com/600/200/people'>
                <div class='caption'>
                    <h3>
                        Rover
                    </h3>
                    <p>Cocker Spaniel who loves treats.</p>
                    <p></p>
                </div>
            </div>
        </div>
        <div class='col-md-4'>
            <div class='thumbnail'>
                <img alt='300x200' src='http://lorempixel.com/600/200/city'>
                <div class='caption'>
                    <h3>
                        Marmaduke
                    </h3>
                    <p>Is just another friendly dog.</p>
                    <p></p>
                </div>
            </div>
        </div>
        <div class='col-md-4'>
            <div class='thumbnail'>
                <img alt='300x200' src='http://lorempixel.com/600/200/sports'>
                <div class='caption'>
                    <h3>
                        Rocky
                    </h3>
                    <p>Loves catnip and naps. Not fond of children.</p>
                    <p></p>
                </div>
            </div>
        </div>
    </div>
</div>

My attempt:

<? $sql = "SELECT srcpath FROM images";
$res = mysql_query($sql) or die(mysql_error());

$result = mysql_query($sql);

echo '<div"><ul>';

while ($fetch = mysql_fetch_array($result)) {
    $petname = $fetch['username'];
    echo '<li><P>'.$fetch['description'].'</P>';
    echo '<img src="'.$fetch['srcpath'].'" alt="" />';
    echo '</li>';
}
echo '</ul></div>';

?>
Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
Tom Reeves
  • 123
  • 2
  • 13

1 Answers1

0

I still disencourage to use mysql, you should use mysqli or some other as mysql is deprecated.

Anyway, I'd recommend preparing the whole html loop via php and then print it, like this:

First of all, this would be your php part:

<?
$sql = "SELECT username, description, srcpath FROM images"; // Note that I included the fields you're actually using.

$res = mysql_query($sql) or die(mysql_error());

$result = mysql_query($sql);

$divHtml ='<div class='panel-body'>'; // Here's where you add the main div that holds the others.

//now in the loop you've got to be careful with the css classes, as your html shows, they won't be always the same. But this should give you some insight on how it works.

while($fetch = mysql_fetch_array($result)){

    $divHtml .='<div class="row">';// add css classes and the like here. In case you don't know, the .= operators concatenate the strings that will make your html code.
    $divHtml .='    <div class="col-md-4">'; // be careful with this class, as you might need to evaluate it for every run of the loop
    $divHtml .='        <div class="thumbnail">';
    $divHtml .='            <li><P>' . $fetch['description'] .'</P>';
    $divHtml .='            <img src="'.$fetch['srcpath'].'" alt="" />';
    $divHtml .='            </li>';
    $divHtml .='        </div>';
    $divHtml .='    </div>';
    $divHtml .='</div>';
}

$divHtml .= '</div>'; // And here you close the main div.
?>

Then your html will look just like this:

<div class='panel-heading' contenteditable='false'>Your items for sale</div>
          <?php echo $divHtml; ?>
Gonzalo Acosta
  • 135
  • 1
  • 13