...

View Full Version : [RESOLVEd] Paginate a result



Peuplarchie
11-12-2007, 04:48 AM
Good day to you,
I need to paginate the result of this code :




<?php
$image_counter = -1;
$row_counter = 0;
$cell_counter = 0;
$show=array('.jpg','.JPG','.gif','.GIF');

$val= $_GET['folder'];
$val1 = $_GET['folder'];
$val0 = $val1;
$val.="/";

$path = "$val";
$path2 = str_replace("Pic-S", "Pic-B", $val);
$dirshort = substr($val,20);
$dirshort0 = $val1;

$comicon = "http://ponder.peuplies.info/V_0-1/Muraille/Change-comment-icon/comment_icon_change.txt";
$fh1 = fopen($comicon, 'r');
$outputt1 = fgets($fh1);
$output1 = str_replace("../..", "http://ponder.peuplies.info/V_0-1", $outputt1 );
fclose($fh1);




$dir_handle = @opendir($path) or die("Unable to open $path");
$image_table = "<table valign=\"top\" align=\"center\" width=\"100%\">\n";
$image_table .= "\n<tr><td bgcolor=\"#cccccc\" align=\"center\" colspan=\"3\"><table valign=\"top\" align=\"center\" width=\"100%\" border=\"0\" cellspacing=\"0\">\n";
$image_table .= "\n<tr><td bgcolor=\"#000033\" class=\" white00\" align=\"left\"><b class=\"white00\">Photos in - Photos dans :</b> $dirshort</td>";
$image_table .= "\n <td bgcolor=\"#000033\" align=\"right\" ><br></td></tr>\n";
$image_table .= "\n<tr><td bgcolor=\"#cccccc\" class=\" white00\" align=\"right\" colspan=\"2\">\n";
$image_table .= "<script type=\"text/javascript\">ajaxinclude(\"http://ponder.peuplies.info/V_0-1/Muraille/Change-shoppingcart-button/change_shoppingcart_button.txt\")</script>";
$image_table .= "\n</td></tr>\n";
$image_table .= "\n</table><tr>\n";
while (false !== ($file = readdir($dir_handle))) {
if(in_array(substr($file,-4,4),$show)){
if(!(($image_counter + 1) % 3)){
$row_counter++;
}


$newpath0 = substr($file,0,-4);
$newpath00 = substr($file,0,-4);

$image_table .=(($image_counter + 1) % 3)? "" : "</tr>\n<tr><td><br></td></tr><tr valign=\"top\">";
$image_table .= "\n<td valign=\"top\" width=\"180\"><table width=\"180\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" align=\"center\">\n";
$image_table .= "\n<tr><td valign=\"top\" bgcolor=\"#ffff00\" width=\"180\"><table width=\"180\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\" align=\"center\">\n";
$image_table .= "\n<tr><td valign=\"top\" bgcolor=\"#003366\" width=\"180\"><table width=\"180\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\" align=\"center\">\n";
$image_table .= "\n<tr><td bgcolor=\"#000033\" align=\"center\"width=\"180\"><b>$newpath0</b></td></tr>\n";
$image_table .= "\n<tr><td bgcolor=\"#000000\" width=\"180\"><a href='http://ponder.peuplies.info/V_0-1/Art/$path2$file' target='_blank' rel=\"thumbnail\"><img src=\"http://ponder.peuplies.info/V_0-1/Art/$path$file\" width=\"180\" border=\"0\"></a></td></tr>\n";
$image_table .= "\n<tr><td bgcolor=\"#000033\" align=\"center\" width=\"180\"><IMG SRC=\"http://ponder.peuplies.info/V_0-1/Art/minus.gif\" CLASS=\"showstate\" ONCLICK=\"expandcontent(this, 'sc".$newpath0."')\" align=\"right\"><b>Information</b></td></tr>\n";
$image_table .= "\n</table>\n";
$image_table .= "\n<div id=\"sc".$newpath0."\" class=\"switchcontent\" width=\"180\">\n";
$image_table .= "\n<table border=0 align=center cellspacing=0 cellpading=0 width=\"180\">\n";
$image_table .= "\n<tr><td bgcolor=\"#ffffff\" align=\"left\" class=\"black0\" width=\"180\"><b class=\"black0\" width=\"180\">Description :<br></b><script type=\"text/javascript\">ajaxinclude(\"http://ponder.peuplies.info/V_0-1/Art/Pictures/Pics/Commentaires/$newpath0.txt\")</script></td></tr>\n";
$image_table .= "\n<tr><td bgcolor=\"#ffffff\" align=\"left\" class=\"black0\" width=\"180\"><br></td></tr>\n";
$image_table .= "\n<tr><td valign=\"top\" bgcolor=\"#ffffff\" width=\"180\"><table width=\"180\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\" align=\"center\">\n";
$image_table .= "<tr><td bgcolor=\"#ffffff\" align=\"left\" width=\"180\" valign=\"top\"><a href=\"http://ponder.peuplies.info/V_0-1/Art/rater0.php?var1=$newpath0&var00=$path$file\" target=\"autre\" title=\"Rate it! - Noter la !\" alt=\"Rate it!\" class=\"black000\"><script type=\"text/javascript\">ajaxinclude(\"http://ponder.peuplies.info/V_0-1/Art/rater1.php?var1=$newpath0&var00=$path$file\")</script></a></td>";
$image_table .= "\n<td bgcolor=\"#ffffff\" align=\"center\" class=\"black0\" width=\"180\" colspan=\"2\"><script type=\"text/javascript\">ajaxinclude(\"http://ponder.peuplies.info/V_0-1/Art/Pictures/Pics/Prix/$newpath0.txt\")</script><script type=\"text/javascript\">ajaxinclude(\"http://ponder.peuplies.info/V_0-1/Art/Pictures/Pics/Vente/$newpath0.txt\")</script></td></tr>\n";
$image_table .= "\n</table>\n";
$image_table .= "\n<tr><td valign=\"top\" bgcolor=\"#ffffff\" width=\"180\"><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
$image_table .= "\n<tr><td bgcolor=\"#ffffff\" align=\"left\" width=\"180\"><a href=\"http://ponder.peuplies.info/V_0-1/Art/commentaires.php?var001=$newpath0&var002=$path$file\" target=\"autre\" title=\"Add comments - Ajout de commentaires.\" alt=\"Add comments - Ajout de commentaires\" class=\"white000\"><img src=\"".$output1."\" border=\"0\"></a></td>";
$image_table .= "</td></tr>\n";
$image_table .= "\n</table>\n";
$image_table .= "\n</div>\n";
$image_table .= "\n</table>\n";
$image_table .= "\n</table>\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 valign=top>&nbsp;</td>" : "";
$image_table .= "</tr>\n</table>\n";
echo "$image_table<br><br>";

?>






This code read a given directory and output image with info formated in rows of 3.

Now I need that the code after 4 row would create a link to go to the nexts images.

Thanks !

JustBlaze
11-12-2007, 05:47 AM
Quick question, What's the /n used for. I've never used it and have been wondering what it is.

Peuplarchie
11-12-2007, 06:01 AM
http://www.petefreitag.com/cheatsheets/ascii-codes/

\n = New Line
\r = cariage retur or something like that.

Peuplarchie
11-12-2007, 06:02 AM
sometime it work very well.....

JustBlaze
11-12-2007, 06:39 AM
so it makes the source look better in a browser?

Inigoesdr
11-12-2007, 07:19 AM
so it makes the source look better in a browser?

Yes, or you can use it in file/text output to split things to new lines.

Peuplarchie
11-12-2007, 07:34 AM
No it don't work, I playing with directory images listing .
Variable are set vor a variable directory

Inigoesdr
11-12-2007, 07:45 AM
No it don't work, I playing with directory images listing .
Variable are set vor a variable directory

... what?
All you need to do pagination is the total number of files in the directory, the number of items per page, and the current page(if any). To find out the number of files in the directory look at the examples in the manual (http://php.net/readdir).

Here is some example code I have that prints a pagination string:

/**
* paginate - This function uses the $_GET['page'] variable to pass the page number to the next/prev page
*
* @param int $currentPage Current Page
* @param int $totalPages Total Pages
* @param string $link Hyperlink with a ? at the end if it doesn't have any $_GET vars
*
* @return string
*/
function paginate($currentPage, $totalPages, $link)
{
$prev = $pages = $next = '';

if($currentPage == 0)
$currentPage = 1;
elseif($currentPage > 1)
$prev = sprintf('<a href="%s&page=%d">&laquo; Previous</a> ', $link, $currentPage - 1);

for ($i = 1; $i <= $totalPages; $i++)
{
if($currentPage == $i)
$pages .= $i . ' ';
else
$pages .= sprintf('<a href="%s&page=%d">%d</a> ', $link, $i, $i);
}

if($currentPage < $totalPages)
$next = sprintf('<a href="%s&page=%d">Next &raquo;</a>', $link, $currentPage + 1);

return $prev . $pages . $next;
}
// example:
echo paginate((int) $_GET['page'], 10, $_SERVER['SCRIPT_NAME'] . '?');
So all you need to do is count the files, and replace the 10 with it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum