Pagination links working only once- PHP

Hi,

I have a simple pagination system that is currently working just for the first link.
Ex:

If I have this  <<Previous 1 2 3 Next>>, I can choose any of them and it will work properly, but once I´m in the new page, the links are not working anymore.

The format that all the links should have is
<a href=\"$targetpage?$valorpaginacion&page=

But after I click any option of the pagination, the links structure turns into
<a href=\"$targetpage?page=

Pagination code

<?php  ///// PAGINATION CODE STARTS
	
$valorpaginacion = "keyword=$url_pagination&categories=$id_cat_paginacion&regions=$id_reg_paginacion&radio=$radio" ;
var_dump($valorpaginacion);
var_dump($keywords);
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;
	
	/* Setup vars for query. */
	$targetpage = "search.php"; 	//your file name  (the name of this file)
	$limit = 50; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$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;						//last page minus 1
	
	
	$res= $mysqli->query($sql2 .$start.','. $limit);

	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$prev\"><< 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?$valorpaginacion&page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$lastpage\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$lastpage\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$counter\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?$valorpaginacion&page=$next\">next >></a>";
		else
			$pagination.= "<span class=\"disabled\">next >></span>";
		$pagination.= "</div>\n";		
	}

Open in new window

Klaus AndersenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jagadishwor DulalBraces MediaCommented:
Did Your $targetpage = "search.php";       is same in another page?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
We seem to have a code fragment that does not contain enough information to be able to reproduce the problem.  
http://www.laprbass.com/RAY_temp_pj_.php

Two suggestions: Add error_reporting(E_ALL) to the top of the script and learn about how to use the SSCCE when you post questions here.  If I cannot install your script and run it to see the behavior, I almost certainly cannot help you debug it.

An important concept of programming is the idea of "unit testing" which often goes hand-in-hand with the use of "mock objects."  In building your SSCCE for this example, you would want to create a mock object that represents the query results set.  When we have that we will be able to make quick progress!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.