Jump to content

Multiple If-Statements (I'm overwhelmed)


jedney

Recommended Posts

Hello.

 

I have kinda overwhelmed myself with the latest scripting I have wanted to do.  I thought I had a good enough knowledge of if-statements to perform what I want to do, but I'm seeing that I do not.

 

What I am trying to do, is run an entire site off index.php.  The site will be divided into 2 sections, with multiple pages and details per section.  I'm calling these sections based on the URL...

eg.

index.php?game=dod&page=contact

index.php?game=css&page=matches

index.php?game=dod&page=roster&id=1

 

This is the first time, I have tried to run everything on one page, and I think i'm getting mixedup in my mess.  MY assumptions from what I already know, and what I've read was I could do something like...

 

<?php if (isset($_GET['game']))
{
// Insert information here for index.php?game=game; this will be the game's home page
} else {
if (isset($_GET['game']) & ($_GET['page'] = 'roster'))
{
// Insert information here for index.php?game=game&page=roster
} else {
if (isset($_GET['game']) & ($_GET['page'] = 'roster') & ($_GET['id']))
{
// Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member
} else {
// Insert main page information, which allows visitors to choose a game to view
}
}
}
?>

 

When I post code in this format, I get errors like

"Parse error: syntax error, unexpected T_ELSEIF" at my first } else { line.

 

Am I doing something wrong, or am I even going about what I want to do in a wrong way?

Link to comment
https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/
Share on other sites

UPDATE

 

<?php if (isset($_GET['game']))
{
// Insert information here for index.php?game=game; this will be the game's home page
echo("Insert information here for index.php?game=game; this will be the game's home page");
} else {
if (isset($_GET['game']) & ($_GET['page'] = 'roster'))
{
// Insert information here for index.php?game=game&page=roster
echo("Insert information here for index.php?game=game&page=roster");
} else {
if (isset($_GET['game']) & ($_GET['page'] = 'roster') & ($_GET['id']))
{
// Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member
echo("Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member");
} else {
// Insert main page information, which allows visitors to choose a game to view
echo("Insert main page information, which allows visitors to choose a game to view");
}
}
}
?>

 

"index.php" shows correct information.

"index.php?game=game" shows correct information

Anything after that, just shows the "index.php?game=game" information ?

here's some clues:

if (isset($_GET['game']) & ($_GET['page'] = 'roster'))

-- to express the 'and' condition in php use 2 &'s not 1

-- to express the test for equality in a php condition, use 2 ='s not 1

below is correct

if (isset($_GET['game']) && ($_GET['page'] == 'roster'))

 

Thank you for the post; the code still does not work.  Below is the code.  It will be linked to a DB and everything, but I shortened it up for testing purposes.

 

<?php if (isset($_GET['game']))
{
// Insert information here for index.php?game=game; this will be the game's home page
echo("Insert information here for index.php?game=game; this will be the game's home page");
} else {
if (isset($_GET['game']) && ($_GET['page'] == 'roster'))
{
// Insert information here for index.php?game=game&page=roster
echo("Insert information here for index.php?game=game&page=roster");
} else {
if (isset($_GET['game']) && ($_GET['page'] == 'roster') && ($_GET['id'] == '1'))
{
// Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member
echo("Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member");
} else {
// Insert main page information, which allows visitors to choose a game to view
echo("Insert main page information, which allows visitors to choose a game to view");
}
}
}
?>

You may want to choose a different way to format your if statements as well. I personally use:

 

if(something)
{
    if(somethingelse)
    {
         if(anotherthing)
         {
              do something;
         }
    }
    else
    {
           do something;
    }
}
else
{
      do something;
}

 

I find this is really easy to see how multiple if statements relate to each other.

I have attempted the structure you specified, haku. 

 

<?php
if(isset($_GET['game']))
{
    if(isset($_GET['game']) && ($_GET['page'] == 'roster'))
    {
         if(isset($_GET['game']) && ($_GET['page'] == 'roster') && ($_GET['id'] == '1'))
         {
              // Insert information here for index.php?game=game; this will be the game's home page
echo("Insert information here for index.php?game=game; this will be the game's home page");
         }
    }
    else
    {
           // Insert information here for index.php?game=game&page=roster
echo("Insert information here for index.php?game=game&page=roster");
    }
}
else
{
      // Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member
echo("Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member");
}

?>

 

My question is, I see how the brackets are lined up, but there is no way to display a default page, if there is no variables after the main URL.

You are being too redundant.

 

If it passed the first IF, why are you comparing the exact same thing a second time?

 

if(isset($_GET['game']))

{

    if(isset($_GET['game']) && ($_GET['page'] == 'roster'))

    {

        if(isset($_GET['game']) && ($_GET['page'] == 'roster') && ($_GET['id'] == '1'))

 

should be

 

if(isset($_GET['game']))

{

    if($_GET['page'] == 'roster')

    {

        if($_GET['id'] == '1')

 

<?php
if(isset($_GET['game']))
{
    if(isset($_GET['page']) && $_GET['page'] == 'roster')
    {
         if(isset($_GET['id']) && $_GET['id'] == 1)
         {
              // do whatever you need to do when $_GET['id'] == 1
              // this spot is entered when all three get variables are set
         }
         else
         {
              // do whatever you need to do when $_GET['page'] == 'roster'
              // this spot is entered when only the first two variables are set ($_GET['id'] has not been set here as it is the else statement for that)
         }
    }
    else
    {
           // do whatever you need to do when $_GET['game'] is set
           // this spot is entered when only the first variable has been set
    }
}
else
{
      // do what you need to do when nothing is set
}

?>

 

You had all the else statements set in the wrong spot, thats all. You had the right idea.

 

You were also doubling up on checking $GET variables in your first three nested if statements. You only need to check once for those.

<?php
if(isset($_GET['game']))
{
    if($_GET['page'] == 'roster')
    {
         if($_GET['id'] == '1')
         {
              // Insert information here for index.php?game=game; this will be the game's home page
              echo("Insert information here for index.php?game=game; this will be the game's home page");
         }
    }
    else
    {
           // Insert information here for index.php?game=game&page=roster
           echo("Insert information here for index.php?game=game&page=roster");
    }
}
else
{
      // Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member
      echo("Insert information here for index.php?game=game&page=roster&id=id; this will be detailed information of rostered member");
}

?>

 

I don't see why that wouldn't work

Glad to see we have another gamer. I run TheElders clan. check us out here http://theelders.net

 

Now on to your problem. When working with if/thens start from the one with the most checks and work back to the simple one

 

<?php
if(isset($_GET['game']) && ($_GET['page'] == 'roster') && ($_GET['id'] == '1')){
// Insert information here for index.php?game=game&page=roster&id=id;
} else {
  if(isset($_GET['game']) && ($_GET['page'] == 'roster')){
  // Insert information here for index.php?game=game&page=roster
  } else {
    if(isset($_GET['game'])){
    // Insert information here for index.php?game=game;
    } else {
    // Show just the index page

    }
  }
}
?>

 

Let me know

 

Ray

On your index page use a switch() to determine the 'game', then I would suggest using an external file (as appropriate) to determine the sub-page. And then add'l external files for each page (assuming they don't share a lot of common code). This keeps the different bits of logic separate and prevents a page from becomming so long that it is difficut to troubleshoot or modify. Of course you can keep that logic in-line on the main page as well.

 

<?php

switch ($_GET['game']) {

 case 'dod': //Include the dod index file which will determine the page to display
   include ('dod_index.php');
   break;

 case 'css': //Include the css index file which will determine the page to display
   include ('css_index.php');
   break;

 default: //Include the main page - i.e. no 'game' page selected
   include ('main.php');
   break;

}

?>

 

The 'dod_index.php' page could look very similar:

 

<?php

switch ($_GET['page']) {

 case 'contacts': //Include the contacts file to display the page content
   include ('dod_contacts.php');
   break;

 case 'matches': //Include the matches file to display the page content
   include ('css_matches.php');
   break;

 case 'roster': //Include the roster file to display the page content
   include ('css_roster.php');
   break;

 default: //No 'page' was selected - display the home page for css
   include ('css_main.php');
   break;

}

?>

 

However, if the 'contacts' page will have the same functionality for each game type, then I would create one file to display contacts and use the $game variable to make the page pull the ciorrect content.

I don't see why that wouldn't work

 

Because the else statements don't line up with the correct if statements.

 

---------------------------------------------

 

craygo's method will work, but its got redundancy built into it - its checking for things that have already been checked (in this case its checking for $_GET['game'] three times, which is two more than necessary). So its not the most efficient.

Awesome, if it's the same team as I think it is, I've gamed with you a lot back in TFC 1.6, years back.

 

The coding works nicely, I'm new to using multiple if statements, and I'm trying to make my team's site run off a DB.  Thanks!  We'll see how I do implementing it into the rest of the site.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.