Forum Moderators: coopster

Message Too Old, No Replies

using if and elseif statements.

how to layout an elseif script

         

mack

8:56 am on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi all.

Being fairly new to php this will probably be a very simple question.

What I am trying to do is create a menu based on different variables. for example if I want to show link1 I would have $link1="y";

the variables woudl be placed in a seperate file and by editing the included file you can change what links are displayed. I would then call this file via an include from the main php script.

this is the file that holds the values.


<?php
$iten1='y';
$item2='y';
$item3='y';
$item4='y';
$item5='y';
$item6='y';
?>

lets call the above file include.php

then in the main script file


<?php
include ("include.php");
print"Menu";

if ($item1=='y') // this is the line that causes a parse error
{
echo "the link to item1 goes here";
}

elseif ($item2=="y")
{
echo "the link to item2 goes here";
}

elseif ($item3=="y")
{
echo "the link to item3 goes here";
}

elseif ($item4=="y")
{
echo "the link to item4 goes here";
}

elseif ($item5=="y")
{
echo "the link to item5 goes here";
}

elseif ($item6=="y")
{
echo "the link to item6 goes here";
}
exit;
?>

I think I must be using the wrong syntax for my if statements. Can anyone make a sugestion on how to correct the faulty code.

My though is that elseif may not be the correct approach. In the example I have used

if ($item1=="y")
{what to do}

then it goes onto a different variable for the elseif?

Thanks in advance.

Mack.

dreamcatcher

9:05 am on Jan 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Mack,

Firstly, may I suggest you use a switch statement, rather than multiple if else statements. Your code should be ok, so long as no two variables have the value of 'y'. I would assign the variable as just $item, then have your values as y1, y2, y3 etc.

Then you could use a switch statement like this:


switch ($item)
{
case "y1":
//do something
break;

case "y2":
//do something
break;

case "y3":
//do something
break;
}

etc etc

Hope that helps

dc

mack

9:16 am on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Thank you very much for the quick response, I appreciate it.

Im just really confused why it errors on the very first "if"
if ($item1=='y')

It's as if I have messed up on that one line of coe. I just cant seam to understand why that would cause a parse error. I have had situations in the past where I have left a bracket open or something and it causes an error further down the page but there really is nothing above it.

Mack.

RobOgden

10:21 am on Jan 8, 2005 (gmt 0)

10+ Year Member



Dear Mack,
I may be completely wrong, but it could be because there is a spelling error in the file that holds the values, i.e. $iten1

Therefore the variable $item1 may not be recognisable.

Yours,

Rob

mack

10:25 am on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi rob,

Very well spotted, although I examplified the real link names and variables before I posted.

Un fortunatly no typos on the real file.

You must have an eagle eye to spot that :)

Mack.

RobOgden

10:32 am on Jan 8, 2005 (gmt 0)

10+ Year Member



Dear Mack,

Sorry it wasn't the misspelling. It could be that you end the clause with an 'elseif'. Shouldn't there be an 'else' at the end? i.e.

elseif ($item6=="y")
{
echo "the link to item6 goes here";
}
else {
**do something else**;
exit;
}

Yours,

Rob

mack

10:37 am on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi again rob.

Sorry about that :)

I think youre right because the if is all about one variable. In my code the elseif is about another variable.

I think perhaps I should have the if then an else for the same variable.. then a second if for the next variable etc.

Can I have the if statement then the else go to another if? or is that more or less what I have by using the else if. lol im conusing myself now :)

I see what you mean though about ending it in an elseif. I have changed that if else then used
{
exit;
}

Mack.

RobOgden

10:48 am on Jan 8, 2005 (gmt 0)

10+ Year Member



Dear Mack,

Do you want each to be a separate clause?

if ($item1=='y') {
echo "the link to item1 goes here";
}
if ($item2=="y") {
echo "the link to item2 goes here";
}

Or do they need to be dependent on each other? i.e.

if ($item1=='y') {
echo "the link to item1 goes here";
} else {
if ($item2=="y") {
echo "the link to item2 goes here";
}

One of these two will probably suit you!

Rob

mack

11:31 am on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Thanks again rob.

I did as you suggested and it worked perfectly. I simply places each "if" as a seperate clause with no elseif.

Basicaly What I am doing is creating a menue that can be edited through a browser by using fopen to create a config txt file. that way it is possible to check boxes to enable or disable certain links from the menu.

I tried it with all set as "y" and it worked.. I removed a few y's and those items disapeared from the menu, so it seams to work :)

Thanks again

Mack.

jshpro2

5:30 pm on Jan 8, 2005 (gmt 0)

10+ Year Member



I would like to suggest using an array, this way you code can be expanded and more options can be added in the future.