Jump to content

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'm trying to reproduce a url for the next, prev 1, 2, 3 ,4 ,5 links you put on a pagination page so you can get to the rest of the results,  I use the foreach to get all the get variables and I restricted the limit so that I can modify it manully

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

or maybe you can have your paging in a function and add a parameter that will hold your limit value as well as the link location 

for($i = 1; $i<=$num_pages; $i++){

where do you get $num_pages

actually paging is simple so no hard work needed

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.