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
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 ?

Link to comment
Share on other sites

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'))

 

Link to comment
Share on other sites

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");
}
}
}
?>

Link to comment
Share on other sites

your problem is here:

 

<?php if (isset($_GET['game']))

 

That will always evaluate to true if "game" has any value. 1, 10, 100, a, b, c.. whatever

 

Consider looking into switch statements or re-evaluating your logic.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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')

 

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.