Jump to content

Undefined offset


jesushax

Recommended Posts

Hi all,

 

just wondering what this error means and how i can solve it

 

the full error is here

 

Notice: Undefined offset: 2 in E:\domains\c\domain.co.uk\user\htdocs\includes\titles.php on line 3

 

and heres the code from titles.php

<?php
$url = $_SERVER['REQUEST_URI'];
list($root, $lvl1, $lvl2) = split('[/]', $url);
//front pages
if ($lvl1 == "about.php")
{ $title = 'Domain | About Us';}
elseif ($lvl1 == "business_directory.php")
{ $title = 'Domain | Business Directory';}  
elseif ($lvl1 == "new_directory")
{ $title = 'Domain | Business Directory';}  
elseif ($lvl1 == "business_support.php")
{ $title = 'Domain | Business Support';}  
elseif ($lvl1 == "case_studies.php")
{ $title = 'Domain | Case Studies';}  
elseif ($lvl1 == "contact.php")
{ $title = 'Domain | Contact Us';}  
elseif ($lvl1 == "email_page.php")
{ $title = 'Domain | Email Page To A Friend';}  
elseif ($lvl1 == "meet_the_buyer.php")
{ $title = 'Domain | Meet The Buyer';}  
elseif ($lvl1 == "work_vine.php")
{ $title = 'Domain | Work Vine Newsletter';} 
elseif ($lvl1 == "working_with_developers.php")
{ $title = 'Domain | Working With Developers';}  
//sub pages
elseif ($lvl1 == "links.php")
{ $title = 'Domain | Useful Links';}
elseif ($lvl1 == "news")
{ $title = 'Domain | News';}
elseif ($lvl1 == "admin")
{ $title = 'Domain | Staff';}
//default
else 
{ $title = 'Domain | Business Directory | Business Support';}  
?>

 

Thanks for any help

Link to comment
Share on other sites

Undefined offset is (as it says) a Notice, which is a very low level error. Basically it means you are attempting to access an item in an array that doesn't exist. In your specific case I suspect that the call to split is finding less than two forward slashes. Therefore $lvl2 is attempting to assign itself to a value that doesn't exist.

Link to comment
Share on other sites

I'm pretty sure the [/] should actually just be / unless your URL contains [/] which I doubt it does

for the actual URL producing the error if you want to give the URI that would be useful. Use

echo $_SERVER['REQUEST_URI'];

if you want to see if it is the split thats wrong use

list($root, $lvl1, $lvl2) = split('/', $url);

Link to comment
Share on other sites

What is it exactly that you're trying to do.  Apparently you're not aware of the usage of split, but you do know the usage of list?

 

If you're splitting on /index.php with a slash (/), then you will only get two results..  The first will be blank and the second will be index.php

 

As for your list() I don't see how you're going to decipher a lvl1 and lvl2 out that... What does lvl1 and 2 stand for in your script?  Perhaps your trying to get the variables AFTER index.php?  Such as

 

index.php?root=blah&lvl1=blah&lvl2=blah

if so... for that you would use $_GET['root'] and etcetera instead of using the $_SERVER variable

Link to comment
Share on other sites

what im trying to do is get the current url and split the url into what i call lvl1 & 2

 

for example

 

domain.com/business/aboutus.php

would return

$lvl1 = business

$lvl2 = aboutus.php

 

then date level 1 and add the title to all the level 1 pages "domain.com | business"

so the page title indicates the users is in the business section

 

then on about us id have in the page title "domain.com | business | about us"

 

you see where im going with it?

 

so im stripping the url and creating a page title depending on which folder, subfolder or page the user is on

 

Link to comment
Share on other sites

Ok. I understand.  Well instead of using REQUEST_URI try SCRIPT_URI which will give you the entire address.. domain and all.

 

From there you'll need to strip out the protocol first.. such as http:// or https://

THEN you can split it by slash.  If it were me though, I wouldn't use list to create my variables as you could run into more problems with adding more subdirectories later on.

Link to comment
Share on other sites

What is the point of checking $lvl1, $lvl2 etc. It seems like something that could be sidestepped entirely using mod_rewrite. Before I knew much about mod_rewrite or regular expressions I used to forward every page that didn't exist to a single page, then attempt to parse the request url in a similar manner to you. Only instead of using list I simply used the array returned by explode. But since then I've realised it's alot easier to let mod_rewrite do the grunt work and pass the information back to a php through $_GET values. If that doesn't apply to you for whatever reason I would use something along the lines of.

 

$parts = explode('/', $_SERVER['REQUEST_URI']);

 

Then the index of $parts as being it's index in the heirachy.

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.