dirigibleplum
dirigibleplum

Reputation: 137

php - Displaying more than one image from a database

I am trying to display images (and other data connected to them, e.g. titles) in a table. The filenames of the images are stored in my database as imgname, but the problem is that there is only one image that is being displayed.
Here is the code I use. What should I change?

mysql_select_db($database_connection, $connection);
$query_img = "SELECT imgname FROM img ORDER BY imgname";
$img = mysql_query($query_img, $connection) or die(mysql_error());
$row_img = mysql_fetch_assoc($img);
$totalRows_img = mysql_num_rows($img);

Table

<ul>
    <li>
        <img src="images/<?php echo $row_img['imgname']; ?>">
        <h3><?php echo $row_title['title']; ?></h3>
        <p><?php echo $row_description['des']; ?></p>
    </li>
    <li>
        <img src="images/<?php echo $row_img['imgname']; ?>">
        <h3><?php echo $row_title['title']; ?></h3>
        <p><?php echo $row_description['des']; ?></p>
    </li>
    <li>
        <img src="images/<?php echo $row_img['imgname']; ?>">
        <h3><?php echo $row_title['title']; ?></h3>
        <p><?php echo $row_description['des']; ?></p>
    </li>
</ul>


Since I use a simmilar code for the titles and descriptions, I have the same problem with them.

Upvotes: 0

Views: 1128

Answers (4)

kayra
kayra

Reputation: 218

$img = mysql_query($query_img, $connection) or die(mysql_error());
    $result = mysql_fetch_assoc($img);
    foreach ($result as $row_img)
    {
    ?>
        <li>
            <img src="images/<?php echo $row_img['imgname']; ?>">
            <h3><?php echo $row_img['title']; ?></h3>
            <p><?php echo $row_img['des']; ?></p>
        </li>
    <?php
    }

Upvotes: 0

rakeshjain
rakeshjain

Reputation: 1761

mysql_select_db($database_connection, $connection);
$query_img = "SELECT imgname FROM img ORDER BY imgname";
$img = mysql_query($query_img, $connection) or die(mysql_error());
<ul>
    <?php
        while($row_img = mysql_fetch_assoc($img)) {
    ?>
        <li>
            <img src="images/<?php echo $row_img['imgname']; ?>">
            <h3><?php echo $row_title['title']; ?></h3>
            <p><?php echo $row_description['des']; ?></p>
        </li>
    <?php
    }
    ?>   

</ul>

Upvotes: 0

John Conde
John Conde

Reputation: 219794

Just use a loop to iterate through the database resultset:

$img = mysql_query($query_img, $connection) or die(mysql_error());
while($row_img = mysql_fetch_assoc($img)) {
?>
    <li>
        <img src="images/<?php echo $row_img['imgname']; ?>">
        <h3><?php echo $row_title['title']; ?></h3>
        <p><?php echo $row_description['des']; ?></p>
    </li>
<?php
}

Upvotes: 1

hanleyhansen
hanleyhansen

Reputation: 6452

Use a foreach to loop through the results and include the <li> in that loop to get a list element per row returned from the query.

Upvotes: 0

Related Questions