How can I change title using php? - php

I have my index.php in which I am including the header. But the problem is that I want a different header title as I am including it in other pages well. I want it to change dynamically according to page like if its other page like shop then title should be Toys - Shop like this. I am new to PHP can anyone please help me with how to do it? Index.php:
<?php include'headernav.php';?>
<div class="header-outs" id="home">
<div class="header-bar">
<div class="info-top-grid">
<div class="info-contact-agile">
<ul>
<li>
<span class="fas fa-phone-volume"></span>
<p>+(000)123 4565 32</p>
</li>
<li>
<span class="fas fa-envelope"></span>
<p>info#example1.com</p>
</li>
<li>
</li>
</ul>
</div>
</div>
</div>
</div>
Headernav.php:
<!DOCTYPE html>
<html lang="zxx">
<head>
<title>Toys-Home</title>
<!--meta tags -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="" />
<link href="css/style.css" rel='stylesheet' type='text/css' media="all">
<!--//stylesheets-->
<link href="//fonts.googleapis.com/css?family=Sunflower:500,700" rel="stylesheet">
<link href="//fonts.googleapis.com/css?family=Open+Sans:400,600,700" rel="stylesheet">
</head>
<body>

You can do something like this:
index.php:
<?php
$pagename = 'Toys-Home';
include ('headernav.php');?>
<div class="header-outs" id="home">
<div class="header-bar">
<div class="info-top-grid">
<div class="info-contact-agile">
<ul>
<li>
<span class="fas fa-phone-volume"></span>
<p>+(000)123 4565 32</p>
</li>
<li>
<span class="fas fa-envelope"></span>
<p>info#example1.com</p>
</li>
<li>
</li>
</ul>
</div>
</div>
</div>
</div>
headernav.php
<!DOCTYPE html>
<html lang="zxx">
<head>
<title><?php print $pagename; ?></title>
<!--meta tags -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="" />
<link href="css/style.css" rel='stylesheet' type='text/css' media="all">
<!--//stylesheets-->
<link href="//fonts.googleapis.com/css?family=Sunflower:500,700" rel="stylesheet">
<link href="//fonts.googleapis.com/css?family=Open+Sans:400,600,700" rel="stylesheet">
</head>
<body>
You'd have to set the $pagename variable on each page if that's something you're looking for. Each page would have the title you've set in that variable.

#Loupeznik's accepted answer provides a solution. An alternative is to use functions in include files and call them from the main PHP. You will have to write a bit more code, but you will get more flexibility. E.g. you will be able to add different css or js files on different pages letter or use a single include file for both header and footer or other common PHP code.
The way to do this is to add a function called writeHeaderNav() or something similar and then call it from index.php with $pagename as an argument.
headernav.php
<?php
function writeHeaderNav($pagetitle){
echo"<!DOCTYPE html>
<html lang='zxx'>
<head>
<title>$pagetitle</title>
<!--meta tags -->
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta name='keywords' content='' />
<link href='css/style.css' rel='stylesheet' type='text/css' media='all'>
<!--//stylesheets-->
<link href='//fonts.googleapis.com/css?family=Sunflower:500,700' rel='stylesheet'>
<link href='//fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet'>
</head>
<body>";
}
//you can have other PHP code here if needed
function writeFooter(){
//echo html for common footer
}
?>
index.php
<?php
$pagename = 'Toys-Home';
include ('headernav.php');
writeHeaderNav($pagename);
// or you can use writeHeaderNav('Toys-Home');
?>
<div class="header-outs" id="home">
<!--rest of code-->

You can put a variable like this in your Headernav.php file and have it defined before your include
< title> < ?php echo "$my_title"; ?>
This variable can be changed by the way you get it
from your url ($_GET)
from a sql query
from a session variable
...

Related

Using PHP "include" function adds a second header to page

I've just updated my site to use the PHP "include" function to add a CSS menu to every page. When I look at the result of the include function online, it has added a second HTML and Head to each page. The web site and menu work perfectly but this looks like bad design to me.
My CSS code is in an HTML file called menu.html. I've tried to remove the HTML and Head code from the menu.html file hoping to avoid the second instance in the final result but it doesn't work. The menu doesn't display correctly.
Is this a problem or should I do something else to eliminate the second insertion of the HTML and Head?
Here is partial code from a file using the "include" function to call the menu:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD>
<META NAME="Description" CONTENT="Gary and Nancy's travel photo gallery">
<META NAME="Keywords" CONTENT="Gary and Nancy's travel photo gallery">
<META http-equiv="title" CONTENT="Gary and Nancy's travel photo gallery">
<META NAME="revisit-after" CONTENT="30 days">
<META NAME="classification" CONTENT="travel">
<META NAME="robots" CONTENT="all">
<META NAME="distribution" CONTENT="global">
<META NAME="products" CONTENT="Travel Photos">
<TITLE>Gary and Nancy's Photo Gallery</TITLE>
<link Rel=Stylesheet href="style.css" type="text/css">
</HEAD>
<font class=a24b color="#937666">Gary and Nancy's Photo Gallery</font>
<br><br>
<div class="menu">
<?php include 'menu.html';?>
</div>
<br>
Here is partial code of the menu.html file:
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="mbcsmbmcp.css" type="text/css" />
</head>
<body>
<div id="mbmcpebul_wrapper" style="max-width: 804px;">
<ul id="mbmcpebul_table" class="mbmcpebul_menulist css_menu">
<li class="first_button"><div class="buttonbg gradient_button gradient38" style="width: 62px;">Home</div></li>
<li><div class="buttonbg gradient_button gradient38" style="width: 94px;"><div class="arrow"><a>Australia</a></div></div>
<ul class="gradient_menu gradient116">
<li class="gradient_menuitem gradient29 first_item">Cairns</li>
<li class="gradient_menuitem gradient29">Heron Island</li>
<li class="gradient_menuitem gradient29">Port Douglas</li>
<li class="gradient_menuitem gradient29 last_item">Great Barrier Reef</li>
</ul></li>
Here is the resulting code that I see online i.e. including double HTML and HEAD:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD>
<META NAME="Description" CONTENT="Gary and Nancy's travel photo gallery">
<META NAME="Keywords" CONTENT="Gary and Nancy's travel photo gallery">
<META http-equiv="title" CONTENT="Gary and Nancy's travel photo gallery">
<META NAME="revisit-after" CONTENT="30 days">
<META NAME="classification" CONTENT="travel">
<META NAME="robots" CONTENT="all">
<META NAME="distribution" CONTENT="global">
<META NAME="products" CONTENT="Travel Photos">
<TITLE>Gary and Nancy's Photo Gallery</TITLE>
<link Rel=Stylesheet href="style.css" type="text/css">
</HEAD>
<Body>
<center>
<font class=a24b color="#937666">Gary and Nancy's Photo Gallery</font>
<br><br>
<div class="menu">
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="mbcsmbmcp.css" type="text/css" />
</head>
<body>
<div id="mbmcpebul_wrapper" style="max-width: 804px;">
<ul id="mbmcpebul_table" class="mbmcpebul_menulist css_menu">
<li class="first_button"><div class="buttonbg gradient_button gradient38" style="width: 62px;">Home</div></li>
<li><div class="buttonbg gradient_button gradient38" style="width: 94px;"><div class="arrow"><a>Australia</a></div></div>
<ul class="gradient_menu gradient116">
<li class="gradient_menuitem gradient29 first_item">Cairns</li>
<li class="gradient_menuitem gradient29">Heron Island</li>
<li class="gradient_menuitem gradient29">Port Douglas</li>
<li class="gradient_menuitem gradient29 last_item">Great Barrier Reef</li>
</ul></li>
"this looks like bad design to me"
...yep. You should not have a HTML document inside another one. Get rid of the inner one.
Menu.html should contain the HTML specific to the menu, and nothing else. Then you can slot it into the body of your main document using include(). You probably need to keep the stylesheet link, too
Get rid of all the header information on menu.html, just keep this line below (put it in the header of the main file)
<link rel="stylesheet" href="mbcsmbmcp.css" type="text/css" />

How to include file php

I have a problem with including a php file
PagePrevie.php :
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="Style/css/bootstrap.min.css" />
<script src="Style/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="Style/css/Header-Picture.css">
<link rel="stylesheet" href="Style/css/Footer-with-logo.css">
<title></title>
</head>
<body class="container lg-warning">
<?php
include 'include/LogoUMBB.php';
include 'include/Header.php';
?>
<div class="container bg-success text-primary" style="width:500px;padding: 20px;">
</div>
<br />
<?php
include 'include/Footer.php';
?>
</body>
</html>
The problem is that this file is located in a directory that contains other web page with the same code
And here is the structure of files:
PHP told you your path is incorrect, add "../" so you get correct include path.
include '../include/LogoUMBB.php';
include '../include/Header.php';
include '../include/Footer.php';

Fatal error: Cannot redeclare class data in D:\xampp\htdocs\Test1\tedboy\include\home\header.php on line 11

This code work before this. after i format my laptop, i cant use this code anymore. please help me to solve this. this is my final year project. Fatal error: Cannot redeclare class data in D:\xampp\htdocs\Test1\tedboy\include\home\header.php on line 11
?php include('db.php'); ?>
<?php session_start(); ?>
<?php //print_r($_SESSION['cart']); ?>
<?php date_default_timezone_set('Asia/Kuala_Lumpur'); ?>
<?php
$jim = new data();
$countproduct = $jim->countproduct();
$cat = $jim->getcategory();
class data {
function countproduct(){
$count = 0;
$cart = isset($_SESSION['cart']) ? $_SESSION['cart']:array();
foreach($cart as $row):
if($row['qty']!=0){
$count = $count + 1;
}
endforeach;
return $count;
}
function getcategory(){
$result = mysql_query("SELECT * FROM category");
return $result;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>Tedboy Bakery</title>
<link rel="icon" href="images/home/Tedboy.png" type="image" />
<link href="css/bootstrap1.min.css" rel="stylesheet">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/font-awesome.min.css" rel="stylesheet">
<link href="css/main1.css" rel="stylesheet">
<link href="css/responsive1.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
</head><!--/head-->
<body>
<header id="header"><!--header-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Tedboy Bakery</title>
<link rel="icon" href="images/home/damailers_logo.png" type="image" />
<link href="styles/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="container">
<header>
<nav>
<ul id="nav">
<li>Home</li>
<li>Menu</li>
<li>Cart <span class="badge"></span></li>
<li>Gallery</li>
<li><a href="contact.php" >Contact</a></li>
<li>About Us</li>
</ul>
</nav>
</header>
<div class="wrapper">
<div class="border"></div>
</div>
</body>
</html>
</header><!--/header-->
that's because you are using the class before defining it!
Move the Definition of the data class before this line:
$jim = new data();
First, you should declare your data class before using it.
class data { ...
$jim = new data();
Then, double check that your included files don't have the data class declaration already.

when i normally run html bootstrap enabled code, it shows proper output but when i load it using codeigniter view it shows different output

This is my directory:
miuse/application
miuse/application/views/templates/header.php
miuse/bootstrap/css
miuse/bootstrap/js
miuse/bootstrap/fonts
This is my controller code:
<?php
class page extends CI_controller{
public function view($page= 'home')
{
if(!file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
show_404();
}
$data['title']='Moodlist home';
$this->load->view('templates/header', $data);
}
}
?>
This is my view code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>
<?php echo $title; ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="http://localhost/miuse/bootstrap/css/bootstrap.css">
<link rel="stylesheet" href="header.css" type="text/css">
</head>
<body>
<header class="container">
<div class="row">
<h1 class="col-sm-4">List</h1>
<nav class="col-sm-8 text-right">
<p>Home</p>
<p>Category</p>
<p>About us</p>
</nav>
</div>
</header>
</body>
</html>
when i run my view normally in a browser it shows proper output but when i load it with codeigniter view then it shows different output which i dont want.
I guess your header.css file is not being loaded.
Put the header.css file in miuse/bootstrap/css directory.
And change your code from,
<link rel="stylesheet" href="header.css" type="text/css">
to
<link rel="stylesheet" href="http://localhost/miuse/bootstrap/header.css" type="text/css">
When loading css and other stuff I would recommend using base_url() in your head area
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('bootstrap/css/bootstrap.css');?>">
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/header.css');?>">
</head>
Make sure you have set the base url in config.php and autoload the url helper.

How can I incorporate inline CSS styles and stylesheets relating to one page only when using the PHP 'header'?

I have a 'header.php' file and I am incorporating it into my html pages using the following code:
<?php /*
$pageTitle = "Home";
$section = "Home";
include('inc/header.php');
?>
The PHP header file contains a number of CSS stylesheets. Some of these CSS stylesheets I only want to include on one page - e.g. I only want to include 'index.css' on the page 'index.php'. How can I include these stylesheets when I am using the same 'header.php' file in every page of the site?
Is there a way of specifying these styles in the individual pages themselves, rather than in the header file? If so, can these styles be placed in another element, even if the element has already been used (opened and closed) in the header file?
Here is the code for my 'header.php' file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;">
<title><?php echo $pageTitle ?> || Young Academy </title>
<meta name="description" content="Based in Pinner in North-West London, the Young Academy has been creating the highest standard of musical and dramatic education for 30 years.">
<meta name="keywords" content="Piano, Flute, Music Theory, Singing, Violin, Concerts, Pinner, Middlesex, Harrow, Teacher">
<meta name="author" content="Barbara Young">
<!--Stylesheets!-->
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css"/>
<link rel="stylesheet" type="text/css" media="all" href="css/text.css" />
<link rel="stylesheet" type="text/css" media="all" href="css/960_16_col.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/style.css"/>
<link rel="stylesheet" type="text/css" media="screen" href="css/contact.css"/>
<!--Google fonts!-->
<link rel="stylesheet" type="text/css" media="all" href="http://fonts.googleapis.com/css?family=Quicksand:400,700">
<link href='http://fonts.googleapis.com/css?family=Titillium+Web' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Noto+Serif:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Karla:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Marck+Script' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto:400,700,500,500italic,400italic' rel='stylesheet' type='text/css'>
**<link rel="stylesheet" type="text/css" media="screen" href="css/index.css"/>
<link rel="stylesheet" type="text/css" media="screen" href="css/bgaudioplayer.css"/>**
<!--Meta tags!-->
<meta name="author" content="Robert Young" />
<meta name="copyright" content="2013 by Robert Young" />
<meta name="keywords" content="Music lessons, Pinner, music tuition, piano lessons, speech and drama, London" />
<meta name="description" content="Site of the Young Academy, a private music tuition business based in Pinner" />
<script src="../js/modernizr.custom.63826.js"></script>
<script src="../js/html5shiv.js"></script>
</head>
<body>
<div class="header">
<div class="container_16 clearfix">
<ul class="nav grid_16 alpha">
<li><a class="<?php if ($section == "Home") {echo "here"; } ?>" href="index.php" accesskey="1"> Home </a></li>
<li><a class="<?php if ($section == "About Us") {echo "here"; } ?>" href="about_us.php" accesskey="2"> About </a></li>
<li><a class="<?php if ($section == "What We Do") {echo "here"; } ?>" href="what_we_do.php" accesskey="3"> What We Do </a></li>
<li><a class="<?php if ($section == "Enrolement") {echo "here" ; } ?>" href="enrolement.php" accesskey="4"> Enrolement </a></li>
<li><a class="<?php if ($section == "Contact Us") {echo "here" ; } ?>" href="contact.php" accesskey="5"> Contact Us </a></li>
<!--<li> Blog </li>-->
</ul>
<div id="logo">
<img src="img/Logo original.gif" alt="The Young Academy"/>
</div>
<div class = "grid_4 alpha">
<div class="telephone grid_4 alpha">
<img src="img/phone_icon_white.png" alt="small telephone logo" class="align-left small" />
<p> +44 (0)20 8866 3813 </P>
</div>
<div class="timezone">
<p><?php
date_default_timezone_set('Europe/London');
mktime(0,0,0,1,1,1970);
echo date('l, d F Y');
?> </p>
<!--<form action="http://www.example.com/login.php">
<p>
<input type="text name=search" size="20+" id="search" value="Search this site"
</p>
</form>!-->
</div>
</div>
<ul class="secondmenu grid_6 push_3">
<li><a class="<?php if ($section == "Musical Glossary") {echo "here" ; } ?>" href="glossary.php">Musical Glossary </a></li>
<li><a class="<?php if ($section == "FAQ") {echo "here" ; } ?>" href="faq.php">FAQ</a></li>
<ul>
</div>
I want to include the stylesheets in bold only on the index.php page. Can I include them like this:
<?php
include('inc/header.php');
?>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="css/index.css"/>
<link rel="stylesheet" type="text/css" media="screen" href="css/bgaudioplayer.css"/>
</head>
<article class="content">
<p class="grid_9 alpha"><i>Outstanding vocal and instrumental tuition.</i></p>
</article>
<article class="second-content">
<p class="grid_6 alpha omega">Develop a passion for music.</p>
</article>
<article class="third-content">
<p class="grid_5 alpha omega">Become a virtuoso.</p>
</article>
etc.
Any help would be much appreciated!
Thanks,
Robert.
I have just tried the above code and it happens to work! You can include head tags in both the header.php file and the main (index.php) site file.
Cheers!
long story short you need some sort of router. but Looking at your code it seems like you may not have a firm grasp on Object Oriented Programming. what you can do though is look at the URI with $_SERVER['REQUEST_URI'] if the string is lets say /about then load in resources for about-stylesheet.css.
You can do this with an if else statement that replaces the link to your CSS and refers to the unique variable you sent for the page you want to pull the CSS in for:
<link rel="stylesheet" type="text/css" media="screen" href="
<?php
if ($pageTitle == "Home") {
echo "css/index.css";
else ($pageTitle == "SomeOtherTitle") {
echo "css/relativelnk.css";
}
?>
"/>

Categories