Pagination problem with listing directory result

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Pagination problem with listing directory result

Post by Peuplarchie »

Good day to you all,
can somebody help me with my pagination, the following code does not give me any error but the pagination part of the script don't do his job.





Code: Select all

<?php




		// Images directory lister start script
$image_counter = -1;
$row_counter = 0;
$cell_counter = 0;
$show=array('.jpg','.JPG','.gif','.GIF');
$path = 'Images/2/';


$dir_handle = @opendir($path) or die("Unable to open $path");

$image_table = "<table valign=\"top\" align=\"center\">\n<tr><th colspan=3>Existing Pics in Directory:</th></tr>\n<tr>";
while (false !== ($file = readdir($dir_handle))) {
  if(in_array(substr($file,-4,4),$show)){
        if(!(($image_counter + 1) % 3)){
          $row_counter++;
        }
$newpath0 = basename($file,".*");
      $image_table .=(($image_counter + 1) % 3)? "" : "</tr>\n<tr><td><br></td></tr><tr valign=\"top\">";
      $image_table .= "\n<td valign=\"top\"><table width=\"200\" border=\"1\" cellpadding=\"0\" cellspacing=\"1\" align=\"center\">\n";
      $image_table .= "\n<tr><td align=\"center\"><b>$newpath0</b></td></tr>\n";
      $image_table .= "\n<td><img src=\"$path$file\" width=\"200\"></td></tr>\n";
      $image_table .= "\n<tr><td></td></tr>\n";
      $image_table .= "\n<tr><td><b>Prix :</b> Test</td></tr>\n";
      $image_table .= "\n<tr><td><b>Prix :</b> Test</td></tr>\n";
      $image_table .= "\n<tr><td><b>Prix :</b> Test</td></tr>\n";
      $image_table .= "\n</table></td>\n\n\n";

      ++$image_counter;
      ++$cell_counter;
  }

}





$colspan= ($row_counter * 3) - $cell_counter;
$image_table .= ($cell_counter % 3) ?  "<td colspan=$colspan>&nbsp;</td>" : "";
$image_table .= "</tr>\n</table>\n";
echo "$image_table<br><br>";


		// Pagination script start
class display {
	function pagination($rows, $per_page, $current_page, $page_link) {
		global $core,$C;
		
		// Create a Page Listing
		$this->pages = ceil($rows / $per_page);
		
		// If there's only one page, return now and don't bother
		if($this->pages == 1) {
			return;
		}
		
		// Pagination Prefix
                $output .= "<!-- Pagination by Dennis Pedrie. Used by Permission -->";
		$output = "Pages: ";
		
		// Should we show the FIRST PAGE link?
		if($current_page > 2) {
			$output .= "<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>";
		}
		
		// Should we show the PREVIOUS PAGE link?
		if($current_page > 1) {
			$previous_page = $current_page - 1;
			$output .= " <a href=\"". $page_link .">page=". $previous_page ."/\" title=\"Previous Page\"><</a>";
		}
		
		// Current Page Number
		$output .= "<strong>[ ". $current_page ." ]</strong>";
		
		// Should we show the NEXT PAGE link?
		if($current_page < $this->pages) {
			$next_page = $current_page + 1;
			$output .= "<a href=\"". $page_link ."?page=". $next_page ."/\" title=\"Next Page\">></a>";
		}
		
		// Should we show the LAST PAGE link?
		if($current_page < $this->pages - 1) {
			$output .= " <a href=\"". $page_link ."?page=". $this->pages ."/\" title=\"Last Page\">>></a>";
		}
		
		// Return the output.
		return $output;
	}
}

$display = new display;
echo $display->pagination($row_counter, "2", "1", "http://theponther.peuplies.info/AAA%20TEST/Images/images3.php");

echo "<br>";
echo "<b>Total Cells w pics: $cell_counter<br>";
echo "<b>Total Rows: $row_counter<br>";
echo "<b>Empty Cells in last row: $colspan";
?>


Thanks !
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

So how do you mean this doesn't work? More detail might help.
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Post by Peuplarchie »

When I run the code,
Pagination mean that it create a page for more thatX rows let's say.

In my code I say split page after 2 rows but it don't split the page, it's doing nothing , just list the directory .
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Post by Peuplarchie »

instead of giving the URL of the page can I or how can I just give the name or ID of the table itself ?
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Post by Peuplarchie »

There is 2 script put together in my script :

The first a script that take all image in a directory and display them in a html table in rows of 3.

Code: Select all

<?php




$image_counter = -1;
$row_counter = 0;
$cell_counter = 0;
$show=array('.jpg','.JPG','.gif','.GIF');
$path = 'Images/2/';


$dir_handle = @opendir($path) or die("Unable to open $path");

$image_table = "<table valign=\"top\" align=\"center\">\n<tr><th colspan=3>Existing Pics in Directory:</th></tr>\n<tr>";
while (false !== ($file = readdir($dir_handle))) {
  if(in_array(substr($file,-4,4),$show)){
        if(!(($image_counter + 1) % 3)){
          $row_counter++;
        }
$newpath0 = basename($file,".*");
      $image_table .=(($image_counter + 1) % 3)? "" : "</tr>\n<tr><td><br></td></tr><tr valign=\"top\">";
      $image_table .= "\n<td valign=\"top\"><table width=\"200\" border=\"1\" cellpadding=\"0\" cellspacing=\"1\" align=\"center\">\n";
      $image_table .= "\n<tr><td align=\"center\"><b>$newpath0</b></td></tr>\n";
      $image_table .= "\n<td><img src=\"$path$file\" width=\"200\"></td></tr>\n";
      $image_table .= "\n<tr><td></td></tr>\n";
      $image_table .= "\n<tr><td><b>Prix :</b> Test</td></tr>\n";
      $image_table .= "\n<tr><td><b>Prix :</b> Test</td></tr>\n";
      $image_table .= "\n<tr><td><b>Prix :</b> Test</td></tr>\n";
      $image_table .= "\n</table></td>\n\n\n";

      ++$image_counter;
      ++$cell_counter;
  }

}





$colspan= ($row_counter * 3) - $cell_counter;
$image_table .= ($cell_counter % 3) ?  "<td colspan=$colspan>&nbsp;</td>" : "";
$image_table .= "</tr>\n</table>\n";
echo "$image_table<br><br>";


echo "<br>";
echo "<b>Total Cells w pics: $cell_counter<br>";
echo "<b>Total Rows: $row_counter<br>";
echo "<b>Empty Cells in last row: $colspan";
?>

The other part, that I want to include would be to "paginate" the result of the first script.

Here in my code I want after 2 rows of 3 image, it would do some next -1-2-3 link to go to the next 2 rows of images.

Code: Select all

class display {
	function pagination($rows, $per_page, $current_page, $page_link) {
		global $core,$C;
		
		// Create a Page Listing
		$this->pages = ceil($rows / $per_page);
		
		// If there's only one page, return now and don't bother
		if($this->pages == 1) {
			return;
		}
		
		// Pagination Prefix
                $output .= "<!-- Pagination by Dennis Pedrie. Used by Permission -->";
		$output = "Pages: ";
		
		// Should we show the FIRST PAGE link?
		if($current_page > 2) {
			$output .= "<a href=\"". $page_link ."?page=1/\" title=\"First Page\"><<</a>";
		}
		
		// Should we show the PREVIOUS PAGE link?
		if($current_page > 1) {
			$previous_page = $current_page - 1;
			$output .= " <a href=\"". $page_link .">page=". $previous_page ."/\" title=\"Previous Page\"><</a>";
		}
		
		// Current Page Number
		$output .= "<strong>[ ". $current_page ." ]</strong>";
		
		// Should we show the NEXT PAGE link?
		if($current_page < $this->pages) {
			$next_page = $current_page + 1;
			$output .= "<a href=\"". $page_link ."?page=". $next_page ."/\" title=\"Next Page\">></a>";
		}
		
		// Should we show the LAST PAGE link?
		if($current_page < $this->pages - 1) {
			$output .= " <a href=\"". $page_link ."?page=". $this->pages ."/\" title=\"Last Page\">>></a>";
		}
		
		// Return the output.
		return $output;
	}
}

$display = new display;
echo $display->pagination($row_counter, "2", "1", "http://theponther.peuplies.info/AAA%20TEST/Images/images3.php");
Post Reply