tiny Posted April 24, 2008 Share Posted April 24, 2008 Hi! Im new with PHP and I have found a php code that would display images from a certain directory from my server and have a checkbox next to them so the user can select which image to delete.. What I want is to be able to paginate the displayed images. Can anyone please help me? Here's my code: This is the code where the form appears with the images and their corresponding checkbox <?php $path = "/"; $dir_handle = @opendir($path) or die("Unable to open folder"); ?> <form name="form1" method="post" action="gallery_post.php"> <?php while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; //explode to include only images $explodefile = explode(".", $file); if($explodefile[1] != "jpg" && $explodefile[1] != "gif" && $explodefile[1] != "png") continue; echo "<input type=CHECKBOX name='Files[$file]' value='y'>"; echo "<a href='http://www.mysite.com/$file' target='_blank'>$file</a><br />"; } closedir($dir_handle); ?> <input name="delete" type="submit" id="delete" value="Delete"> </form> This is the gallery_post.php which handles the form <?php $path = "/"; $dir_handle = @opendir($path) or die("Unable to open folder"); if($_POST['delete']){ foreach($_POST[Files] as $name => $value) if($value == "y"){ unlink ($path."/".$name); } echo "Image(s) successfully deleted. <a href='gallery.php'>Click here to go back.</a>"; } ?> The displaying of images and deleting is working fine but I really have no idea how to divide them into pages. Can anyone here please help me? Thanks so much ! Quote Link to comment Share on other sites More sharing options...
mrdamien Posted April 24, 2008 Share Posted April 24, 2008 You should consider using a database for this, since it is much more suited to your needs. But try this: <?php $path = "/"; $dir_handle = @opendir($path) or die("Unable to open folder"); ?> <form name="form1" method="post" action="gallery_post.php"> <?php while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; //explode to include only images $explodefile = explode(".", $file); if($explodefile[1] != "jpg" && $explodefile[1] != "gif" && $explodefile[1] != "png") continue; $fileList[] = $file; } closedir($dir_handle); $page = (int)$_GET['page']; $imagesPerPage = 10; for($i = $page*$imagesPerPage; $i < count($fileList) + $imagesPerPage; $i++){ echo "<input type=CHECKBOX name='Files[$fileList[$i]]' value='y'>"; echo "<a href='http://www.mysite.com/$fileList[$i]' target='_blank'>$fileList[$i]</a><br />"; } echo "<a href='?page={$page-1}'>Back 1 page</a> <a href='?page={$page+1}'>Forward 1 page</a>"; ?> <input name="delete" type="submit" id="delete" value="Delete"> </form> Quote Link to comment Share on other sites More sharing options...
tiny Posted April 24, 2008 Author Share Posted April 24, 2008 Hi! Thanks so much for helping. I tried it, but I am getting this error "Parse error: syntax error, unexpected '-', expecting '}' in /home/site/public_html/v1/img/gallery.php on line 39" line 39 is this: echo "<a href='?page={$page-1}'>Back 1 page</a> <a href='?page={$page+1}'>Forward 1 page</a>"; I think it's because of the {$page-1} but what should I do about it? And also, I tried to remove the -1 and +1 just to see how it looks, it seems like 10 additional checkboxes are being added on the form: http://img516.imageshack.us/img516/354/imghy1.jpg Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted April 24, 2008 Share Posted April 24, 2008 Try this.... <?php $path = "/"; $dir_handle = @opendir($path) or die("Unable to open folder"); function list_Pages($pg, $pages){ for($i = 1; $i <= $pages; $i++){ if($i == $pg){ $string .= "<b>{$i}</b>"; }else{ $string .= "<a href='index.php?page={$i}'>{$i}</a>"; } } return $string."<br>"; } ?> <form name="form1" method="post" action="gallery_post.php"> <?php while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; //explode to include only images $explodefile = explode(".", $file); if($explodefile[1] != "jpg" && $explodefile[1] != "gif" && $explodefile[1] != "png") continue; $fileList[] = $file; } closedir($dir_handle); if(!isset($_GET['page'])){ $page = 1; }else{ $page = $_GET['page']; } $imagesPerPage = 10; $pages = ceil(count($fileList) / $perPage); $from = ($pg * $perPage) - $perPage; for($i = $from; $i < $from + $imagesPerPage; $i++){ if($fileList[$i] != ""){ echo "<input type='checkbox' name='Files[{$fileList[$i]}]' value='y'>"; echo "<a href='http://www.mysite.com/{$fileList[$i]}' target='_blank'>{$fileList[$i]}</a><br />"; } } echo list_Pages($page, $pages); if($page > 1){ $prev = $page-1; }else{ $prev = 1; } if($page < $pages){ $next = $page+1; }else{ $next = $pages; } echo "<br><a href='?page={$prev}'>Back 1 page</a> <a href='?page={$next}'>Forward 1 page</a>"; ?> <input name="delete" type="submit" id="delete" value="Delete"> </form> Wasn't tested. Quote Link to comment Share on other sites More sharing options...
tiny Posted April 24, 2008 Author Share Posted April 24, 2008 Hi! Thanks for your reply. I tested your code and I didn't get errors but when I click on the next page, it doesn't load the next 10 list of files that are supposed to show on page 2, but instead it retains the 10 list of files that were loaded on page 1. Quote Link to comment Share on other sites More sharing options...
tiny Posted April 25, 2008 Author Share Posted April 25, 2008 Using ProjectFear's code.. here's what page 1 shows: http://img208.imageshack.us/img208/698/79629043on9.jpg and when I click on page 2, it shows the same thing and not the next 10 images: http://img177.imageshack.us/img177/9435/87194614ej0.jpg Anybody please please help me Quote Link to comment Share on other sites More sharing options...
mrdamien Posted April 25, 2008 Share Posted April 25, 2008 <?php $path = "."; $dir_handle = @opendir($path) or die("Unable to open folder"); function list_Pages($pg, $pages){ for($i = 1; $i <= $pages; $i++){ if($i-1 == $pg){ $string .= "<b>{$i}</b>"; }else{ $off = $i-1; $string .= "<a href='index.php?page={$off}'>{$i}</a>"; } } return $string."<br>"; } ?> <form name="form1" method="post" action="gallery_post.php"> <?php while (false !== ($file = readdir($dir_handle))) { if(!in_array($file, array("index.php","..","."))){ //explode to include only images $explodefile = explode(".", $file); if(in_array($explodefile[1], array("jpg","png","gif"))){ $fileList[] = $file; } } } closedir($dir_handle); if(!isset($_GET['page'])){ $page = 0; }else{ $page = $_GET['page']; } $perPage = 10; $from = ($page * $perPage); for($i = $from; $i < $from + $perPage; $i++){ if($fileList[$i] != ""){ echo "<input type='checkbox' name='Files[{$fileList[$i]}]' value='y'>"; echo "<a href='{$fileList[$i]}' target='_blank'>{$fileList[$i]}</a><br />"; } } $pages = ceil(count($fileList) / $perPage); echo list_Pages($page, $pages); if($page > 0){ $prev = $page-1; }else{ $prev = 1; } if($page < $pages){ $next = $page+1; }else{ $next = $pages; } echo "<br><a href='?page={$prev}'>Back 1 page</a> <a href='?page={$next}'>Forward 1 page</a>"; ?> <input name="delete" type="submit" id="delete" value="Delete"> </form> Fixed. (Tested this time.) Quote Link to comment Share on other sites More sharing options...
tiny Posted April 25, 2008 Author Share Posted April 25, 2008 Yay yay!! It worked! Thanks so soo much for your help. I really truly appreciate it. It's been driving me nuts for days but finally it's working already. I'm so happy. Thanks! Quote Link to comment 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.