Jump to content

get one item from file folder?


radar

Recommended Posts

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

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

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.

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] . '">';

 

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)) {

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.