Forum Moderators: coopster

Message Too Old, No Replies

Help.. passing parameters

(form GET)

         

fabiox

5:10 pm on May 17, 2005 (gmt 0)

10+ Year Member



Hi. I'm trying to develop a search with the follow schema:

<input type="checkbox" name="jornals[]" value="1">
<input type="checkbox" name="jornals[]" value="2">

The method of the form is GET.
In first page it is cool, but for second page for third the url is cumulative.See the code:

-----------------------------------------------
if(!isset($page) OR $page=="1"){

$page=1;
$next_page=$page+1;
$link_next=''.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&page='.$next_page.'';
} else {

$prev_page=$page-1;
$link_prev=''.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&page='.$prev_page.'';

$next_page=$page+1;
$link_next=''.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&page='.$next_page.'';
}
}

The link in the first page:
search.php?jornals%5B%5D=1&jornals%5B%5D=2&page=2

And second for third
search.php?jornals%5B%5D=1&jornals%5B%5D=2&page=2&page=3

(CORRECT: search.php?jornals%5B%5D=1&jornals%5B%5D=2&page=3)

Any other PHP Variable works?

StupidScript

5:30 pm on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are wondering how NOT to pass the old "page" parameter, just the new one?

I ended up looping through

argv
and NOT writing the old parameter, in a similar situation:

$qstring="";

foreach($argv as $val) {

if (eregi("page=",$val)) { continue; }

$qstring .= $val."&";

#$qstring ends up with an & at the end

}

$link_next=''.$_SERVER['PHP_SELF'].'?'.$qstring.'page='.$next_page.'';

$link_prev=''.$_SERVER['PHP_SELF'].'?'.$qstring.'page='.$prev_page.'';

Hope that helps!

webkami

4:13 pm on May 18, 2005 (gmt 0)

10+ Year Member



Use this handy function I developed for this purpose.

For your case you can use it like

$link_prev='page='.$prev_page.getQueryString("page","&");

It will automatically add & after page=1 and then rest of parameters

example
start page=1&a=0&b=3

after this function

page=n&a=0&b=3

where n is your new page number.


<?

function getQueryString($notThis="=",$preString)
{
//set default query string as pre string
$qStr=$preString;
$arr_notThis=explode(",",$notThis);
//go through each url parameter
$qStrings=split("&",$_SERVER['QUERY_STRING']);

foreach ($qStrings as $qString)
{
$key_val=split("=",$qString);
$found=false;
foreach($arr_notThis as $val1 => $val2){
if (strcmp($key_val[0],$val2)==0) $found=true;
}
// if the parameter key is NOT compared with "notwanted", add it into new string
if (!$found && isset($key_val[0]) && isset($key_val[1]))
$qStr.= $key_val[0]."=".$key_val[1]."&amp;";
}

//chop off last & if length is greater than zero
$len=strlen($qStr);
if($len>0) $qStr=substr($qStr,0,$len-5);

//return the new created string
return $qStr;

}?>

fabiox

5:51 pm on May 19, 2005 (gmt 0)

10+ Year Member



Thanks :)