Page 1 of 1
more PHP help
Posted: October 15th, 2008, 0:10
by ProfHawking
As you might have guessed im working on a new php project.. something else has come up that im a bit unsure about.
I want to have a setting near the top of the page - the page title set by a php file that is included later.
for example if i had:
Code: Select all
<html>
<head>
<title><php></title>
</head>
<body>
<h1>some title</h1>
<php>
</body>
</html>
where page.php contains the setting:
$page_title = "This is the title";
and the rest of the page content in a mix of html and php.
is it possible to get php to "go back" and set an earlier variable as such?
Posted: October 15th, 2008, 0:23
by Stoat
You could be a good boy and get all your content ready before beginning the HTML, as per most template systems or frameworks.
Output buffering could help, but I've never used it.
If you've already written the HTML to the browser though there's nothing you can do about it with PHP.
Posted: October 15th, 2008, 0:31
by Fear
I don't use PHP, however I do use a technique in .Net where I hook into the PostRender event and tweak the output html. Is there not something like this in PHP you can use?
But I'd go with toast's suggestion of getting your ducks in a row before you render any html. Or write the page to a global variable and write that into the response stream at the very end - as you can do what you want with strings, but not streams.
Posted: October 15th, 2008, 0:56
by ProfHawking
ok fair doos. i have changed it so it is now something like this:
Code: Select all
(php
include(page.php);
php)
<head>
<title>(php echo "$title"; php)</title>
</head>
<body>
(php
$showcontent="yes";
include(page.php);
php)
</body>
</html>
------------------------------------------
page.php contains something like this:
------------------------------------------
(php
// query db etc for stuffs
$title = "blah blah from DB";
if($showcontent=="yes"){
php)
<h1> the bulk of the page</h1>
This is the content stuffs.
<p>
(php echo "with a bit of code here and there."; php)
etc etc goodbyes!
</p>
(php
// end show content
}
php)
This seems to work, although i am not liking the idea as it queries the database etc twice - once before the html starts and once during - before the content is pulled from the file. Thinking about it, i suppose i could use an IF(){}else{} in the page.php file to do it, as both sides will be run at one point, both before the content.
otherwise, i'm not sure how to do it - but i feel like there should be a neater way.
PS im changing <php> for (php php) as it all gets hidden otherwise...
Posted: October 15th, 2008, 1:00
by Fear
Why don't you have a single database include which goes and gets all the stuff you need in a single connection separate to all this. Then just include that at the top.
Posted: October 15th, 2008, 1:09
by ProfHawking
What it needs to retrieve will be dependant on what is in the included file that needs to be included later, and some of it may or may not be from a database, depending on the page in question - some will be static some from databases.
if that makes any sense huzzar - im not sure im 100%, my brain is melting a bit. perhaps a good sleep on it would be a good plan
Posted: October 15th, 2008, 1:19
by Fear
When you include a file in php it effectively just forms one big php file - so you can get everything you want the first time and store it in a variable for the second time, i guess.
I always find my most genius programming moments are when I'm nowhere near a computer. Taking a break is more productive than not.