Jump to content

[SOLVED] Using PHP to highlight the current page in a list


biwerw

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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-->

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.