开发者

Multiple Variables in PHP URL

开发者 https://www.devze.com 2023-03-09 09:01 出处:网络
I am new to PHP and this problem has stumped me. I have a situation where I have a URL like this http://www.test.com/test.php?month=5&year=5&pn=4&ItemsPerPage=15&sort=5

I am new to PHP and this problem has stumped me.

I have a situation where I have a URL like this

http://www.test.com/test.php?month=5&year=5&pn=4&ItemsPerPage=15&sort=5

Month and Year state the Date, pn = Page Number, ItemsPerPage = Items Per Page, and sort = ORDER BY.

My problem is that t开发者_如何学Gohere are multiple spots on the page to change different variables.

A user can change the month and date through the calendar...and this wipes the rest of the variables out so the user loses their place on the page and how many items were on the page and how the page was sorted.

How do I make it to where a user can change one of the variables...but the other variables stay the same?

Any help would be greatly appreciated. Thanks in advance.


you need to add hidden fields to your forms which account for all the other fields you want to preserve.

eg:

<input type=hidden name="sort" value=5 />

the other (better) way is to initialize a php_session


see Multiple Variables into 1 in a URL


While your question isnt clear the answer would seem to be, for each place they can change 1 item, either you need to do a "reload" where it updates multiple, or, for each place they can change it you need to send the missing variables with the new value.. eg put them as hidden values in your form, or encode them as part of the URL destination.


I guess you mean changing stuff in Javascript.

Make a script that changes it using window.location?


To remember your variables, you should use SESSION, see this link http://www.php.net/manual/en/book.session.php

In short, I suggest this function

function remember($var,$default=null) {
  if(!session_id()) session_start(); // needed if session.auto_start is off
  if(isset($_GET[$var])) $_SESSION["remember"][$var] = $_GET[$var];
  if(!isset($_SESSION["remember"][$var])) $_SESSION["remember"][$var] = $default;
  return $_SESSION["remember"][$var];
}

Then you can write in your code something like this

$sort = remember("sort");

and it will be remembered even if clicking the calendar will clear the variable from URL


I personally use a function that recreates the URL variables except chosen excluded ones, you might find it useful.

The function

<?php
function urlVarsRecreate ($exclude='') {
    $urlArray = array();
    foreach($_GET as $key => $val) {
        if(!is_array($exclude)) {
            if ($key != $exclude) {
                array_push($urlArray, $key.'='.urlencode($val));
            }
        }
        else {
            if (!in_array($key, $exclude)) {
                array_push($urlArray, $key.'='.urlencode($val));
            }
        }
    }
    return implode('&', $urlArray);
}
?>

Now when you call it you specify which variables you don't want, either in an array or just one in a string, as shown below.

Calling it

<?php
//For excluding a single variable
echo '<a href="test.php?'.urlVarsRecreate('sort').'&sort=newVal">sort by newVal</a>';

//For excluding multiple variables
echo '<a href="test.php?'.urlVarsRecreate(array('year','month','pn')).'&sort=newVal">Some link</a>';'
?>

I hope it helps, i find it quite useful and often use it when making search result pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号