jedney Posted March 6, 2008 Share Posted March 6, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/ Share on other sites More sharing options...
jedney Posted March 6, 2008 Author Share Posted March 6, 2008 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484530 Share on other sites More sharing options...
mainewoods Posted March 6, 2008 Share Posted March 6, 2008 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')) Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484561 Share on other sites More sharing options...
jedney Posted March 6, 2008 Author Share Posted March 6, 2008 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"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484584 Share on other sites More sharing options...
awpti Posted March 6, 2008 Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484592 Share on other sites More sharing options...
haku Posted March 6, 2008 Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484616 Share on other sites More sharing options...
jedney Posted March 6, 2008 Author Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484938 Share on other sites More sharing options...
revraz Posted March 6, 2008 Share Posted March 6, 2008 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') Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484945 Share on other sites More sharing options...
haku Posted March 6, 2008 Share Posted March 6, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484952 Share on other sites More sharing options...
therealwesfoster Posted March 6, 2008 Share Posted March 6, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484953 Share on other sites More sharing options...
craygo Posted March 6, 2008 Share Posted March 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484957 Share on other sites More sharing options...
Psycho Posted March 6, 2008 Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484963 Share on other sites More sharing options...
haku Posted March 6, 2008 Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484964 Share on other sites More sharing options...
jedney Posted March 6, 2008 Author Share Posted March 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484969 Share on other sites More sharing options...
haku Posted March 6, 2008 Share Posted March 6, 2008 You should look into switch statements like mjdamato showed as well. Once you get to a point where you have a lot of if statements, they are the way to go. Quote Link to comment https://forums.phpfreaks.com/topic/94624-multiple-if-statements-im-overwhelmed/#findComment-484975 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.