Page 1 of 1

Help with Friendly URL Pagination

Posted: Thu Feb 20, 2014 8:55 am
by tsalmeida
Hi,


Im implementing friendly url in my website.

Some pages working correctly, but i cant implement the pagination.

i want the friendly url pagination like this "website/categoria/categoriaid/p/number"

can anyone help me?

Follow my codes

code for pagination, work correctly without friendly url

Code: Select all

 require_once 'conexao.php';

$ref=(int)$_GET['idcat'];
$p = $_GET['p'];
if(isset($p)) { $p = $p; } else { $p = 1; }
$qnt = 12;
$inicio = ($p*$qnt) - $qnt;
$sql_select = "SELECT p.*, 
						c.nome_categoria
					  FROM produtos p, categorias c 
					  WHERE c.id_categoria = p.id_categoria 
			AND p.status = 'A'
			AND p.id_categoria = $ref
			ORDER BY p.nota +0 DESC LIMIT $inicio, $qnt";
						  
$sql_query = mysql_query($sql_select);
$sql_select_all = "SELECT p.*, c.* FROM produtos p, categorias c where p.id_categoria = c.id_categoria AND p.id_categoria = $ref";
$sql_query_all = mysql_query($sql_select_all);
$total_registros = mysql_num_rows($sql_query_all);
$pags = ceil($total_registros/$qnt);
$max_links = 5;
$url = "http://localhost/qmmamigavel/categoria/$ref"; 
echo '<div class="boxpg">';
echo '<a href="'.$url . '&p=1" target="_self">Primeira</a> ';
for($i = $p-$max_links; $i <= $p-1; $i++) { 
if($i <=0) { 
 } 
 else { 
 echo '<a href="'.$url . '&p='.$i.'" >'.$i.'</a> ';
 }
 }
 echo '<span class="current">' .$p." ";'</span>';
 for($i = $p+1; $i <= $p+$max_links; $i++) {
if($i > $pags){
}
else { echo '<a href="'.$url . '/p/'.$i.'" >'.$i.'</a> '; 
} 
}
echo '<a href="'.$url . '&p='.$pags.'" >Ultima</a> ';
htaccess code for categoria

Code: Select all

RewriteRule ^categoria/(\d+).*$ view/exibe.php?idcat=$1	
If u need anything more, just tell me.

Thanks.

Re: Help with Friendly URL Pagination

Posted: Thu Feb 20, 2014 9:11 am
by Celauran

Code: Select all

RewriteRule ^categoria/(\d+)/p/(\d+)/?$ view/exibe.php?idcat=$1&p=$2
should do it.

To test rules, check out http://htaccess.madewithlove.be/

Re: Help with Friendly URL Pagination

Posted: Thu Feb 20, 2014 9:28 am
by tsalmeida
Hi Celauran,

Thanks for fast answer, but this code inst work. i did a echo on my get

Code: Select all

 $p = $_GET['p'];
and this isnt work..always the result is 1.

Can u help me with this too?

Thanks

Re: Help with Friendly URL Pagination

Posted: Thu Feb 20, 2014 10:10 am
by Celauran
What URI were you using?

Code: Select all

RewriteRule ^categoria/(\d+)/p/(\d+)/?$ view/exibe.php?idcat=$1&p=$2
http://www.example.com/categoria/10/p/2 should give you $_GET['p] == 2

Re: Help with Friendly URL Pagination

Posted: Thu Feb 20, 2014 10:27 am
by tsalmeida
that is exactly how I am using, but my get always return 1.

without friendly url works corretly

Re: Help with Friendly URL Pagination

Posted: Thu Feb 20, 2014 11:03 am
by Celauran
There's something else at play, then. Works perfectly fine for me.

.htaccess

Code: Select all

RewriteEngine On
RewriteRule ^categoria/(\d+)/p/(\d+)/?$ exibe.php?idcat=$1&p=$2
exibe.php

Code: Select all

<pre><?php print_r($_GET); ?></pre>
URI: example.com/categoria/10/p/2

output

Code: Select all

Array
(
    [idcat] => 10
    [p] => 2
)

Re: Help with Friendly URL Pagination

Posted: Thu Feb 20, 2014 8:57 pm
by tsalmeida
Hi,

I found the error on GET.

in my htaccess i have

Code: Select all

RewriteRule ^categoria/(\d+).*$ view/exibe.php?idcat=$1
.this rule works for exibe.php "website/categoria/categoriaid/"


If i remove the "/" from this rule my get page works great,