mamoosh Posted March 19, 2008 Share Posted March 19, 2008 Hi everyone! I have this script that fetches images from a directory and shows them on one page. I was wondering if there is a way to have it list them in pages. For example, I want it to show 50 images per page, rather than echoing everything I have. Thanks in advance. Here's the script I'm using: <?php $dir = 'images'; $dirHandle = opendir($dir); $count = -1; $returnstr = ""; while ($file = readdir($dirHandle)) { if(!is_dir($file) && strpos($file, '.jpg')>0) { $returnstr .= '<img src="'.$file.'" /><br />'; } } $returnstr .= ' '; echo $returnstr; closedir($dirHandle); ?> P.S. can I do this: <?php if(!is_dir($file) && strpos($file, '.jpg'||'.png'||'.gif')>0) { ?> Link to comment https://forums.phpfreaks.com/topic/96811-fetching-image-from-directory/ Share on other sites More sharing options...
jeremyphphaven Posted March 19, 2008 Share Posted March 19, 2008 read the whole directory into an session array once (check for it to make sure you don't keep doing it). Then create a page variable that gets sent based on where you are Then pick up the page variable and depending on where what page you are on, you'll get served up the correct files. let me know if you need a quick example... hoping that just suggesting to you to put the thing in an array and use a page number system will suffice. Link to comment https://forums.phpfreaks.com/topic/96811-fetching-image-from-directory/#findComment-495442 Share on other sites More sharing options...
mamoosh Posted March 19, 2008 Author Share Posted March 19, 2008 read the whole directory into an session array once (check for it to make sure you don't keep doing it). Then create a page variable that gets sent based on where you are Then pick up the page variable and depending on where what page you are on, you'll get served up the correct files. let me know if you need a quick example... hoping that just suggesting to you to put the thing in an array and use a page number system will suffice. Thank you very much, but if it's not too much to ask, can you please provide that example. I'm a newbie. Thank you again. Link to comment https://forums.phpfreaks.com/topic/96811-fetching-image-from-directory/#findComment-495451 Share on other sites More sharing options...
mamoosh Posted March 19, 2008 Author Share Posted March 19, 2008 I apologize for double posting, but I couldn't find an Edit button. Anywho... Someone tried to help me with the code, however I'm getting errors like unexpected T_VARIABLE on line 2 <?php if(!isset($_SESSION["page"]) $_SESSION["page"]=1; $dir = 'images'; $dirHandle = opendir($dir); $count = -1; $returnstr = ""; $localImageCount=0; $ImagesPerPage=50; while ($file = readdir($dirHandle)) { if(!is_dir($file) && strpos($file, '.jpg')>0&&(localImageCount>.. ($ImagesPerPage*$_SESSION.. ["Page"]-1)&& localImageCount>(.. $ImagesPerPage*$_SESSION["Page"]))) { $returnstr .= '<img src="'.$file.'" /><br />'; localImageCount++; } } $returnstr .= ' '; echo $returnstr; closedir($dirHandle); $_SESSION["Page"]++; ?> Link to comment https://forums.phpfreaks.com/topic/96811-fetching-image-from-directory/#findComment-495556 Share on other sites More sharing options...
AdRock Posted March 19, 2008 Share Posted March 19, 2008 what is line 2 Link to comment https://forums.phpfreaks.com/topic/96811-fetching-image-from-directory/#findComment-495708 Share on other sites More sharing options...
uniflare Posted March 19, 2008 Share Posted March 19, 2008 use this instead: if(!isset($_SESSION["page"])){ $_SESSION["page"]=1; } (you missed a closing bracket on the if statement up top) Link to comment https://forums.phpfreaks.com/topic/96811-fetching-image-from-directory/#findComment-495734 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.