Jump to content

pagination help


tiny

Recommended Posts

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 !

Link to comment
https://forums.phpfreaks.com/topic/102654-pagination-help/
Share on other sites

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>

 

Link to comment
https://forums.phpfreaks.com/topic/102654-pagination-help/#findComment-525771
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/102654-pagination-help/#findComment-525870
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/102654-pagination-help/#findComment-525989
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/102654-pagination-help/#findComment-526718
Share on other sites

<?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.)

Link to comment
https://forums.phpfreaks.com/topic/102654-pagination-help/#findComment-526769
Share on other sites

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.