Jump to content

Dynamic Page Title, how to achieve?


musnoure

Recommended Posts

Hi guys,

In index.php, I am creating files to be included in the body section yet these pages' title should go in the header, not in the body so I thought I would work it out like this:

 

-------------------header.php-------------------

 

<title><?php echo $page_title; ?></title>

 

 

 

-------------------page1.php-------------------

<?php

$page_title = 'Title of my first page';

require_once ("header.php");

text text text

?>

 

-------------------index.php-------------------

in the header:

<?php require_once ("header.php"); ?>

 

 

In the Body:

<?php require_once("page1.php"); ?>

 

 

 

THE OUTPUT:

In the header:

<title></title> (empty)

 

In the Body

text text text

 

 

I do not know what I am missing here. If anyone could help, I would greatly appreciate it!

Link to comment
Share on other sites

 

you could have an array in your session (or somewhere else, external file, included in index, etc...) that translates page names to page titles. then on each load, you just check the page name, and grab the appropriate title.

 

how are you linking from page to page? (since you're including page1.php in index, i assume you've got a structure like:

 

<a href = "index.php?page=page1"> ??

Link to comment
Share on other sites

I would need help if I were to use sessions or external files.

Yes, but I I pull the content out of the Database. For instance, the page is named auto.php and contains the following:

 

$found = mysql_query("SELECT * FROM auto") or die (mysql_error());

while ($row = mysql_fetch_assoc($found))

{

    echo '<a href="index.php?proc='.$row['id'].'">'.$row['article_title']. '</a>' .  "<br />" .

    substr($row['article'], 0, 250). '...' ;

}

 

 

 

And the link is:

<a href="index.php?proc=auto">

 

 

Link to comment
Share on other sites

An exmaple of the output when I click on "auto" link:

 

Why my Check Engine Light is on? (This is $row['article_title'])

 

Why is my Check Engine Light on? All modern vehicles have a built-in ECM (Electronic Control Module, also Electronic Control Unit) that controls the functioning of the engine and transmission and mainly keeps the engine run as efficient as possible w... (And this is $row['article'])

 

Both $row['article_title'] and $row['article'] are two fields in DB.

 

auto.php basically loads content from a table named auto in the Database and displays them like this:

 

Why my Check Engine Light is on?

Why is my Check Engine Light on? All modern vehicles have a built-in ECM (Electronic Control Module, also Electronic Control Unit) that controls the functioning of the engine and transmission and mainly keeps the engine run as efficient as possible w...

 

 

My car has been stolen!

My car, auto, autos, has been stolen! Experiencing car theft can be very frustrating especially if you have left valuable stuff inside it. It is extremely important to act as soon as you find out that it has been stolen.The first thing to do is call ...

 

 

How to change engine oil

How to change car auto, autos, engine oil Changing oil and filter for your car is a job that you can do yourself instead of paying a mechanic too much for such an easy task. Learning how to check oil and change it yourself will not only save you mone...

 

 

Tips on passing your driving road test

Tips on passing your driving road test As many think, it is a frustrating process to prepare for the road test, it can be, but the more you practice, the easier the test gets. Here is a list of some tips that will help you prepare for the test:Start...

 

 

etc.

 

etc.

 

 

So auto.php has to have a title, and when you click on the title of the article (to view full text), the entire article has to have its title as well.

 

Link to comment
Share on other sites

well, I got all that from your previous little bit of code, my question was simple: I just wanted to know if the article title is what you also want to use as a page title... I'll assume it is.

 

Since you're grabbing the articles from the database and creating links to them, you could just add the title to the links, so instead of:

 

echo '<a href="index.php?proc='.$row['id'].'">'.$row['article_title']. '</a>' .  "<br />" . substr($row['article'], 0, 250). '...' ;

 

you would have:

 

echo '<a href="index.php?proc='.$row['id'].'&title='.$row['article_title'].'">'.$row['article_title']. '</a>' .  "<br />" . substr($row['article'], 0, 250). '...' ;

 

 

and then all you need to do is:

 

<title>
<?php
echo isset($_GET['title']) ? $_GET['title'] : 'YOUR DEFAULT TITLE HERE'; // for when no article is selected 
?>
</title>

Link to comment
Share on other sites

That is one way to do it, but if you are using the ID anyway.  Run the Query through first then use what you get back from the query to spit out.

 

<?php
if ( isset ( $_GET [ "proc" ] ) && !empty ( $_GET [ "proc" ] )
{
  // Run Query
}

$title = ( isset ( $row ) ) ? $row [ "article_title" ] : "Default Title" ;

 

That should work.

 

 

 

Link to comment
Share on other sites

WebStyles, Thank you so much for your help, it did work perfect!

I still have two more questions though, I would really appreciate if you could take sometime to help me out.

 

1. Can I apply the same thing to pull out Keywords and Description Meta Tags from DB? For example meta_keywords and meta_description?

 

2. Is there anyway auto.php can have a title as well?

 

 

Again, thank you very much!

Link to comment
Share on other sites

Also, for security measures, don't you think I should use POST instead of GET?

 

Personally I use them in accordance of what they look like they would do.  GET information from , POST information to!

 

You can send a lot more information when you submit a form using POST, but if you are going to retrieve information using $_GET is the least painful method.

 

Always use MRES when dealing with post or get values though, you can never really trust what the client is sending your way.  In fact that is the #1 rule to coding: Never trust the client!  Well it is my #1 rule.

Link to comment
Share on other sites

Back to your current problem.

 

The links you make should only be what you originally had -

 

    echo '<a href="index.php?proc='.$row['id'].'">'.$row['article_title']. '</a>' .  "<br />" .

    substr($row['article'], 0, 250). '...' ;

 

Your code in index.php should detect that $_GET['proc'] is set and use the id value to do everything it needs to do to produce and output the necessary content that matches that id.

Link to comment
Share on other sites

I am still trying to figure it out.

I thought I would do this:

 

 

$result = mysql_query("SELECT meta_keywords FROM auto LIMIT 1");

while($row = mysql_fetch_assoc($result))

{

      echo $row['meta_keywords'];

}

 

 

It does pull out keywords and display them, but it's not dynamic. If you click on a different link, the keywords won't change. So I need to find a way to display keywords according to the id or something like that.

 

Table structure is as follows:

 

id article_title article article_author article_date meta_keywords meta_description

                                                                                                        word 1, word2,

                                                                                                        word 3 etc...

 

 

 

Any help is appreciated!

 

Link to comment
Share on other sites

I am still trying to figure it out.

I thought I would do this:

 

 

$result = mysql_query("SELECT meta_keywords FROM auto LIMIT 1");

while($row = mysql_fetch_assoc($result))

{

      echo $row['meta_keywords'];

}

 

 

It does pull out keywords and display them, but it's not dynamic. If you click on a different link, the keywords won't change. So I need to find a way to display keywords according to the id or something like that.

 

Table structure is as follows:

 

id article_title article article_author article_date meta_keywords meta_description

                                                                                                        word 1, word2,

                                                                                                        word 3 etc...

 

 

 

Any help is appreciated!

 

Link to comment
Share on other sites

if you're using LIMIT 1 in your query, you do not need a while loop to pull out data... The while loop basically repeats the code per each row retrieved from the database. Since you're only retrieving one row, you don't need to loop through them.

 

you can change this:

while($row = mysql_fetch_assoc($result))
{
       echo $row['meta_keywords'];
}

for this:

$row = mysql_fetch_assoc($result);
echo $row['meta_keywords'];

 

also, SELECT meta_keywords FROM auto LIMIT 1 is always pulling out the same row, since you do not have any conditions in there. you'll need to add a WHERE clause:

 

(...) where `id` = '$id' limit 1

Link to comment
Share on other sites

I made those modifications, but the output is empty.

<meta name="keywords" content=" " />

 

 

Here is the edited code:

 

$result = mysql_query("SELECT meta_keywords FROM auto WHERE 'id' = '$id' LIMIT 1");

$row = mysql_fetch_assoc($result);

echo $row['meta_keywords'];

 

 

May I ask something, do I have to enclose id with single quotes? I actually tried it with and without but same output. I tried the following: WHERE id = id, and it did work but only for the first row. when you click on a different article, the keywords stay the same, do not change accordingly. So I have no idea what I am missing here.

 

Thanks for the quick replies by the way!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.