Jump to content

Problem with pagination


milanello72

Recommended Posts

Hello! Please, excuse me because I will put a big code here, but I hope that you will understand me!

I want to make a pagination and I addapt links for the files.

******************

If I want to transform a link like

index.php?page=2&pagina=teams&id_team=1&numele=Juventus

 

in a link like

"/".$pagina."/".$id_team."/".$numele.".html";

So without page in link

******************************

In .htaccess I WILL HAVE

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9.\s]+)/?.html$ index.php?pagina=$1&id_team=$2&numele=$3

*****************************

My file with the pagination is teams.php:

$sql29="select * from pozesq where id_team='$id_team'";
 $resursa29=mysql_query($sql29);
 $numar = mysql_num_rows($resursa29);

 $page = 1;				 //if no page var is given, default to 1.
 $adjacents = 3;

 $limit = 20;								 //how many items to show per page

 if(isset($_GET['page']))
 {
 $page = $_GET['page'];
$lastpage = ceil($numar/$limit);
	 $start = ($page - 1) * $limit;
 }			 //first item to display on this page
 else
 {
 $start = 0;
 }


 $sql1="select * from pozesq where id_team='$id_team' order by id_pozasq asc LIMIT $start, $limit";
 $resursa1=mysql_query($sql1);
 $numar=mysql_num_rows($resursa1);

...... show photos ......

$prev = $page - 1;						 //previous page is page - 1
$next = $page + 1;						 //next page is page + 1
$lastpage = ceil($total_pages/$limit);	 //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;

$self = $_SERVER['PHP_SELF'];
$targetpage = $self;

$path='pagina=teams&id_team='.$id_team.'&numele='.$numele.'';

include('balariipagination.php');

*********************************

And the file balariipagination.php is

$pagination = "";

if($lastpage > 1)
{
 $pagination .= "<div class=\"pagination\">";
 //previous button
 if ($page > 1)
	 $pagination.= "<a href=\"$targetpage?page=$prev&".$path."\">« previous</a>";
 else
	 $pagination.= "<span class=\"disabled\">« previous</span>";

 //pages
 if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
 {
	 for ($counter = 1; $counter <= $lastpage; $counter++)
	 {
		 if ($counter == $page)
			 $pagination.= "<span class=\"current\">$counter</span>";
		 else
			 $pagination.= "<a href=\"$targetpage?page=$counter&".$path."\">$counter</a>";
	 }
	 }
.........
.........
.........
etc

******************************

**********************************

I repeat that i want to obtain a link like

"./".$pagina."/".$id_team."/".$numele.".html";

How could i obtain that?

1) in file teams.php i will change $path in ???

$path="/".$pagina."/".$id_squadra."/".$numele.".html";

********

2) Must i change and $self = $_SERVER['PHP_SELF']; in teams.php in ....?

$_SERVER['REQUEST_URI']

**********

3) in this situation how it will change the file balariipagination.php ??? Tell me for exemple how will I change...?

href=\"$targetpage?page=$prev&".$path."\"

??

thank you very much and i hope that you will understand my message!

Link to comment
https://forums.phpfreaks.com/topic/272622-problem-with-pagination/
Share on other sites

The RewriteRule has a /? that doesn't belong (otherwise "/teams/1/Juventus/.html" would be valid). It should also have the QSA flag which I'll explain in a second, and most likely L too.

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9.\s]+).html$ index.php?pagina=$1&id_team=$2&numele=$3 [L,QSA]

 

I didn't look at the pagination code because it doesn't sound like you have a problem with it per se, just the links it creates.

 

1. That $path looks correct... mostly.

2. You should construct the URL yourself from scratch - basically, use $path. You should urlencode() the parts to protect yourself against malicious URLs and XSS.

$path = "/" . urlencode($pagina) . "/" . urlencode($id_squadra) . "/" . urlencode($numele) . ".html";
$targetpage = $path; // or you could just use $path

3. Since $targetpage contains the $path (which has the $pagina, $id_squadra, and $numele) all that's left to add is the page number.

"href=\"$targetpage?page=$prev\""

 

As for QSA:

The substitution URL (the index.php one) includes a query string; when you do that then mod_rewrite will assume that's all you want and will discard anything else in the original URL. That means the ?page=$prev would be lost. The QSA ("query string append") tells mod_rewrite to actually keep what was there before, so the URL rewriting will go from

/teams/1/Juventus.html?page=2

to

/index.php?pagina=1&id_team=1&numele=Juventus&page=2

thank u very much for your answer, I have put your suggestions, but I will obtain a link like this:

http://127.0.0.1/calcioitalia/teams/1/Juventus%20F.C.%20Torino.html?page=2&/teams/1/Juventus+F.C.+Torino.html

So I think that my problem is:

"href=\"$targetpage?page=$prev\""

how could I solve this problem?

thank u again!

Excuse me, I have this path:

http://127.0.0.1/calcioitalia/teams/1/Juventus%20F.C.%20Torino.html?page=2

but in the page=2 I have the content of the page 1, and in the page 3 I have the content of the same page 1.

So I need pagina, id_team and numele to change the page.....

what can I do?

Thank u!

It WORKS! I have some conditions:

1) If I use in balariipagination.php $path=$path='pagina='.$pagina.'&id_squadra='.$id_squadra.'&numele='.$numele.'';

$pagination.= "<a href=\"$targetpage?page=$prev&".$path."\">« previous</a>";

2)

$self = $_SERVER['PHP_SELF'];
$targetpage = $self;

But in this situation I will have this link:

calcioitalia/teams/2/A.C. Milan.php?page=2&pagina=teams&id_squadra=2&numele=A.C.%20Milan

 

So I will have the path with php. (without .html)

**********************************************************************

**********************************************************************

I would like to obtain a path without &id_squadra=2&numele=A.C.%20Milan and without php (so with .html) and I made:

1)

$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";

so without $path

2)

$self = $_SERVER['REQUEST_URI'];
$targetpage = $self;

In this situation i have a path calcioitalia/teams/2/A.C. Milan.html?page=2 but in page=2

I have the content of the page 1 and isset($_GET['page']) NOT WORKING.

So what can I do to set $_GET['page'] ??? This is the most important thing!

thank u again!

It works %50 :)

I have put:

$pagination.= "<a href=\"$targetpage?page=$next\">« previous</a>";

And

$self = $_SERVER['REQUEST_URI'];
$targetpage = $self;

But... i will go here calcioitalia/teams/1/Juventus F.C. Torino.html and after that I will enter in page=2 and it works

(I will have calcioitalia/teams/1/Juventus F.C.Torino.html?page=2 )

and if I will go in page=3 I will obtain this link calcioitalia/teams/1/Juventus F.C. Torino.html?page=2?page=3 with the content of page=2

And it was set this

$_GET['page']=2?page=3

so this is the problem:

$pagination.= "<a href=\"$targetpage?page=$next\">« previous</a>";

how could i solve this problem?

Hello, Requinix! Please excuse me because I did not read your message carefully...

IT WORKS!!! I used your script and IT WORKS!

it is fault of my english! And maybe I don't have more experience in PHP!

I have a question for u,: if I use urlencode how to decode from url?? (I want to use and urlencode in my script)

Thank u very much! :)

I have some problems:

1) If I use in horizontalmenu.php

href="/calcioitalia/'.urlencode($pagina).'/'.urlencode($id_squadra).'/'.urlencode($numele).'.html">

and I try to load calcioitalia/teams/2/A.C.+Milan.html, doesn't work and I will receive "The requested URL /calcioitalia/teams/2/A.C.+Milan.php was not found on this server."

Why does not it work at me?

*******************************

2) if i use

href="/calcioitalia/'.rawurlencode($pagina).'/'.rawurlencode($id_squadra).'/'.rawurlencode($numele).'.html"

It works!

******************************

3) I would like to obtain a link like this calcioitalia/teams/2/A.C.-Milan.html

How could I obtain this link?

***********************************

Thank u!

 

 

Hello, Requinix! I found this function:

function seoUrl($string) {	
 //Strip any unwanted characters
 $string = preg_replace("/[^A-Za-z0-9_\s-]/", "", $string);
 //Clean multiple dashes or whitespaces
 $string = preg_replace("/[\s-]+/", " ", $string);
 //Convert whitespaces and underscore to dash
 $string = preg_replace("/[\s_]/", "-", $string);
 return $string;
}

and in horizontalmenu.php I will have:

href="/calcioitalia/'.seoUrl($pagina).'/'.seoUrl($id_squadra).'/'.seoUrl($numele).'.html"

But how will display teams.php in index.php?? I use

if (isset($_GET['pagina']) and $_GET['pagina'] == 'teams' and isset($_GET['id_squadra']) and isset($_GET['numele']))
{
 include('./includes/teams.php');
}

but doesn't work because in menuhorizontal i used seoUrl.

I have this message: The requested URL /calcioitalia/teams/2/AC-Milan.php was not found on this server.

How could I solve this problem?

But how will display teams.php in index.php?? I use

if (isset($_GET['pagina']) and $_GET['pagina'] == 'teams' and isset($_GET['id_squadra']) and isset($_GET['numele']))
{
    include('./includes/teams.php');
}

but doesn't work because in menuhorizontal i used seoUrl.

I have no idea what you're talking about.

 

I have this message: The requested URL /calcioitalia/teams/2/AC-Milan.php was not found on this server.

The file extension is wrong.

i think that the problem is that I must to decode $pagina, $id_squadra, $numele in index.php, because in horizontalmenu.php I have

href="/calcioitalia/'.seoUrl($pagina).'/'.seoUrl($id_squadra).'/'.seoUrl($numele).'.html"

and in index.php, and teams.php I need the originally $pagina, $id_squadra, $numele

So how could I decode?

OK! I will present the problem...

My link is with $numele=$row['numele']

href="/calcioitalia/'.$pagina.'/'.$id_squadra.'/'.$numele.'.html"

and in htacces i have

Options +FollowSymlinks

RewriteEngine on

php_flag session.auto_start 1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9.\s]+)/?.php$ index.php?pagina=$1&id_squadra=$2&numele=$3 [L,QSA]

RewriteRule ^(.*)\.html$ $1.php [nc]

ErrorDocument 404 /errors/404
ErrorDocument 403 /errors/403
ErrorDocument 500 /errors/500

and the page calcioitalia/teams/1/Juventus F.C. Torino.php works!!!

*********************************************************************************************

My link is with $numele=urlencode($numele);

href="/calcioitalia/'.$pagina.'/'.$id_squadra.'/'.$numele.'.html"

and in htacces i have

Options +FollowSymlinks

RewriteEngine on

php_flag session.auto_start 1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9.\s]+)/?.php$ index.php?pagina=$1&id_squadra=$2&numele=$3 [L,QSA]

RewriteRule ^(.*)\.html$ $1.php [nc]

ErrorDocument 404 /errors/404
ErrorDocument 403 /errors/403
ErrorDocument 500 /errors/500

and the page /calcioitalia/teams/1/Juventus+F.C.+Torino.php NOT works!!!

I have the error 404 Not Found

The requested URL /calcioitalia/teams/1/Juventus+F.C.+Torino.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

*******************

So why doesn't work with urlencode???

***

Could you present me one of your examples that work?

Thank you for all!

OK! I will present the problem...

My link is with $numele=$row['numele']

href="/calcioitalia/'.$pagina.'/'.$id_squadra.'/'.$numele.'.php"

and in htacces i have

Options +FollowSymlinks

RewriteEngine on

php_flag session.auto_start 1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9.\s]+)/?.php$ index.php?pagina=$1&id_squadra=$2&numele=$3 [L,QSA]

RewriteRule ^(.*)\.html$ $1.php [nc]

ErrorDocument 404 /errors/404
ErrorDocument 403 /errors/403
ErrorDocument 500 /errors/500

and the page calcioitalia/teams/1/Juventus F.C. Torino.php works!!!

*********************************************************************************************

My link is with $numele=urlencode($numele);

href="/calcioitalia/'.$pagina.'/'.$id_squadra.'/'.$numele.'.php"

and in htacces i have

Options +FollowSymlinks

RewriteEngine on

php_flag session.auto_start 1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/([a-zA-Z0-9.\s]+)/?.php$ index.php?pagina=$1&id_squadra=$2&numele=$3 [L,QSA]

RewriteRule ^(.*)\.html$ $1.php [nc]

ErrorDocument 404 /errors/404
ErrorDocument 403 /errors/403
ErrorDocument 500 /errors/500

and the page /calcioitalia/teams/1/Juventus+F.C.+Torino.php NOT works!!!

I have the error 404 Not Found

The requested URL /calcioitalia/teams/1/Juventus+F.C.+Torino.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

*******************

So why doesn't work with urlencode???

***

Could you present me one of your examples that work?

Thank you for all!

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.