/    Sign up×
Community /Pin to ProfileBookmark

Solving problem/ adding title/ need declare var before including

OK, currently I use generic titles on all of my pages, because I use includes. Now, however, I have a simple way that could mean having a title for each page which would be very good, especially for my potential google rating.

However, the way I have set it up means that the string used for the title will be retreived after the top of the page, top.php is included. I can think of a very simple solution, but it is very unefficient and impractical (reading the file into a string, then echoing the string later in the file rather than including the page.)

I’ve attached a text version of my page right now , with a few changes (my page works perfectly right now. You can view the .txt files, they’re in [url]http://projep.t35.com/article[/url] . I hope my code isn’t messy and unreadable.. if you need more source, I don’t exactly have cutting edge innovative functions, so I’ll gladly show you it.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@Ben_RogersauthorApr 02.2004 — Oops :o forgot to attach the file....

[upl-file uuid=856005eb-1685-485d-8385-b71a19833c58 size=2kB]indexphpfile.txt[/upl-file]
Copy linkTweet thisAlerts:
@SamApr 02.2004 — page not found, but [URL=http://ryanbrill.com/archives/including-content-with-php/]ryan beat you to it[/URL]
Copy linkTweet thisAlerts:
@JonaApr 02.2004 — [font=arial]Can't you just declare a variable and [b]then[/b] include your file?[/font]

[code=php]
global $pageTitle;
$pageTitle = "Index";
include("headers.php");
[/code]


[font=arial]And in your include file, just use:[/font]

[code=php]
<title><?php echo($pageTitle); ?></title>
[/code]


[font=arial]You might not even have to set it as a global variable--but it couldn't hurt.

[b]Edit: I didn't see Sam's post.[/b][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorApr 02.2004 — It's not that simple at all.

OK.. now text files are seperated into different folders based on subject. Within each folder, they title of the page is contained withing :::, so it'd be <h3>:::title:::</h3> or something like that. Now, I have a script for my navigation that I got off of php.net and heavily modified for my needs, to get the read the contents of the file (file_get_contents()), and explode it for :::, so that for example $foo = explode(":::",$content); then $foo[1] would be the title, and the script would echo out <a href="?nums-based-on-category=name-of-file">title of article</a>, so it's auto updating and all that. Now, I can easily get that title once the variable $file in the index.php is declared, but it'll be useless, because the page with the <title> will have already been included. Now, I need to be able to somehow declare the title before top.php is included in an efficient way. I'm just wondering if there's an easy way for it, otherwise I'll just set up a line of if's and else if's to detect isset, read the file, and get the title for top.php to use. All I want to know is if there's a better way.
Copy linkTweet thisAlerts:
@SamApr 02.2004 — I didn't see your attachment before I posted, I'd say that Jona's solution might work though...
Copy linkTweet thisAlerts:
@JonaApr 02.2004 — [code=php]
function check_file($file) {
if(file_exists($file))
{
// Title to use
global $pageTitle;

// Open file for reading
$fp = fopen($file, 'r');
// read contents
$getTitle = fread($fp, filesize($file));
// close file
fclose($fp);
// split file contents at <title>
$getTitle = explode("<title>", $getTitle);
// set $getTitle to everything that comes after <title>
$getTitle = $getTitle[1];
// split file contents at </title>
$getTitle = explode("</title>", $getTitle);
// set $pageTitle to everything that comes before </title>
$pageTitle = $getTitle[0];
include $file;
}
else
{
echo "Either I've moved the file, or it is non-existant. Sorry!";
}
}
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorApr 02.2004 — Much thanks, Jona ?! In perfect time, too, I redid the entire backside to be better documented, less repetitive, and more sensible. I put your code in a function, and it works perfectly. I'll have to remember those functions, setting global variables and especially fread it's better suited for the job than file_get_contents. Thanks again!
Copy linkTweet thisAlerts:
@JonaApr 02.2004 — [font=arial]Happy to help. ?[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Ben_RogersauthorApr 03.2004 — Good- if you didn't I'd still have a 'temporarily under construction sign up.
×

Success!

Help @Ben_Rogers spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...