Jump to content

Dynamic Titles In Php


thinkthinker
 Share

Recommended Posts

Hello all!

 

Brand new to this here forum! I am pretty new to PHP and have a minor issue I have been working on for days. I hope you can shed some light on this.

 

I am trying to create a dynamic <title>. The variable is caleld $headline which is created in the PHP/MSQL Select script which is located in the body. Because the header loads first it doesn't recognize the php in the body which I need to pull for the dynamic titles to work. Here is my body script:

<?php

if (!isset($_REQUEST['page'])&&!isset($_REQUEST['story']))

{

header('Location: index.php');

exit;

}

 

$page = $_REQUEST['page'];

$story = intval($_REQUEST['story']);

 

include_once('db_fns.php');

include_once('header.php');

 

$handle = db_connect();

if($story)

{

$query = "select * from stories

where id = '$story' and

published is not null";

}

else

{

$query = "select * from stories

where page = '$page' and

published is not null

order by published desc";

}

$result = $handle->query($query);

 

while ($story = $result->fetch_assoc())

{

// title

$title = 'Hello!';

// headline

$id = $story['id'];

echo "<a href='page.php?story=$id'><h2>{$story['headline']}</h2></a>";

//picture

if ($story['picture'])

{

echo '<div style="float:right; margin:0px 0px 6px 6px;">';

echo '<img src="resize_image.php?image=';

echo urlencode($story[picture]);

echo '&max_width=200&max_height=120" align = right/></div>';

}

// byline

$w = get_writer_record($story['writer']);

echo '<br /><p class="byline">';

echo $w[full_name].', ';

echo date('M d, H:i', $story['modified']);

echo '</p>';

// main text

echo $story['story_text'];

}

include_once('footer.php');

?>

 

What I need to echo in the title is this: echo "{$story['headline']}"; which I cannot pull because of the senerio. Any suggestions?

 

Thanks!

 

S the ThinkThinker

Link to comment
Share on other sites

Welcome to the forums thinkthinker :tchrocks:

 

>echo "<a href='page.php?story=$id'><h2>".$story['headline']."</h2></a>";

 

Or something along those lines.

 

Thanks for the help and welcomes! Actually the problem is within something along these lines:

 

>echo "$story['headline'].";

 

There is actually a header.php file where I want to put the code to echo the headline which is located in the page.php file. My problem is since the head loads first it can't pull the data from the body. Is there a pre loader or something that I can do to make it work?

 

I would like to echo this:

 

><title><?php echo "$story['headline']."; ?> </title>

Which would bring up the page title based upon the headline in the above mentioned script.

 

One way I got it to work was compile the entire script above into the header. That causes duplicate scrips so the body doesn't execute when the head does. Any suggestions to make this work?

 

Thanks again for all your help!!!

 

Sean

Think Thinker

Edited by thinkthinker
Link to comment
Share on other sites

You could write it external to the header and use the "include" tag in the header to load it.

 

Hi Bruce, excuse me if I am wrong maybe I just don't understand. If I add it as an include doesn't this accomplish the same thing? Either way its getting loaded into the header right? Maybe I will try adding the page.php text into an include and see if that works. I am not sure if I can post URLs on here or not if so let me know and I will show you where I am testing this live and you can see where I am at.

 

Thanks!

Link to comment
Share on other sites

You need to split your code up into functions defined at the top of the file. As soon as the page loads, pull the info from the database. Then call a separate function from the body of your page that will actually display the info (all your "echo" statements).

 

Also, make sure you're sanitizing the input before you use it in a db query or your script may be vulnerable to SQL injection attacks.

Link to comment
Share on other sites

You need to split your code up into functions defined at the top of the file. As soon as the page loads, pull the info from the database. Then call a separate function from the body of your page that will actually display the info (all your "echo" statements).

 

Also, make sure you're sanitizing the input before you use it in a db query or your script may be vulnerable to SQL injection attacks.

 

Thanks click i'll give er a whirl.

Link to comment
Share on other sites

Thanks click i'll give er a whirl.

 

Sorry to be a pain... maybe you can tell me if I am getting close on this. Would this be a legitimate function?

 

>function connect()
{

 if (!isset($_REQUEST['page'])&&!isset($_REQUEST['story']))
 {
header('Location: index.php');
exit;
 }
 }
 connect();

Edited by thinkthinker
Link to comment
Share on other sites

If you're asking if that's the correct way to define and call a function, then yes. You don't HAVE to separate it into functions, but it makes it easier to read and maintain if it's broken down into logical chunks.

 

I'm a little unclear on something: Is the query only returning one story or many? You say that you want to use $story['headline'] as the page <title>, but you set and display $story in a while loop, implying that the database is going to return more than one story.

 

If you're only displaying one "story", then just connect to the database and put the results in the variable $story before the "include('header.php');". That way, $story['headline'] will have been initialized for header.php to use.

Link to comment
Share on other sites

  • 1 month later...

I've only quickly read this entire thread but here's my thinking.

 

Call everything at the start of the page before any HTML. Instead of echoing the output straight away put it into a variable and then echo the variable later in the page where you want it to appear.

 

Without more commenting in your code as to what is going on and what is contained in the including files I could not say more.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

 Share

×
×
  • Create New...