Jump to content

[SOLVED] switch doesnt use its default


dazzclub

Recommended Posts

Hi guys and girls,

 

Im using a switch statment to display certain text in the title tags. currently all my links, have an action variable to select which title to use on certain pages.

 

contact_us.php?action=contact

 

however i visited the home page to see if it uses the default statement and it doesnt

 

here is the switch statement

switch($_GET['action'])
{
case 'about';
echo ' - about us';
break;
case 'news';
echo ' - read all the latest news and our press release';
break;
case 'contact';
echo ' - make an enquiry';	
break;
case 'quality';
echo ' - quality';
break;
case 'home';
echo ' - more';
break;
case 'news headlines';
echo 'news bulletins';
break;	
default;
echo 'nothing';
}

 

Reading the error php displays it says i have an undefined index, the line it shows is;

switch($_GET['action'])

 

here is the whole function if it helps

function pageTitle()
{
if ((isset($_GET['medical_product'])) && (isset($_GET['subcat_id'])) && (is_numeric($_GET['medical_product'])) && (is_numeric($_GET['subcat_id']))) { //correctly accessed 
$medical_product=$_GET['medical_product'];
$subcat_id=$_GET['subcat_id']; 
global $dbc;
$query="SELECT * FROM SubCat1 WHERE CategoryID = $medical_product AND SubCatID = $subcat_id";
$result = mysqli_query ($dbc, $query)or die(mysqli_error() . "<p>With query:<br>$query");
while ($row = mysqli_fetch_array($result))
{
echo "$row[subCatName]";
}
} else {
switch($_GET['action'])
{
case 'about';
echo 'LCR/Hallcrest - about us';
break;
case 'news';
echo 'LCR/Hallcrest - read all the latest news and our press release';
break;
case 'contact';
echo 'LCR/Hallcrest - make an enquiry';	
break;
case 'quality';
echo 'LCR/Hallcrest - quality';
break;
case 'home';
echo 'LCR/Hallcrest - Temperature Triggered Color Changing Technology and Graphics';
break;
case 'news headlines';
echo 'news bulletins';
break;	
default;
echo 'nothing';
}	
}
}

 

 

Thanks,

Darren

Link to comment
Share on other sites

There should be a ":" after each case statement, not ";"

 

You should only do the switch if $_GET['action'] is set:

<?php
if (isset($_GET['action'])) {
    switch($_GET['action']) {
//
//  etc
//
    }
else 
   echo 'Nothing';
?>

Or you could use temp variable that is set before the switch statement:

<?php
$tmp = (isset($_GET['action']))?$_GET['action']:'';
switch($tmp) {
?>

 

Ken

Link to comment
Share on other sites

Hi Ken,

 

I followed your advice and replaced : after each ;

 

so now my code looks like this


}else{
if(isset($_GET['action'])) {
    switch($_GET['action']) {
case 'about':
echo ' - about us';
break;
case 'news':
echo ' - read all the latest news and our press release';
break;
case 'contact':
echo ' - make an enquiry';	
break;
case 'quality':
echo ' - quality';
break;
case 'home':
echo ' - Temperature Triggered Color Changing Technology and Graphics';
break;
case 'news headlines':
echo 'news bulletins';
break;	
default:
echo 'home page';
}	
}
}

 

Thanks

Darren

 

so when to test it out, when to the homepage and it still gave me no title and an error message, undefined index action...

 

I will go back to the books on this one

 

Again thanks for everyones help on this

Link to comment
Share on other sites

Was it a NOTICE or an ERROR?

 

If it was a NOTICE then it just means you've got strict error reporting on.

 

You can fix this using 1 of 3 ways.

 

1. Add this at the start:

error_reporting(E_ALL ^ E_NOTICE);

 

2. Add an @ in the switch() to suppress the NOTICE

switch (@$_GET['action']) {

 

3. Edit the php.ini file to do the same as (1)

Link to comment
Share on other sites

Hi Ken,

 

Sorted it out now, i placed the else statement within the switch...so it wasnt reading it write (if that makes any sense to you :S)

 

This is the finished working code

else{
if(isset($_GET['action'])) {
    switch($_GET['action']) {
case 'about':
echo ' - about us';
break;
case 'news':
echo ' - read all the latest news and our press release';
break;
case 'contact':
echo ' - make an enquiry';	
break;
case 'quality':
echo ' - quality';
break;
case 'home':
echo ' - Temperature Triggered Color Changing Technology and Graphics';
break;
case 'news headlines':
echo 'news bulletins';
break;
}		
}
else{
echo 'nothing';
}
}

 

Again thanks for all your help :)

Link to comment
Share on other sites

Hi Yesideez,

 

I had all reporting errors on so it would display everything stupid thing that i might do..

 

I didnt want to surpress the notice, because if the function didnt work it wont harm how the site works but it would show a page without a title and i ideally want a title for each page.

 

Thanks for your help

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.