php switch case error

i having an error came out each time i echo this on my index.php

below is my switch code, as i’m using php dynamic site hence i use switch to change the data each time when different php is call.

[php]<?php
function title_switch()
{
$var = (isset($_GET[‘page’]) && !empty($_GET[‘page’])) ? $_GET[‘page’] : ‘home’;
switch ($var) {
case ‘home’:
$a[‘title’] = " » Home";
$a[‘meta_d’] = “This page is about my site!”;
$a[‘meta_k’] = “something, somethingelse”;
$a[‘slider1’] = “<script type=“text/javascript” src=”./js/jquery.easing.1.3.js">\n";
$a[‘slider2’] = “<script type=“text/javascript” src=”./js/tms-0.3.js">\n";
$a[‘slider3’] = “<script type=“text/javascript” src=”./js/tms_presets.js">\n";
$a[‘main’] = “<script type=“text/javascript” src=”./js/script.js">\n";
$a[‘pageid’] = “page1”;
$a[‘slider’] = “slide”;
break;
case ‘company’:
$a[‘title’] = " » Company";
$a[‘meta_d’] = “This page is about my site!”;
$a[‘meta_k’] = “something, somethingelse”;
$a[‘all’] = “<script type=“text/javascript” src=”./js/all.js">\n";
$a[‘pageid’] = “page2”;
$a[‘slider’] = “banner”;
break;
default:
$a[‘title’] = " » HTTP 404 - Page Not Found";
$a[‘meta_d’] = “”;
$a[‘meta_k’] = “”;
break;
}
return $a;
}
$details = title_switch();
?> [/php]

this is my index.php on line 21

[php]<?php echo $details['all']; ?>[/php]

if go to www.example.com it will show below error

[code] Notice: Undefined index: all in C:\wamp\www\site\index.php on line 21 Call Stack
Time Memory Function Location

1 0.0035 258968 {main}( ) ..\index.php:0[/code]

if i call www.example.com/company it will not show any error…

may I know where did i went wrong? kinda confuse here…

example: if i call www.example.com/company it didnt show error, but if i call www.example.com it show error.

i wanted this code

[php]<?php echo $details['all']; ?>[/php]

to show only if www.example.com/company and it will not show if www.example.com is call.

the code inside:
[php]<?php echo $details['all']; ?>[/php]

is

[php]case ‘company’:
$a[‘title’] = " » Company";
$a[‘all’] = “<script type=“text/javascript” src=”./js/all.js">\n";[/php]

hope this can give a

You should define $a as an array and include a default value for every key expected to be in $details

For example

[php]
function title_switch() {
// define default/global values
$a = array(
‘title’ => ‘’,
‘all’ => ‘’,
);

$var = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 'home';
switch ($var) {

[/php]

This way, if your switch fails, title_switch() will always return an array of default values.

I would agree with M@tt.

You are trying to access the $a[‘all’] variable - Key ‘all’ of the $a array.
This has been set if the Page you are viewing is Company, but not on any other - so you are getting the error - Undefined index ‘all’ as it hasnt’ been set.

Initiallising $a as an array and giving it default values for each key you intend to populate should help with this.

Thanks,

Sponsor our Newsletter | Privacy Policy | Terms of Service