Rineru Posted October 22, 2007 Share Posted October 22, 2007 I have a code similar to this: (Cut down and renamed) <?php ($_GET['id']) if ($id == 'news') { include 'blahblah.php'; } //info elseif ($id == 'rock') { include 'rockblahblah.php'; } <-- other Elseifs --> else { require_once 'blahblah.php'; } ?> Included on my main page. What I want to do, I had working with this When they go to main.php I want the news to be the shown object, and if they navigate to something else. main.php?id=rock (that should be the link to it) I want rock's to be included. But for some reason news in the only thing included always. I think there is a small piece of code missing from the main.php. Came someone help me or direct me to a place that can? Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/ Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 you need to assign $_GET['id'] to $id $id = $_GET['id']; Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375158 Share on other sites More sharing options...
derwert Posted October 22, 2007 Share Posted October 22, 2007 Aside from not assigning $_GET['id'] to $id I didn't see any problems, though it would be more efficient to use a switch. <?php $id = (isset($_GET['id']) ? $_GET['id'] : ''); switch ($id) { case 'news': include 'blahblah.php'; break; case 'rock': include 'rockblahblah.php'; break; default: require_once 'blahblah.php'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375160 Share on other sites More sharing options...
cooldude832 Posted October 22, 2007 Share Posted October 22, 2007 Hey braand I've seen a lot of people lately posting about not assign globals. Can you get a sticky up about this possibly?? Saying how registered globals works and it should be off and that you need to assign them properly? Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375165 Share on other sites More sharing options...
trq Posted October 22, 2007 Share Posted October 22, 2007 And even easier to create an array of valid files and simply check that and dynamically include. <?php $valid = array('news','rock'); if (in_array($_GET['id'])) { include $_GET['id'] . '.php'; } else { include 'default.php'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375166 Share on other sites More sharing options...
Rineru Posted October 22, 2007 Author Share Posted October 22, 2007 Thanks everyone, I'm going to have hundreds of values so creating an array would be to tedious... and hard to view for me. Plus I'm comfortable with what I'm using. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375177 Share on other sites More sharing options...
Rineru Posted October 22, 2007 Author Share Posted October 22, 2007 What's wrong with this? <?php $id = $_GET['id']; include("include/session.php"); if ($id == 'news') { include 'news/news.php'; } //Start of Rock elseif (($id == 'rock')&&($session->logged_in)) { include 'rock/index.php'; <-- more elseif's and else--> Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375182 Share on other sites More sharing options...
derwert Posted October 22, 2007 Share Posted October 22, 2007 Missing curly bracket at the end and the lack of the error message you received... Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375183 Share on other sites More sharing options...
trq Posted October 22, 2007 Share Posted October 22, 2007 quote]I'm going to have hundreds of values so creating an array would be to tedious... You are kidding right? So instead of simply adding a valid file to an array your going to add an entire case to a switch statment? Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375197 Share on other sites More sharing options...
Rineru Posted October 22, 2007 Author Share Posted October 22, 2007 I figured out the problem, I was including session twice. Once on that include and then again in the log in. Quote Link to comment https://forums.phpfreaks.com/topic/74256-solved-switch-help/#findComment-375560 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.