Unspoiled Posted September 22, 2015 Share Posted September 22, 2015 Hi guys, i'm making a system with linking like this: <?php $link=$_GET['page']; if ($link == '1'){ include ('page.php'); } if ($link == '1-1'){ include ('page.php'); } if ($link == '1-2'){ include ('page.php'); } if ($link == '1-3'){ include ('page.php'); } if ($link == '3'){ include ('page.php'); } if ($link == '4'){ include ('page.php'); } if ($link == '5'){ include ('page.php'); } if ($link == '6'){ include ('page.php'); } else{ include_once ('pages/home.php'); } ?> And at the end I have the else{ include_once ('pages/home.php'); } But I want to have the else statement for all the if statements, someone has a solution? Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 22, 2015 Share Posted September 22, 2015 What are you trying to accomplish here? You're linking the same file in all cases. What is the point of all of those if statements? Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted September 22, 2015 Solution Share Posted September 22, 2015 if ($link == '1'){ include ('page.php'); } elseif ($link == '1-1'){ include ('page.php'); } elseif ($link == '1-2'){ include ('page.php'); } else { include ('home.php'); } // alternatively switch ($link) { case '1': include ('page.php'); break; case '1-1': include ('page.php'); break; case '1-2': include ('page.php'); break; default: include ('home.php'); break; } Quote Link to comment Share on other sites More sharing options...
Unspoiled Posted September 22, 2015 Author Share Posted September 22, 2015 What are you trying to accomplish here? You're linking the same file in all cases. What is the point of all of those if statements? I just changed the names to page.php, in my real code it's different if ($link == '1'){ include ('page.php'); } elseif ($link == '1-1'){ include ('page.php'); } elseif ($link == '1-2'){ include ('page.php'); } else { include ('home.php'); } // alternatively switch ($link) { case '1': include ('page.php'); break; case '1-1': include ('page.php'); break; case '1-2': include ('page.php'); break; default: include ('home.php'); break; } This solved it, used the elseif Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 22, 2015 Share Posted September 22, 2015 For what it's worth, you could avoid all the elseifs and cases by creating an array of possible values...and then testing for the existence of a value. For example: <?php $pageReferences = array( '1' => 'page.php', '1-1' => 'page.php', '1-2' => 'page.php' ); $link = $_GET['page']; if(isset($pageReferences[$link])){ include $pageReferences[$link]; } else { include 'home.php'; } ?> Quote Link to comment 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.