Insert dynamic H1 tag in web page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adpd
Forum Newbie
Posts: 9
Joined: Tue Jun 14, 2005 4:37 am

Insert dynamic H1 tag in web page

Post by adpd »

Hi,

I am trying to use a simple PHP script to insert a H1 title on a HTML web page.

I am determining the variable ($thisPage) in a script in the HTML web page:

Code: Select all

<?php $thisPage="Services"; ?>
The include would look like this:

Code: Select all

<?php if ($thisPage=="Services") echo "Services"; ?>
<?php if ($thisPage=="News") echo "News"; ?>
<?php if ($thisPage=="About Us") echo "About Us"; ?>
<?php if ($thisPage=="Contact") echo "Contact"; ?>
This works fine in this example.

However, when the page is deeper than top level, I might want the variable to be:

Code: Select all

<?php $thisPage="Services Administration"; ?>
But I still want the H1 tag to show the top level information (i.e. Services).

How would I rewrite my include to say: if $thisPage begins with the word "Services", echo "Services"?

Alternatively, if you know of a better way of achieving this, I would appreciate your feedback

Thanks in advance
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

example

Code: Select all

$string = "services administration";
// explode on " "
$explode = explode(" ", $string);
if (in_array("services", $explode))
{
   // the word is in the array print whole string
   print "<h1>Services</h1>";
}
else
{
   // not in array
}
Last edited by malcolmboston on Tue Jun 14, 2005 7:46 am, edited 2 times in total.
adpd
Forum Newbie
Posts: 9
Joined: Tue Jun 14, 2005 4:37 am

Post by adpd »

Thanks for the reply.

However, I am not sure this answers the question.

I want to know that if the page isn't "Services", but begins with the word "Services", still make $thisPage "Services".

For example, take IBM - they offer Services to the IT Sector.

$thisPage might be "Services IT Sector"; showing that the top level is Services, and the second level is IT Sector.

Taking it further - to a third level:

$thisPage might be "Services IT Sector Software"; the page you are on is "Software" in the "IT Sector" which is a "Service" offered.

I would still want the H1 tage to show "Services". That is, if the variable begins with the word "Services", display "Services".

How would I write this in PHP?

Thanks once again.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

the code above does what you are asking, it requires a little editing to turn it from an example into a working "dynamic" function.

basically the code puts each word into an array, checks the array for ther keyword services and if found, prints the services header
adpd
Forum Newbie
Posts: 9
Joined: Tue Jun 14, 2005 4:37 am

Post by adpd »

Many thanks.

I am trying to implement this code, but am getting an error on line 4:

Warning: Wrong datatype for second argument in call to in_array in (web_server_path)/sec_nav_h1.inc.php on line 4

Any ideas what this might be.

I am a complete noob to PHP (and programming), so apologies if I am asking a stupid question (or missing something obvious).

Thanks.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

whoops, fixed
adpd
Forum Newbie
Posts: 9
Joined: Tue Jun 14, 2005 4:37 am

Post by adpd »

Thanks Malcolm.

How would I go about constructing this into a larger array that would encompass all top level navigation?

For example, if my top level is: "Services", "News", "About Us", "Contact US".

The array should then display a top level name in H1 tags, if $thisPage begins with one of those words.

An example is:

Code: Select all

<php? $thisPage="About Us Meet the Team"; ?>
This should return the result: <h1>About Us</h1>.

I am struggling with getting the array to work.

To clarify my set-up, $thisPage is manually defined before the <head> tag of a HTML page.
I want to dynamically generate a <h1> in the page by examining $thisPage and deciding what section the document is in.

Thanks in advance.
adpd
Forum Newbie
Posts: 9
Joined: Tue Jun 14, 2005 4:37 am

Post by adpd »

I am still struggling with this...

Anybody got any suggestions (apart from to give up!)

Thanks.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

tbh you should redesign your database to deal with sub-headings on a less sloppy basis.

however i will write some code for you, gimme 10 mins to get a cuppa tea :)
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

example

Code: Select all

function GetHeadings ($string)
{
   $seperator = "&nbsp;>&nbsp; ";
   // first check if the string is more than one word
   $word_count = str_word_count($string, 0);
   if ($word_count >= 1)
   {
      // theres more than one word
      // do an explode on " " to seperate words
      $explode = explode(" ", $string);
      print "<h1>";
      // do the for loop less than but NOT equal too as explode arrays start at 0
      for ($i = 0; $i < $word_count; $i++)
      {
         // first decide whether we need to print the seperator
         if ($i = 0; !== ($word_count - 1) || ($i !== 0))
         {
            print $seperator;
         }
         print $string;
      }
      print "<h1>";
   }
   else
   {
      // only one word, just print
      print "<h1>{$string}</h1>";
   }
}

Code: Select all

// usage
GetHeadings ($string = "Services Administration Vacancies")
you will need to use a 'hack' if you want to use links in there
adpd
Forum Newbie
Posts: 9
Joined: Tue Jun 14, 2005 4:37 am

Post by adpd »

Thanks for that Malcolm.

However, first I should clarify that I am not using a database to generate the site.

I have a static website with some dynamic functions - using PHP to do the dynamic bits.

I specify at the beginning of each page what it is, relative to the information architecture using:

Code: Select all

<? $thisPage="Services"; ?>
The reason I do this is to control the secondary-navigation.

My secondary-navigation is a long unorderd list (<ul>) with many sub-lists, e.g:

Code: Select all

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Services&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Administration&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Weekly Administration&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Monthly Administration&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Cleaning&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;News&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;About Us&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Why Us &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quote;#&quote;&gt;History&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Contact Us&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
Each of these has PHP in them to decide what page a user is on (from the $thisPage definition at the beginning of the page).

Based upon that, the secondary-navigation only displays the relevant sections, and sub-section as necessary (based upon the $thisPage variable and clever use of more PHP and CSS).

For example, if I was on a Service page, the secondary-navigation would look like:

Code: Select all

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Services&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Administration&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Weekly Administration&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Monthly Administration&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Cleaning&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;News&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;About Us&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Contact Us&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
And if I was in the About Us section, I would get:

Code: Select all

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Services&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;News&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;About Us&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Why Us &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quote;#&quote;&gt;History&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quote;#&quote;&gt;Contact Us&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
This allows me to use a master menu file as a PHP include, reducing maintenance and making it easy to grow the site when I need to.

That is why I started with:

Code: Select all

<?php if ($thisPage=="Services") echo "Services"; ?>
<?php if ($thisPage=="News") echo "News"; ?>
<?php if ($thisPage=="About Us") echo "About Us"; ?>
<?php if ($thisPage=="Contact") echo "Contact"; ?>
But that only works if there is an exact match (and it is quiet messy too).

I need the code to say:

if ($thisPage[is equal to, or begins with]"Services") echo "Services"; ?>

The [bit in here] in the code is the PHP or regular expression that I need.

The H1 tags are in the webpage, and this script is being called as an include, between the H1 tags.

As for your kind response, I do not know how to get that to work in my situation. I can only apologise for my lack of understanding (and can promise to try harder to learn!).

Thanks once again.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

you wrote: if ($thisPage[is equal to, or begins with]"Services") echo "Services"; ?>
my first example does this with a little editing

Code: Select all

$string = "services administration";
// explode on " "
$explode = explode(" ", $string);
if ($explode[0] == "services"))
{
   // the word is in the array print whole string
   print "<h1>Services</h1>";
}
else
{
   // not in array
}
adpd
Forum Newbie
Posts: 9
Joined: Tue Jun 14, 2005 4:37 am

Post by adpd »

This may be a silly question, but how do I tie that code back to my $thisPage variable?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ok, lets go back to the 2nd example which is in my opinion, more suited to you

Code: Select all

// make it use the $thispage var
function GetHeadings ($string)
{
   $seperator = "&nbsp;>&nbsp; ";
   // first check if the string is more than one word
   $word_count = str_word_count($string, 0);
   if ($word_count >= 1)
   {
      // theres more than one word
      // do an explode on " " to seperate words
      $explode = explode(" ", $string);
      print "<h1>";
      // do the for loop less than but NOT equal too as explode arrays start at 0
      for ($i = 0; $i < $word_count; $i++)
      {
         // first decide whether we need to print the seperator
         if ($i = 0; !== ($word_count - 1) || ($i !== 0))
         {
            print $seperator;
         }
         print $string;
      }
      print "<h1>";
   }
   else
   {
      // only one word, just print
      print "<h1>{$string}</h1>";
   }
}

Code: Select all

// usage
GetHeadings ($string = $thispage);
you could then write another function after the above code to parse through the subheadings associated with that page (as defined by an array?)


i really cant see the problem here, maybe its to early in the morning :roll:
adpd
Forum Newbie
Posts: 9
Joined: Tue Jun 14, 2005 4:37 am

Post by adpd »

Maybe it is too early in the morning!

I get a parse error on line 2 with that code (the long one, not the 2-liner).

It may be helpful if I give you all the code - you can then copy and paste it and see if you are getting what I get.

The web page is as follows:

Code: Select all

<!DOCTYPE html PUBLIC &quote;-//W3C//DTD XHTML 1.1//EN&quote; &quote;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quote;>
<html xmlns=&quote;http://www.w3.org/1999/xhtml&quote; xml:lang=&quote;EN&quote;>
<? $thisPage=&quote;Portfolio&quote;; ?>
<head>
<meta http-equiv=&quote;Content-Type&quote; content=&quote;text/html; charset=iso-8859-1&quote; />
<title>Title</title>
<link href=&quote;/screen.css&quote; rel=&quote;stylesheet&quote; type=&quote;text/css&quote; />
</head>
<body>
<div id=&quote;wrapper&quote;>
  <div id=&quote;main&quote;>
    <div id=&quote;strapline&quote;>
      <h1>Strapline</h1>
    </div>
    <div id=&quote;content&quote;>
      <h1>Heading for Page</h1>
      <h2>Heading 2</h2>
      <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam condimentum
        dolor sed orci. Proin ipsum. Aliquam venenatis condimentum augue. Suspendisse
        gravida. Donec suscipit. Donec blandit. Integer enim dui, tempus vitae,
        dignissim faucibus, mattis ut, arcu. Suspendisse potenti. Suspendisse
        convallis. Suspendisse venenatis lectus eu urna. Praesent sed mi. Fusce
        cursus facilisis mi. Morbi malesuada nisl eget nisl. Proin gravida, nulla
        eu molestie condimentum, neque dui pellentesque lectus, a semper quam
        tellus sed quam. Nam ornare sapien ut nibh. Maecenas ac nunc. Sed non
        risus.</p>
    </div>
  </div>
  <div id=&quote;side&quote;>
    <div id=&quote;prim-nav&quote;>
      <object classid=&quote;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quote; codebase=&quote;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0&quote; width=&quote;185&quote; height=&quote;192&quote;>
        <param name=&quote;movie&quote; value=&quote;/flash/menu.swf&quote; />
        <param name=&quote;quality&quote; value=&quote;high&quote; />
        <embed src=&quote;/flash/menu.swf&quote; quality=&quote;high&quote; pluginspage=&quote;http://www.macromedia.com/go/getflashplayer&quote; type=&quote;application/x-shockwave-flash&quote; width=&quote;185&quote; height=&quote;192&quote;></embed>
      </object>
    </div>
    <div id=&quote;sec-nav-wrapper&quote;>
      <h1><? include($_SERVER&#1111;'DOCUMENT_ROOT'] . '/includes/sec_nav_h1.inc.php'); ?></h1>
      <? include($_SERVER&#1111;'DOCUMENT_ROOT'] . '/includes/sec_nav.php'); ?>
    </div>
  </div>
  <div id=&quote;footer&quote;>
    <div id=&quote;search&quote;>
      <? include($_SERVER&#1111;'DOCUMENT_ROOT'] . '/includes/search_form.inc'); ?>
    </div>
    <div id=&quote;text-links&quote;>
      <? include($_SERVER&#1111;'DOCUMENT_ROOT'] . '/includes/text_nav.php'); ?>
    </div>
  </div>
  <div id=&quote;address&quote;>
    <? include($_SERVER&#1111;'DOCUMENT_ROOT'] . '/includes/address.inc'; ?>
  </div>
</div>
</body>
</html>
The important includes are the sec_nav.php:

Code: Select all

<ul>
  <li<? if ( strpos ($thisPage, &quote;Services&quote; ) === false ) echo &quote; class=\&quote;no-sub\&quote;&quote;; ?>><a href=&quote;#&quote;<? if  ( strpos ($thisPage, &quote;Services&quote; ) !== false ) echo &quote; class=\&quote;current\&quote;&quote;; ?>>Services</a>
    <ul<? if ( strpos ($thisPage, &quote;Services&quote; ) === false ) echo &quote; class=\&quote;no-sub\&quote;&quote;; ?>>
      <li><a href=&quote;#&quote;<? if ($thisPage==&quote;Services Administration&quote;) echo &quote; class=\&quote;current\&quote;&quote;; ?>>Administration</a>
        <ul<? if ( strpos ($thisPage, &quote;Services Administration&quote; ) === false ) echo &quote; class=\&quote;no-sub\&quote;&quote;; ?>>
          <li><a href=&quote;#&quote;<? if ($thisPage==&quote;Services Administration Weekly Administration&quote;) echo &quote; class=\&quote;current\&quote;&quote;; ?>>Weekly Administration</a></li>
          <li><a href=&quote;#&quote;<? if ($thisPage==&quote;Services Administration Monthly Administration&quote;) echo &quote; class=\&quote;current\&quote;&quote;; ?>>Monthly Administration</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li<? if ( strpos ($thisPage, &quote;News&quote; ) === false ) echo &quote; class=\&quote;no-sub\&quote;&quote;; ?>><a href=&quote;#&quote;<? if ($thisPage==&quote;News&quote;) echo &quote; class=\&quote;current\&quote;&quote;; ?>>News</a></li>
  <li<? if ( strpos ($thisPage, &quote;About Us&quote; ) === false ) echo &quote; class=\&quote;no-sub\&quote;&quote;; ?>><a href=&quote;#&quote;<? if ($thisPage==&quote;About Us&quote;) echo &quote; class=\&quote;current\&quote;&quote;; ?>>About Us</a>
    <ul<? if ( strpos ($thisPage, &quote;About Us&quote; ) === false ) echo &quote; class=\&quote;no-sub\&quote;&quote;; ?>>
      <li><a href=&quote;#&quote;<? if ($thisPage==&quote;About Us Why Us&quote;) echo &quote; class=\&quote;current\&quote;&quote;; ?>>Why Us?</a></li>
      <li><a href=&quote;#&quote;<? if ($thisPage==&quote;About Us History&quote;) echo &quote; class=\&quote;current\&quote;&quote;; ?>>History</a></li>
    </ul>
  </li>
  <li<? if ( strpos ($thisPage, &quote;Contact Us&quote; ) === false ) echo &quote; class=\&quote;no-sub\&quote;&quote;; ?>><a href=&quote;#&quote;<? if ($thisPage==&quote;Contact Us&quote;) echo &quote; class=\&quote;current\&quote;&quote;; ?>>Contact Us</a></li>
</ul>
And the sec_nav_h1.inc.php, which is the bit I need help with.

The sec_nav_h1.inc.php should generate either: "Services", "News", "About Us", or "Contact Us", depending on the section that you are in.

Thanks in advance.
Post Reply