Forum Moderators: coopster
I finally decided to post after smashing my head against the monitor a few times trying to figure this out by myself (PHP ignorant)
Here is my code:
<?php
$menu = array();
$menu['home'] = 'Home';
//Add in the format of: $menu['page name'] = 'Page Title';
$title='Home'; //Default title
function generateMenu() {
global $menu,$default,$title;
echo ' <ul>';
$p = isset($_GET['p']) ? $_GET['p'] : $default;
foreach ($menu as $link=>$item) {
$class='';
if ($link==$p) {
$class=' class="selected"';
$title=$item;
}
echo '<li><a href="?p='.$link.'"'.$class.'>'.$item.'</a></li>'.' '.' ';
}
echo '</ul>';
}
?>
As mentioned before... I want to be able to extract the page title from the <h1> tags of each page. I'm using a very basic PHP template I found on the web (http://www.dynamicdrive.com/forums/showthread.php?t=28260)
Thanks in advance
If so... you could simply use strip_tags and allow '<h1>' and then go through and pick off the h1 title you want to use.
I think you need to clarify exactly what you want a little better in order to get more help.
Here is the complete code:
<?php
//Add in the format of: $menu['page name'] = 'Page Title';
$url='http://mysite.com';
function generateMenu() {
global $menu,$default,$title;
echo ' <ul>';
$p = isset($_GET['p']) ? $_GET['p'] : $default;
foreach ($menu as $link=>$item) {
$class='';
if ($link==$p) {
$class=' class="selected"';
$title=$item;
}
echo '<li><a href="?p='.$link.'"'.$class.'>'.$item.'</a></li>';
}
echo '</ul>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Site ¦ <?php echo $title; ?></title>
<link rel="shortcut icon" href="images/favicon.ico" />
</head>
<body>
<div id="wrapper">
<div id="menu">
<?php
$default = 'home'; //Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page.
$page = isset($_GET['p']) ? $_GET['p'] : $default; //Checks if ?p is set, and puts the page in and if not, it goes to the default page.
if (!file_exists('inc/'.$page.'.php')) { //Checks if the file doesn't exist
$page = $default; //If it doesn't, it'll revert back to the default page
//NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory.
}
?>
<? include('tmpl/menu.html'); ?>
</div>
<div id="header">
</div>
<div id="content">
<?
include('inc/'.$page.'.php'); //And now it's on your page!
?>
</div>
<div id="footer">
<? include('tmpl/footer.php');?>
</div>
</div>
</body>
</html>
@ newb2seo
Yes, I think you got the idea... with the template system I'm using I cannot (or I don't know how) to create dynamic titles per each page... so I want to use the <h1> from each page as a title. Can you explain how to do this using my template?... I tried to use the strip_tags but since I'm not a PHP savvy I thing I'm doing something wrong
Thanks a lot for your help!
Instead of grabbing the <h1> and add it to the title what I did is... first added a new variable to the URL link on the menu
index.php?p=index&title=Homepage
and then instead of using
<? echo $title;>
I used
<?php echo $_GET['title']; ?>
This does the trick but I still would like to be able to extract the title from the <h1> tags of each included page
Thank you