Forum Moderators: coopster

Message Too Old, No Replies

Generate page titles from <h1> without a database

Generate page titles from <h1> without a database

         

Danno

8:55 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



Hi everyone!

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>'.'&nbsp;'.'&nbsp;';
}
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

phanty

2:01 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



Can you post all the code including the HTML? I was able to test the tutorial you listed locally without any issues, but I am trying to understand exactly what your issue is.
Joseph

newb2seo

3:07 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



hi.
When you say you want to "extract" it sounds as if you need to load a page in and then read it and fetch the h1 tag?
Is that what you mean?

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.

Danno

2:03 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



@phanty

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!

Danno

9:08 pm on Jun 29, 2009 (gmt 0)

10+ Year Member



So I manage to add the titles but not exactly as I wanted...

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&amp;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