Else if statements help -total newbie with a simple question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tonysense
Forum Newbie
Posts: 3
Joined: Thu Feb 12, 2009 5:23 am

Else if statements help -total newbie with a simple question

Post by Tonysense »

Hi i need some help with an Else If statement. Basically I want to display some include text depending on where the user is i.e.

I want to pull in seo text for my homepages, using simple php includes, so index.php would pull in homepage_seo.php, aboutus.php would pull in aboutus_seo.php etc.

No i know that the statement has to be something like below, but I'm new to php, so any help would be great.

From W3 schools:

if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>

Thanks in advance,
Tony
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Else if statements help -total newbie with a simple question

Post by mattpointblank »

(Try using the code tags to display your code like I'm doing here)

You're almost there, just missed the curly braces which indicate the start and end of the if statement:

Code: Select all

 
<?php
$d=date("D");
 
if ($d=="Fri") {
    echo "Have a nice weekend!";
} elseif ($d=="Sun") {
    echo "Have a nice Sunday!";
} else {
    echo "Have a nice day!";
}
?>
 
Tonysense
Forum Newbie
Posts: 3
Joined: Thu Feb 12, 2009 5:23 am

Re: Else if statements help -total newbie with a simple question

Post by Tonysense »

Hi,

that statement works fine, but how do i modify it to pull in my includes?

Tony
Tonysense
Forum Newbie
Posts: 3
Joined: Thu Feb 12, 2009 5:23 am

Re: Else if statements help -total newbie with a simple question

Post by Tonysense »

UPDATE:
I have managed to locate the following code, which is doing the job, but the problem is that my default seo include is defaulting no matter what page i am on. The code i am using is below.

<?php
switch ($_SERVER['PHP_SELF']) {
case 'about-mb-centre-nottingham.php':
include 'seoaboutus.php';
break;
case 'mb-centre-contact-us.php':
include 'seocontact.php';
break;
case 'mb-centre-diagnostics.php':
include 'seodiagnostic.php';
break;
case 'mb-centre-faqs.php':
include 'seofaq.php';
break;
case 'mb-centre-mot.php':
include 'seomot.php';
break;
case 'mb-centre-ordering-parts.php':
include 'seoorderingparts.php';
break;
case 'mb-centre-repairs.php':
include 'seorepairs.php';
break;
case 'mb-centre-service.php':
include 'seoservices.php';
break;
case 'mb-centre-services.php':
include 'seombcservices.php';
break;
case 'mb-centre-used-cars.php':
include 'seousedcars.php';
break;

default:
include 'seohome.php';
break;
}
?>

I do have an alternative option:

if ($_SERVER['PHP_SELF'] == 'aboutus.php' {
include 'seoaboutus.php';
} else {
include 'seohome.php';
}

But the other one is better due to the multiple seo files.

Regards,
Tony
Post Reply