Jump to content

remove old value from query


wkilc

Recommended Posts

I'm a noob, tring to reverse engineer some current code.

 

This code is grabbing the current URL:

 

<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>

 

This form is adding "&limit=50" to the URL, to show 25, 50 or 100 results per page:

<form>
<select name="link">
<option <? echo "$select25" ?> value="<? echo curPageURL(); ?>&limit=25">25 records per page</option>
<option <? echo "$select50" ?> value="<? echo curPageURL(); ?>&limit=50">50 records per page</option>
<option <? echo "$select100" ?> value="<? echo curPageURL(); ?>&limit=100">100 records per page</option>
</select>
</form>

 

The problem is here (I think).  This is supposed to flush the old limit before requesting a new one... but right now, I'm getting another one tacked on with each new request:

www.website.com/members?sub[]=math&sub[]=science&lev[]=middleschool&limit=25

www.website.com/members?sub[]=math&sub[]=science&lev[]=middleschool&limit=25&limit=50

www.website.com/members?sub[]=math&sub[]=science&lev[]=middleschool&limit=25&limit=50&limit=100

<?php 
//remove any old limit from query
$tmp = array();
foreach ($_GET as $fld => $val)
    if ($fld != 'limit')
         $tmp[] = $fld . '=' . $val;
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] . '?' . implode('&',$tmp);
?>

 

Any ideas?

 

Thanks.

 

~Wayne

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/211230-remove-old-value-from-query/
Share on other sites

There's also the stristr function which you can locate the first occurence of "&limit=" and pull everything before that using one function and putting true as the third param.  Here's an example from php.net (http://www.php.net/manual/en/function.stristr.php)

 

<?php

  $email = '[email protected]';

  echo stristr($email, 'e'); // outputs [email protected]

  echo stristr($email, 'e', true); // As of PHP 5.3.0, outputs US

?>

 

 

Thank you.

 

Again, I'm pretty green...

 

<?php
  $curPageURL = $curPageURL;
  echo stristr($curPageURL, '&limit='); // outputs [email protected]
  echo stristr($curPageURL, '&limit=', true); // As of PHP 5.3.0, outputs US
?>

 

gives me:

 

Warning: Wrong parameter count for stristr()

 

Thanks.

 

~Wayne

Just realized that there might be variables after &limit= that I would want to keep...

 

So I'm back to:

 

//remove any old limit from query
$tmp = array();
foreach ($_GET as $fld => $val)
    if ($fld != 'limit')
         $tmp[] = $fld . '=' . $val;
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] . '?' . implode('&',$tmp);

 

Just trying to flush the current "limit"... this has worked in other instances for me before.

 

~Wayne

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.