radar Posted July 27, 2010 Share Posted July 27, 2010 Okay, so no clue if this is possible ive never had to do it before... I've got an array, and in this array is a field named img ($data['img']) and what this holds is the name of a folder. this folder may have up to 8 images inside it. but for the current page of display i need to only display 1 of those images. so how (if possible), can i take $data['img'] which is populated from the database and the limit is 1, so i don't need any iterations, and retrieve one of the file names (img1.jpg) from that file folder to display? Link to comment https://forums.phpfreaks.com/topic/209042-get-one-item-from-file-folder/ Share on other sites More sharing options...
AbraCadaver Posted July 27, 2010 Share Posted July 27, 2010 So, you want image1.jpg or you want a random image or the first listed image? Link to comment https://forums.phpfreaks.com/topic/209042-get-one-item-from-file-folder/#findComment-1091834 Share on other sites More sharing options...
smonkcaptain Posted July 27, 2010 Share Posted July 27, 2010 If it's an image, you can do this... <?php //IN YOUR CASE YOUR USING A QUERY TO FETCH THE IMAGE NAME, HOWEVER LETS CUT IT SHORT AND SAY THE IMAGE IS $row=mysql_fetch_array($example); $name='$row'; echo '<img src="../example/example/'.$name.'.jpg.'">'; ?> Hope that helps? - Just noticed that it's an array of 8 images, and you want to display 1 per page? In which case, you can use a <?php while($row=mysql_fetch_array($example){ $name=$row; echo '<img src="../example/example/'.$name.'.jpg'">'; } ?> For it to display one image per page, your going to have to look at offsets and pagination, you can find the tutorial here http://www.phpfreaks.com/tutorial/basic-pagination Link to comment https://forums.phpfreaks.com/topic/209042-get-one-item-from-file-folder/#findComment-1091837 Share on other sites More sharing options...
radar Posted July 27, 2010 Author Share Posted July 27, 2010 either a random image or the first listed image doesn't matter. This portion of my script is for the administration panel which doesn't need to have the photo gallery function, but i'd like to have one image be shown of the product. @smonk, your code would help if the image name was actually in the database, but in actuality the only thing in the database is the folder that the image is in. Link to comment https://forums.phpfreaks.com/topic/209042-get-one-item-from-file-folder/#findComment-1091840 Share on other sites More sharing options...
AbraCadaver Posted July 27, 2010 Share Posted July 27, 2010 either a random image or the first listed image doesn't matter. This portion of my script is for the administration panel which doesn't need to have the photo gallery function, but i'd like to have one image be shown of the product. @smonk, your code would help if the image name was actually in the database, but in actuality the only thing in the database is the folder that the image is in. Depends on what the paths are, but something similar to: $files = glob($data['img'] . '/*.jpg'); echo '<img src="' . $data['img'] . '/' . $files[0] . '">'; Link to comment https://forums.phpfreaks.com/topic/209042-get-one-item-from-file-folder/#findComment-1091844 Share on other sites More sharing options...
radar Posted July 27, 2010 Author Share Posted July 27, 2010 nice i'll give that a shot.. thanks Abra Link to comment https://forums.phpfreaks.com/topic/209042-get-one-item-from-file-folder/#findComment-1091848 Share on other sites More sharing options...
radar Posted July 27, 2010 Author Share Posted July 27, 2010 Okay so only issue to this i am setting, is that it grabs all of them instead of just 1 which means further handling of $files is required. Next issue with the .jpg which i used as an example, is images could be .jpg, .gif, .png, .bmp.. as is defined in the first line of my JS file for uploading: if(!/(\.bmp|\.gif|\.jpg|\.jpeg)$/i.test(window.parent.document.form1.filefieldname.value)) { Link to comment https://forums.phpfreaks.com/topic/209042-get-one-item-from-file-folder/#findComment-1091855 Share on other sites More sharing options...
AbraCadaver Posted July 27, 2010 Share Posted July 27, 2010 Okay so only issue to this i am setting, is that it grabs all of them instead of just 1 which means further handling of $files is required. Next issue with the .jpg which i used as an example, is images could be .jpg, .gif, .png, .bmp.. as is defined in the first line of my JS file for uploading: if(!/(\.bmp|\.gif|\.jpg|\.jpeg)$/i.test(window.parent.document.form1.filefieldname.value)) { I like glob. If you want to just get one file name then it is longer code, use opendir() and readdir() once and check to make sure the entry is not a directory. With glob() use *.* or use the previous method. Link to comment https://forums.phpfreaks.com/topic/209042-get-one-item-from-file-folder/#findComment-1091862 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.