NoyHadar Posted January 21, 2015 Share Posted January 21, 2015 My first post. I am trying to import images related to a particular listing ID. Currently, I have to enter the number of available photos into a db table, and run the following loop; <section id="photos"> <?php $b = $row['PIX']; for ( $a = 01; $a <= $b; $a++ ) {echo "<a href=\"../imgs/{ $row[ 'ID' ] }-" . { $a } . ".jpg\" ><img src=\"../imgs/{ $row[ 'ID' ] }-" . { $a } . "_tn.jpg\" /></a> " ; } ?> </section> I would like to find a way to import all the images that begin with the $row[ 'ID' ], but without me having to enter/store { $b } (the number of images recorded in the db). How can I accomplish this? _ Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/ Share on other sites More sharing options...
CroNiX Posted January 21, 2015 Share Posted January 21, 2015 It would help if you showed the query you use to retrieve the image filenames. Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/#findComment-1503662 Share on other sites More sharing options...
NoyHadar Posted January 21, 2015 Author Share Posted January 21, 2015 Here it is. Thanks! $_ID = $_GET[ 'ID' ]; $query = " SELECT * FROM listings AND MLS='. $_ID .' " ); $result = $con->query($query) or die($con->error.__LINE__); $row = $result->fetch_assoc(); Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/#findComment-1503666 Share on other sites More sharing options...
CroNiX Posted January 21, 2015 Share Posted January 21, 2015 Sorry, that's still pretty vague since you used "select *" instead of listing the actual column names. What does $row look like after you perform the query? Do you have a separate table to store the image names? If you did then you could just use a JOIN and not store 'PIX' at all in the listings table. That's the purpose of a relational database. listings: -id listing_images: -ID -listing_id (references listings.id) -image_name Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/#findComment-1503668 Share on other sites More sharing options...
NoyHadar Posted January 22, 2015 Author Share Posted January 22, 2015 I do not have a need to keep the name of the file in the db for the following reason, if a row (db entry) has an ID value of 'q123456', then I have it's associated images named; q123456-1.jpg, q123456-1_tn.jpg, q123456-2.jpg, q123456-2_tn.jpg, etc, all in the same directory. So if this particular listing has 16 images I would manually have to enter that value in the db as PIX=16 I would like to know if I can avoid that step, and somehow import all the images that begin with 'q123456', without the designation of how many images to check for. Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/#findComment-1503676 Share on other sites More sharing options...
Solution CroNiX Posted January 22, 2015 Solution Share Posted January 22, 2015 Can probably easily do that with glob() $ID = 'q123456'; foreach (glob('/path/to/images/' . $ID . '-*') as $filename) { echo "$filename size " . filesize($filename) . "\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/#findComment-1503678 Share on other sites More sharing options...
NoyHadar Posted January 22, 2015 Author Share Posted January 22, 2015 This worked like a charm, thank you so much!!! One last question, I use this code to retrieve the thumbnails;('../imgs/homes/' . $mls . '_*_tn.jpg') ie. A1736620_01_tn.jpg But when trying to retrieve the original image, I get both;('../imgs/homes/' . $mls . '_*.jpg') ie. A1736620_01.jpg A1736620_01_tn.jpg How can I use the wildcard char to isolate just the original image? Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/#findComment-1503779 Share on other sites More sharing options...
CroNiX Posted January 22, 2015 Share Posted January 22, 2015 (edited) Not going to pretend to be an expert with glob(), but it wouldn't be easy and maybe not possible due to the way your filenames are, and glob's regex is inclusive (can't exclude results). So, I'd either rename your regular non-thumbnail files to something like: q123456-1_full.jpg, q123456-1_tn.jpg and then you can use: foreach (glob('/path/to/images/' . $ID . '-*_full.jpg') as $filename) { or just test each filename when outputting to see if it contains "_tn.jpg" before outputting $ID = 'q123456'; foreach (glob('/path/to/images/' . $ID . '-*') as $filename) { //make sure it's not a thumbnail if (stripos($filename, '_tn.jpg') === FALSE) { echo "$filename</br>"; } } Another option is to put your "full" files in a separate dir from the thumbnails. Then grab all of the full files, and when you need to output the thumbnails you can add the "_tn" to the filename and just change the path. Edited January 22, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/#findComment-1503780 Share on other sites More sharing options...
NoyHadar Posted January 22, 2015 Author Share Posted January 22, 2015 This did the trick; ('../imgs/homes/' . $mls . '_??.jpg') vs. ('../imgs/homes/' . $mls . '_*_tn.jpg') Thanks for all your help Noy Hadar Quote Link to comment https://forums.phpfreaks.com/topic/294107-how-do-i-import-all-images-beginning-with-the-same-characters/#findComment-1503794 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.