1

im trying to sort categories and subcategories with list explode, but getting

Undefined offset: 1

this is a part of code

$squery = $_SERVER['QUERY_STRING'];
$smarty->assign('query', $squery);
list($str, $trash) = explode('&', $squery, 2);
list($category, $subcategory) = explode('=', $str, 2);
switch($category)
    {
        case 'locale':
            $_SESSION['locale'] = $subcategory;
            checklocale();
            header('Location:'.($_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : '.'));
            break;
        default:
            include 'main.php';
            break;
    }

Can you please tell me what i'm doing wrong

1
  • error is on which line? Commented Feb 11, 2013 at 18:17

3 Answers 3

3

It seems as if your query string does not have an & in it, thus your explode return an array with a single item, causing list() to throw the notice.

You probably want to:

  • Verify that there is actually a value in $_SERVER['QUERY_STRING']
  • Consider using something like parse_str() to parse the variables out of the string
  • Verify the existence of the values within the query string individually

You also might want to consider the alternative of using $_GET instead of $_SERVER['QUERY_STRING'] to access the parameters more directly. Your current approach is very prone to break if parameters are passed in wrong order.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem is i want to use this list function if it's provided by user (for example site/news?year=2013&month=2) but by default script should load main.php, but it doesnt. I've no idea what is wrong. By accessing website with ? and & arguments there is no errors
The problem is that explode() returns an array, not string. You are then trying , so this array is set to $str. You are then trying to further explode $str, which again is an array and not a string so the second list() line you have is not working at all. Again, You should probably just use $_GET to look for the specific values you want, or at a minimum use parse_str().
Thx guys, the best thing to "solve" this problem is to set error_reporting(0);
0

Can you please tell me what i'm doing wrong

Well, most of the time I know, Undefined offset: errors appear, when you are trying to define an un-indexed character within an array. For example, $var = "foo"; Now, if you were to echo $var[3]; you would get the same error, because, when 3 is an offset of "foo"; which only has three characters and i.e. from 0 to 2;

Do to the fact, you have given a limited code, I would suspect the problem to be somewhere explode('&', $squery, 2);

Comments

0

you can Do this By only check locale is set on QUERY_STRING or not

like

if(isset($_GET['locale'])){
         $_SESSION['locale'] = $subcategory;
        checklocale();
        header('Location:'.($_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : '.'));

}else{

     include 'main.php';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.