jesushax Posted November 27, 2009 Share Posted November 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/ Share on other sites More sharing options...
cags Posted November 27, 2009 Share Posted November 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966380 Share on other sites More sharing options...
jesushax Posted November 27, 2009 Author Share Posted November 27, 2009 ah right i see thanks for taking the time to explain that to me how would i correct the error if the split is less than 2? an if statement? Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966385 Share on other sites More sharing options...
JAY6390 Posted November 27, 2009 Share Posted November 27, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966394 Share on other sites More sharing options...
jesushax Posted November 27, 2009 Author Share Posted November 27, 2009 ok i did both $url = $_SERVER['REQUEST_URI']; echo $url; list($root, $lvl1, $lvl2) = split('/', $url); the $url; gave me /index.php and the error is still there, what you think it is? i cant think of any other way to debug it :S Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966396 Share on other sites More sharing options...
cags Posted November 27, 2009 Share Posted November 27, 2009 Yes because you are still attempting to assign [2] from the array returned by split to $lvl2. Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966399 Share on other sites More sharing options...
Zane Posted November 27, 2009 Share Posted November 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966401 Share on other sites More sharing options...
jesushax Posted November 27, 2009 Author Share Posted November 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966404 Share on other sites More sharing options...
Zane Posted November 27, 2009 Share Posted November 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966410 Share on other sites More sharing options...
jesushax Posted November 27, 2009 Author Share Posted November 27, 2009 how would you do it? thanks Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966414 Share on other sites More sharing options...
cags Posted November 27, 2009 Share Posted November 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183109-undefined-offset/#findComment-966417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.