blatant3 Posted July 21, 2008 Share Posted July 21, 2008 Hello everyone. This is my first post to the forums so first I would like to say hello to everyone. The question I have is this: I am designing a website and I want to incorporate a system that will look at the URL and decide which include to use. I have everything working using some code like this: <?php $path = $_SERVER['PHP_SELF']; $page = basename($path); $page = basename($path, '.php'); if($page == "index") { include('includes/index.inc'); } elseif($page == "someOtherPage") { include('includes/someOtherPage.inc'); } else { echo 'blah blah blah'; ?> As you can see this can get redundant really fast. What I want to do is store all of the page names in an array and loop through them, including the page that matches the conditional, But I cant figure out how to get it to work. Any suggestions? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/115859-solved-question-about-looping/ Share on other sites More sharing options...
The Little Guy Posted July 21, 2008 Share Posted July 21, 2008 Try this (some editing may need to be done still, not tested): $path = $_SERVER['PHP_SELF']; $page = basename($path); $page = basename($path, '.php'); $myArray = ('index','someOtherPage','anotherPage'); foreach($myArray as $pa){ if($page == $pa) { include('includes/'.$pa.'.inc'); break; }else{ echo 'Page Not Found'; } } Quote Link to comment https://forums.phpfreaks.com/topic/115859-solved-question-about-looping/#findComment-595679 Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 I wouldn't do that because you'd get "Page not found" every time you iterated before the page was found. I'd personally do: $pages = array('home', 'contact', 'aboutus', 'something); if (in_array(strtolower($_GET['page']), $pages)) { include ("{$_GET['page']}.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/115859-solved-question-about-looping/#findComment-595682 Share on other sites More sharing options...
wildteen88 Posted July 21, 2008 Share Posted July 21, 2008 better to use in_array // page name page path $pages = array( 'index' => 'includes/index.inc', 'someotherpage' => 'includes/someotherpage.inc' ); if(in_array($requested_page_var, $pages)) { include $pages[$requested_page_var]; } Quote Link to comment https://forums.phpfreaks.com/topic/115859-solved-question-about-looping/#findComment-595683 Share on other sites More sharing options...
blatant3 Posted July 21, 2008 Author Share Posted July 21, 2008 thanks it worked i was able to get it working using in_array Quote Link to comment https://forums.phpfreaks.com/topic/115859-solved-question-about-looping/#findComment-595691 Share on other sites More sharing options...
The Little Guy Posted July 21, 2008 Share Posted July 21, 2008 I never thought about using that function. Good Idea! Quote Link to comment https://forums.phpfreaks.com/topic/115859-solved-question-about-looping/#findComment-595693 Share on other sites More sharing options...
The Little Guy Posted July 21, 2008 Share Posted July 21, 2008 this could be a way so you don't have to create an array: $path = 'includes/'; $page = $_SERVER['PHP_SELF']; $page = basename($page); $page = basename($page, '.php'); if(is_file($path . $page)){ include $path . $page; }else{ echo 'Invalid File'; } Quote Link to comment https://forums.phpfreaks.com/topic/115859-solved-question-about-looping/#findComment-595703 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.