biwerw Posted January 20, 2009 Share Posted January 20, 2009 I am using an ID array to load certain .php files when their link is clicked. I would like to have this link highlighted after the .php file is loaded. However the includes are not full pages just a DIVs containing image galleries. I applied the following links (http://alistapart.com/articles/keepingcurrent/) code but am looking for an alternative to work with my above situation. What I am thinking, but have no idea how to execute it is: 1. When the include is loaded use php to generate an ID. 2. Use that ID to control whether or not the class is applied to the link. The existing requires <?php $thisPage="page1"; ?> to be placed at the top of each page. Then <li><a href="index.php?id=page1" title="Page 1"<?php if ($thisPage=="page1") echo " id=\"current\""; ?>>Page 1</a></li> to figure out what page its on. My idea is similar but instead of creating the $thisPage ID when the page loads create one when the include php file loads and use that to style the link. Is this possible? Does my question make sense? It's late. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/ Share on other sites More sharing options...
thepip3r Posted January 20, 2009 Share Posted January 20, 2009 I'm not entirely positive about what your'e asking in your "Then" area of your post but it sounds like you're trying to use include(); to include different pages based off of a user's click. If I'm understanding your problem correctly and all you want to do is to know what was the last included page, that's easy. As you posted: <li><a href="index.php?id=page1" title="Page 1" On your index.php page, just echo the $_GET['id'] element of the GET superglobal and you'll see exactly what variable was passed based off of the hyperlink clicked. -- HTH Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-740964 Share on other sites More sharing options...
biwerw Posted January 20, 2009 Author Share Posted January 20, 2009 It's not the loading of the includes I'm troubled with its styling the links according to what php file has been loaded. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-741169 Share on other sites More sharing options...
cytech Posted January 20, 2009 Share Posted January 20, 2009 Hello, Please post the code you are using to "include" file files so we can see how its all working together. Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-741201 Share on other sites More sharing options...
biwerw Posted January 21, 2009 Author Share Posted January 21, 2009 Here is the array for determining which .php file to load. (this works perfectly) <?php // Define our array of allowed $_GET values $pass = array('page1','page2','page3','page4'); // If the page is allowed, include it: if (in_array($_GET['id'], $pass)) { include ('work/' . $_GET['id'] . '.php'); } // If there is no $_GET['id'] defined, then serve the homepage: elseif (!isset($_GET['id'])) { include ('home.php'); } // If the page is not allowed, send them to an error page: else { // This send the 404 header header("HTTP/1.0 404 Not Found"); // This includes the error page include ('error.php'); } ?> and the links: <ul class="nav"> <li><a href="index.php?id=page1" title="Page 1" <?php if ($thisPage=="page1") echo " id=\"current\""; ?>>Page 1</a></li> <li><a href="index.php?id=page2" title="Page 2" <?php if ($thisPage=="page2") echo " id=\"current\""; ?>>Page 2</a></li> <li><a href="index.php?id=page3" title="Page 3" <?php if ($thisPage=="page3") echo " id=\"current\""; ?>>Page 3</a></li> <li><a href="index.php?id=page4" title="Page 4" <?php if ($thisPage=="page4") echo " id=\"current\""; ?>>Page 4</a></li> </ul> And I have this at the top of the index.php page: <?php $thisPage="page1"; ?> Currently the code is styling the links when the index.php file is loaded due to the $thisPage=="page1". I need it to style the links when the includes are loaded into the page. is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-741787 Share on other sites More sharing options...
Philip Posted January 21, 2009 Share Posted January 21, 2009 If I understand the question correctly, this would be one approach, I have simplified the navigation part. <?php // Get our GET $pageID = $_GET['id']; // Define our array of allowed $_GET values, and their page titles $pass = array( 'page1' => 'Page 1', 'page2' => 'Page 2', 'page3' => 'Page 3', 'page4' => 'Page 4', ); // If the page is allowed, include it: if(array_key_exists($_GET['id'], $pass)) { include_once('work/' . $pageID . '.php'); } // If there is no $_GET['id'] defined, then serve the homepage: elseif (!isset($_GET['id'])) { include('home.php'); } // If the page is not allowed, send them to an error page: else { // This send the 404 header header("HTTP/1.0 404 Not Found"); // This includes the error page include ('error.php'); } ?> <ul class="nav"> <?php // For all of the array items, foreach($pass as $key => $value) { // Show the basic structure echo '<li><a href="index.php?id=',$key,'" title="',$value,'"'; // And if it is the current page (if the GET var is the same as the one in the loop) // We should show id=current. if($pageID == $key) echo ' id="current"'; // Finish showing basic structure echo '>',$value,'</a></li>'; } ?> </ul> Currently the code is styling the links when the index.php file is loaded due to the $thisPage=="page1". I need it to style the links when the includes are loaded into the page.If you're including the page that they are trying to load, (i.e. page1, page2, etc), you can call $pageID. Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-741797 Share on other sites More sharing options...
biwerw Posted January 21, 2009 Author Share Posted January 21, 2009 @KingPhilip Just glancing at it, I think it should work for what I am trying to accomplish. Will implement it soon and let you know. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-741933 Share on other sites More sharing options...
biwerw Posted January 22, 2009 Author Share Posted January 22, 2009 I applied the code code and i am receiving this error: PHP Error Message Warning: Invalid argument supplied for foreach() in /home/a2226966/public_html/beta/navigation.php on line 36 Line 36 is: foreach($pass as $key => $value) { I tried a few things but no luck, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742839 Share on other sites More sharing options...
Philip Posted January 22, 2009 Share Posted January 22, 2009 <ul class="nav"> <?php print_r($pass); // For all of the array items, /*foreach($pass as $key => $value) { // Show the basic structure echo '<li><a href="index.php?id=',$key,'" title="',$value,'"'; // And if it is the current page (if the GET var is the same as the one in the loop) // We should show id=current. if($pageID == $key) echo ' id="current"'; // Finish showing basic structure echo '>',$value,'</a></li>'; }*/ ?> </ul> What does that output? Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742840 Share on other sites More sharing options...
biwerw Posted January 22, 2009 Author Share Posted January 22, 2009 Nothing, just a space in between <ul class="nav"> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742847 Share on other sites More sharing options...
Philip Posted January 22, 2009 Share Posted January 22, 2009 Okay, that's odd. Maybe I'm misunderstanding the question. <ul class="nav"> <?php // For all of the array items, foreach($pass as $key => $value) { // Show the basic structure echo '<li><a href="index.php?id=',$key,'" title="',$value,'"'; // And if it is the current page (if the GET var is the same as the one in the loop) // We should show id=current. if($pageID == $key) echo ' id="current"'; // Finish showing basic structure echo '>',$value,'</a></li>'; } ?> </ul> That code is on the included page (page1, page2, etc), correct? Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742852 Share on other sites More sharing options...
Philip Posted January 22, 2009 Share Posted January 22, 2009 I just tested it on my server, and the code works. Mind posting more code? Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742857 Share on other sites More sharing options...
biwerw Posted January 22, 2009 Author Share Posted January 22, 2009 i have a main index.php. This loads home.php into my content div right away because of // If there is no $_GET['id'] defined, then serve the homepage: elseif (!isset($_GET['id'])) { include ('home.php'); } I also have a navigation.php file which is included into the index.php file and contains the list of links which are trying to load page1.php, page2.php etc. when they are clicked. Thats where I currently have <ul class="nav"> <?php // For all of the array items, foreach($pass as $key => $value) { // Show the basic structure echo '<li><a href="index.php?id=',$key,'" title="',$value,'"'; // And if it is the current page (if the GET var is the same as the one in the loop) // We should show id=current. if($pageID == $key) echo ' id="current"'; // Finish showing basic structure echo '>',$value,'</a></li>'; } ?> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742861 Share on other sites More sharing options...
Philip Posted January 22, 2009 Share Posted January 22, 2009 Mind posting code from index.php? I'm thinking that you're defining $pass after you've included the navigation file. Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742864 Share on other sites More sharing options...
biwerw Posted January 22, 2009 Author Share Posted January 22, 2009 You are right, my navigation.php include is above the array. <!--BODY--> <div id="layout-body"> <!--NAVIGATION--> <?php include ("navigation.php"); ?> <!--CONTENT--> <div id="content"> <?php // Define our array of allowed $_GET values $pass = array( 'page1' => 'Page 1', 'page2' => 'Page 2', 'page3' => 'Page 3', ); // If the page is allowed, include it: if(array_key_exists($_GET['id'], $pass)) { include_once('work/' . $pageID . '.php'); } // If there is no $_GET['id'] defined, then serve the homepage: elseif (!isset($_GET['id'])) { include ('home.php'); } // If the page is not allowed, send them to an error page: else { // This send the 404 header header("HTTP/1.0 404 Not Found"); // This includes the error page include ('error.php'); } ?> </div><!--content end--> </div><!--layout-body end--> Is there a way to control which loads first? Or a better way to accomplish this? I've kinda been learning while going and chopping different codes up to get this far. Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742870 Share on other sites More sharing options...
Philip Posted January 22, 2009 Share Posted January 22, 2009 Change it to this: <!--BODY--> <div id="layout-body"> <!--NAVIGATION--> <?php // Simplify the variable name $pageID = $_GET['id']; // Define our array of allowed $_GET values $pass = array( 'page1' => 'Page 1', 'page2' => 'Page 2', 'page3' => 'Page 3', ); // Include the navigation file include ("navigation.php"); ?> <!--CONTENT--> <div id="content"> <?php // If the page is allowed, include it: if(array_key_exists($pageID, $pass)) { include_once('work/' . $pageID . '.php'); } // If there is no $_GET['id'] defined, then serve the homepage: elseif (!isset($pageID)) { include ('home.php'); } // If the page is not allowed, send them to an error page: else { // This send the 404 header header("HTTP/1.0 404 Not Found"); // This includes the error page include ('error.php'); } ?> </div><!--content end--> </div><!--layout-body end--> Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742876 Share on other sites More sharing options...
biwerw Posted January 22, 2009 Author Share Posted January 22, 2009 Awesome, works perfect. Thanks for the help. I owe you a beer or two if your ever in the St. Paul Minnesota area. Quote Link to comment https://forums.phpfreaks.com/topic/141559-solved-using-php-to-highlight-the-current-page-in-a-list/#findComment-742894 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.