简体   繁体   中英

PHP pull specific sets of images from a directory

I'm pulling an image from a directory (images) and placing it in an html table row along with my mysql query. PHP finds the image as the mysql DB id matches the image name. ie if id = 12 then 12.jpg is pulled from directory using $imgnum in the path.

<?php
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    $imgnum =  $row["id"];
?> 
<tr>
<td>    
<img src="images/<?php echo $imgnum ?>.jpg"/>

The issue is there may be a series of images all associated with that row, ie 1-1.jpg 1-2.jpg and so on, sometimes there may be only one and the code above works fine.

Is there a way to output all images with the name equalling the id but with a dash afterwards so it picks up other associated images? Even if someone can tell me what to learn to achieve this?

cheers

You can use php's glob() to find similar files. Something along the lines of this:

foreach (glob("/path/to/images/folder/" . $imgnum . '-*') as $filename) {
    // do something with $filename
}

Thought I'd post what I used from the help above. It now outputs a list of all of the images I needed.

<?php 
    $imgnum = $id;
    foreach (glob("images/" . $imgnum . '-*') as $filename) {
    echo "<a href=''><img src='$filename' class='imgsize'></a> <br>";
    }
?> 

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