Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone
I have created a simple website in PHP. I am using include functions for topnav,header,menu,sidebar and footer.
Now, i am having couple of issues in that:
1. Whenver I click on any menu item the title of my page is all the same on all pages.
2. I wanted to change the background color of the selected menu item (I am able to do it using CSS but if not using include funtions).
The code is like:
TOP nav.php
<br />
<pre lang="xml"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br />
<html><br />
<head><br />
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"><br />
<title>My title goes here!</title><br />
<br />
<link rel="stylesheet" type="text/css" href="style/style.css"><br />
<br />
</head><br />
<body><br />
<div id="wrapper"><br />
    <div id="topbar"><br />
        <div class="date"><?php echo  date("l jS \of F Y ");?></div><br />
        <ul class="topnav"><br />
<br />
                <li><a href="#">About us</a></li><br />
                <li><a href="contact.php">Contact us</a></li><br />
            </ul><br />
<br />
    </div></pre><br />

Since all my html part like title description etc are saved in topnav.php, i am not able to change the title on index.php and so on.
Please guide like on which file i need to write the code like
<br />
<pre lang="php"><?php<br />
$title="";<br />
switch ($title){<br />
case "index.php":<br />
 $title= "index title here";<br />
<br />
 break;<br />
<br />
case  "about.php":<br />
 $title= "about title here";<br />
<br />
 break;<br />
}<br />
<br />
?></pre><br />
<br />
Posted
Updated 22-May-22 11:55am

All I did was like
PHP
$path=$_SERVER['PHP_SELF'];
 $page=basename($path);
switch("$page")

And it worked.
 
Share this answer
 
<?php
    $title = "Default Title Here";
if(1st condition){
    $title = "2nd Title";
}
if(2nd condition){
    $title = "3rd title";
}
?>
<title><?php echo $title ?></title>
 
Share this answer
 
Why switch and if else just add in header.php as

and echo it title
<title><?php echo $title; ?>
Now in any where this header.php is going to include just reassign the value of $title

thus getting different title for every page.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900