简体   繁体   中英

Display some images in php files by getting the images from mysql database

I can display images in html form like below..here these images are in images folder

<div id="thumbs" class="navigation">
  <ul class="thumbs noscript">
  <li><a class="thumb" href="images/disease1.jpg" title="Title #0">Title #0</a></li>
  <li><a class="thumb" href="images/disease2.jpg" title="Title #1">Title #1</a></li>
  </ul>
<div>

But I want to display images in php file by getting imagges from the database according to above format.my mysql database has more than 1 images for an id.so I want to dispay images according to the id.

Below is what I've tried. Here's my PHP file:

<?php
$value=$_GET['val'];
$set_photos=mysql_query("SELECT * from photos where idProblem='$value'",$connection);

if(!$set_prob) {
    die("database query failed".mysql_error());   
}

$num_photos=mysql_num_rows($set_photos);

for($i=1;$i<=$num_photos;$i++) {
    $get_photos='<div id="thumbs"><li><a class=\"thumb"\ href=\".$photo."\ title=\"Title.$i."\>photo </a></li></div>';
}
?>

This is the PHP file where I'm trying to display the images by running the above PHP file:

<div id="thumbs" class="navigation">
<ul class="thumbs noscript">
<?php echo $get_photos; ?></ul></div>

I'm trying to find out what's wrong here. How can solve this issue?

If I understand correctly you need to display them as you pull them out of the database:

This is simply overwriting the variable $get_photos

for($i=1;$i<=$num_photos;$i++) {
$get_photos='
    <div id="thumbs">
        <li>
            <a class=\"thumb"\ href=\".$photo."\ title=\"Title.$i."\>photo </a>
        </li>
    </div>';
}

Change to:

echo '<div id="thumbs" class="navigation"><ul class="thumbs noscript">';

for($i=1;$i<=$num_photos;$i++) {
    echo '<li><a class="thumb" href="'.$photo.'" title="Title'.$i.'">photo </a></li>';
}

echo '</ul></div>';

Hi try below code it solve your issue :)

<?php
$value=$_GET['val'];
$set_photos = mysql_query("SELECT * from photos where idProblem='".$value."'", $connection);

$liList = '';
$ind = 0;
if(mysql_num_rows($set_photos))
{
    while($row = mysql_fetch_assoc($set_photos))
    {
        $liList .= '<li><a class="thumb" href="'.$row['YOUR_IMAGE_PATH_FIELD_NAME'].'" title="Title #'.$ind.'">Title #'.$ind.'</a></li>';
        $ind++;
    }
}
?>

<div id="thumbs" class="navigation">
    <ul class="thumbs noscript">
    <?php echo $liList; ?>
    </ul>
</div>

检查查询是否返回行,因为似乎for语句未执行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM