Forum Moderators: coopster
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.
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
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.
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.
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
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.