Jump to content

problem with arrays in URL query


wkilc

Recommended Posts

I tired something earlier, with no luck...  so I'm taking a few steps back.  I've been working this one complicated page for days...

 

Here's what used to work:

 

//the current URL
$page_name = $_SERVER["REQUEST_URI"];

//remove any old limit from query before requesting a new limit
$tmp = array();
foreach ($_GET as $fld => $val)
    if ($fld != 'limit')
         $tmp[] = $fld . '=' . $val;
$page_name = $_SERVER['SCRIPT_NAME'] . '?' . implode('&',$tmp);

//sets the number of records to be displayed on the page
<form>
<select name="link">
<option value="<? echo "$page_name" ?>&limit=25">25 records per page</option>
<option value="<? echo "$page_name" ?>&limit=50">50 records per page</option>
<option value="<? echo "$page_name" ?>&limit=100">100 records per page</option>
</select>
</form>

 

This grabs the current URL, and clears the &limit= string before a new limit is introduced to avoid:

www.example.com/test.php?sub=math[]&sub[]=science&limit=25&limit=50&limit=100

 

The problem, is that my URL is already querying an array:

www.example.com/test.php?sub[]=math&sub[]=science

 

So now, when I apply the form above to this URL set the new limit to 50 records per page the URL actually becomes:

www.example.com/test.php?sub=Array&limit=50

Warning: Invalid argument supplied for foreach() ... on line 147

 

I guess I've broken the array?

 

//line 147 
echo "subject:<BR />";
if(!empty($_GET['sub']))
  foreach($_GET['sub'] as $sub) {
    echo "$sub <BR />\n";
  }

 

~Wayne

 

Link to comment
Share on other sites

THANK YOU!!!!

 

I was trying to simplify for the purpose of posting... the problem now is...

 

I've got more than one value:

 

there's also &start= which tells it if we're on the first, second, third, etc.. of the displayed pages.

 

The idea is that I want to clear the one I'm changing while keeping the other.

www.example.com/test.php?sub=math[]&sub[]=science&limit=50&start=50

 

Can't do both like this, right?

 

$page_name = $_SERVER["REQUEST_URI"];
$page_name = preg_replace('/&?limit=\d+/','',$page_name); 
$page_name = preg_replace('/&?start=\d+/','',$page_name);

 

~Wayne

Link to comment
Share on other sites

$page_name = $_SERVER["REQUEST_URI"];
$page_name = preg_replace('/&?limit=\d+/','',$page_name);
$page_name = preg_replace('/&?start=\d+/','',$page_name);

 

This doesn't work for the &start= link comes out as "sub=Array" again.

 

If I change it to this:

$page_name = $_SERVER["REQUEST_URI"];
$page_name = preg_replace('/&?limit=\d+/','',$page_name);
$page_name2 = preg_replace('/&?start=\d+/','',$page_name);

 

Then it obviously doesn't  flush &limit= anymore.

 

I'm sorry...

 

~Wayne

Link to comment
Share on other sites

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.