Jump to content
N3lson

[Resolvido] Paginação de Ficheiros

Recommended Posts

N3lson

Boas tenho este código

		$dir = "imagens/galerias/{$cat}";
if ($dh = opendir($dir))
		 {
		   while (($file = readdir($dh)) !== false)
			   {
				if($file !='.' && $file !='..')
				  {
				   echo'
					   <li class="border_fourcol">
						 <a href="'.$dir.'/'.$file.'" title="prettyPhoto"><img src="'.$dir.'/'.$file.'" alt="" /></a>
					   </li>';
	  }
    }
  closedir($dh);
}

Até aqui tudo bem, mas agora gostaria de fazer a paginação destas fotos.

Alguma dica ????

Edited by N3lson

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites
Rui Carlos

Algo deste género deve funcionar:

$page = $_GET['page'];
$per_page = 20;
$i = 0;
while (($file = readdir($dh)) !== false)
{
 if($file !='.' && $file !='..') {
   if(((int)($i / $per_page)) == ($page + 1)) {
     echo ...
   }
   $i++;
 }
}

(O código provavelmente tem erros de sintaxe :D . Adicionalmente, há pelo menos uma optimização que é conveniente aplicar.)

Share this post


Link to post
Share on other sites
N3lson

nao consegui : :(


You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites
N3lson

consegui, ficou assim

			    $cat = isset($_GET['cat']) ? $_GET['cat'] : false;
		    $dir = "images/galerias/{$cat}";

		    if ($cat && file_exists($dir))
		    {

			    // LISTAR FOTOS DAS CATEGORIAS
			    if ($dh = opendir($dir)) // abrir pasta
			    {
				    while (($file = readdir($dh)) !== false) { /
					    if ($file != '.' && $file != '..' && $file != 'thumbs.db') {
						    if (fnmatch('*.jpg', $file)) {

							    $files_array[] = $file;
						    }
					    }
				    }
			    }
			    $count = 0;

			    $limit = 24;

			    $total_pages = ceil(count($files_array) / $limit);

			    $page = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1;

			    if ($page < 1 || $page > $total_pages) $page = 1;

			    $start = ($page - 1) * $limit;

			    echo'<h2>GALERIA: <u>' . strtoupper($cat) . '</u></h2>';
			    echo '<span class="pag">Página: <u>'.$page.'</u> de <strong>'.$total_pages.'</strong></span>
				  <ul class="gallery clearfix">';

				    for ($i = $start; $i < count($files_array); $i ++)
				    {
						 $count ++;
					    echo'<li class="border_fourcol">
							  <a href="' . $dir . '/' . $files_array[$i] . '" title="prettyPhoto"><img src="' . $dir . '/' . $files_array[$i] . '" alt="" /></a>
							 </li>';
					    if ($limit == $count) break;
				    }
			 echo'</ul>';
		   echo '<div class="nav" style="text-align: center;">';
			 if ($page > 1)
			   {
			    echo'<a href="?cat='.$cat.'&pagina='.($page - 1).'"> <img src="images/ant.gif" alt="<<"></a>  ';
			   }
			 if ($total_pages > $page)
			 {
			   echo '<a href="?cat='.$cat.'&pagina='.($page + 1).'"> <img src="images/seg.gif" alt=">>"></a>';
			 }
			    echo'</div>';
		    }


You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. 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.