Jump to content

Getting a Url for Pagination


cooldude832

Recommended Posts

I did this

<?php
$url = $_SERVER['PHP_SELF']."?";
$and_set = 1;
foreach($_GET as $key => $value){
	if($key == "limit"){}
		if($and_set != 1){
			$url .= $key."=".$value;
			$and_set = 1;
		}
		else{
			$url .= "&".$key."=".$value;
		}
}
?>

to get a formatted url for my pagination, however I can't help to think it could be simpler.  Then again any simpler method would return the full query string and you couldn't mod the limit variable as easily.

I'll show you the whole thing, tell me if u see an improvment

<?php
$url = $_SERVER['PHP_SELF']."?";
$and_set = 0;
foreach($_GET as $key => $value){
	if($key == "limit"){}
		if($and_set != 1){
			$url .= $key."=".$value;
			$and_set = 1;
		}
		else{
			$url .= "&".$key."=".$value;
		}
}
//This is the output it can be anywhere below the above (I like it above and below the search results)

if($page >1){
	$temp = $page-1;
	echo "<a href=\"".$url."&limit=".$temp."\">Prev</a>";
}
for($i = 1; $i<=$num_pages; $i++){
	if($i == $page){
		echo "<span class=\"current_page\">";
	}
	echo " ";
	echo "<a href=\"".$url."&limit=".$i."\">-".$i."-</a>";
	echo " ";
	if($i == $page){
		echo "</span>";
	}
}
if($page < $num_pages){
	$temp = $page+1;
	echo "<a href=\"".$url."&limit=".$temp."\">Next</a>";
}
?>

because it outputs the whole html part

<a href="/redesign/search.php?search=yes&type=2&location=all&month=Any&year=2002&limit=2&limit=1">Prev</a>
 <a href="/redesign/search.php?search=yes&type=2&location=all&month=Any&year=2002&limit=2&limit=1">-1-</a> 

<span class="current_page"> <a href="/redesign/search.php?search=yes&type=2&location=all&month=Any&year=2002&limit=2&limit=2">-2-</a> 
</span> <a href="/redesign/search.php?search=yes&type=2&location=all&month=Any&year=2002&limit=2&limit=3">-3-</a> 
 <a href="/redesign/search.php?search=yes&type=2&location=all&month=Any&year=2002&limit=2&limit=4">-4-</a> 
<a href="/redesign/search.php?search=yes&type=2&location=all&month=Any&year=2002&limit=2&limit=3">Next</a>

 

If I don't sort the limit out of it, then can't adjust its value in the ouptutted string "easily" this method lets me edit it very easily in the url, as its not part of it to start.

dude you really have to learn it now i unders tand what your trying to obtain with that limit you want to use function that is dynamic tru the help of get limit right?

and this

foreach($_GET as $key => $value){
	if($key == "limit"){}
		if($and_set != 1){
			$url .= $key."=".$value;
			$and_set = 1;
		}
		else{
			$url .= "&".$key."=".$value;
		}
}

why need to loop

since you use $key == "limit" why not access them directly nad get value and key

 

http://www.php.net/manual/en/function.array-keys.php

function paging($query,$pagelimit,){
$_mysql = mysql_query($query);
$rows = mysql_num_rows($_mysql) / $pagelimit;

$limit = $_GET['page']*$pagelimit;
$mysql_display = mysql_query($query.'limit '.$limit-$pagelimit.','.$limit);
while(mysql_fetch_assoc($mysql_display)){
	//display record on that field
}
//limit pages to be displayed in 5
    if ($rows >1){

	$ivalue = ($_GET['page'] >5 )?$_GET['page']-4:1;// determine where to start 
	for($i = $ivalue; $i <=$rows; $i++){
                        ++$ctr;
		if ($ctr >=5){
		//display here your link for paging
		 $href = echo "<a href=\"".$_SERVER['PHP_SELF']."?page = $i\">-".$i."-</a>";
		}
	}
}
}

note not tested

no returns i just want to show you how im going to do it without a class or do it on my way

i know you can edit this if this make sense dude post here your updated code if you will use my idea

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.