...

View Full Version : Help with basic navigation!



Jan
11-18-2003, 04:49 PM
Hey all!

I have this page with the following snippets of code:


<title><?php echo $page ?></title>

and


<body>
<?php include("$page/header.html"); ?>
<p>content</p>
<?php include("$page/footer.html"); ?>
</body>

Now, as the more experienced of you understand, the page title, header and footer (and why not even the content) is decided upon the address (ie. ?page=news).

Okay, instead of using this system, I'd want the title, header, content and footer to be fetched from a database. How would I do that?

The reason why is because I can't write a title that consists of several words, or contain non-english letters. Anyways, since this is for online newspaper, it would be more handy if the articles were stored in a database.

So, is there anyone who could help me with this? I would really appreciate it if anyone could guide me through this! Thanks!


~Jan

PrObLeM
11-18-2003, 05:25 PM
http://www.tutorialforums.com/showthread.php?s=&threadid=62850

that should help

Jan
11-19-2003, 05:00 PM
Thanks PrObLeM, but that doesen't seem to be what I'm looking for... :(

What I would want is a template, where the header and footer are linked from other files, and the main content changes upon the address (ie. index.php?article=031011). The articles actually wouldn't need to be in a SQL database; they could be in simple html/text files (just the text and some easy tags as <p>, <h1>, <h2> and so on.

So, if the visitor selects a link (let's say index.php?article=031011), nothing else on the page would change, except for the content (that would be taken from the external file (ie. articles/031011.html). I know there are ways of doing this where you need to define all the pages separately, but that's too much work, since this is going to be a online newspaper with lots of articles.

However, it would be cool if the articles could be divided into different categories by year, month and day, but that's not a must.

And, when the user would click on this link, the title would also change so that it would go from ie. "News" to "News | Bla bla bla". So in other words, I would need to be able to define the title of the page, and it would be nice if I could do that inside the external file. Is this possible, and if so, how could I do it? Please, I'd really need some help! I would be most grateful if someone could post some PHP code for me, and try to explain it to me!

I'm desparate! :(


~Jan

Edit: And yes, I would want to add one more thing: There should be a default source page in the main content area if someone only types index.php (well, quite naturally, since that's where the "headlines" would be, if it's a news page that is). If the user however tries to access a page that doesen't exist, there should also be a 404 page.

missing-score
11-20-2003, 12:38 PM
ive been here before: http://www.codingforums.com/showthread.php?s=&threadid=29171

Jan
11-20-2003, 01:43 PM
missing-score: yes, I'm sorry if this is a double post, but I tried to delete this one and re-post it, with a more clear description of what I need. Sorry for this!


~Jan

Jan
11-20-2003, 04:33 PM
Okay, since the new post was deleted - which a completly understand - I will have to refrase myself. And to missing-score: yes, I have permission to put five MySQL databases on my account. I would, however, prefer external files, but if you find this not such a good idea, please let me know!

So, to clear things up, this is what I'd need for my PHP template:


Header, footer and any other extras should be included from external files.
The main content area should change upon the address. Practically, what I mean is, that if you would write index.php?article=031120 in the address bar, the main content area would reload with the article (from eg. articles/031120.php). I know this can be done by predefining every single page in index.php, but that would take too much time, since there would be so many pages.
In the article file (eg. 031120.php), I should be able to define the title, so that it would appear in the browsers title bar.
When only index.php (without eg. ?article=xxxxxx) is entered, there should be a default page.
If the visitor tries to access an article that doesen't exist, or have typed the name wrong, a 404 page should appear instead.


So this is everything I would need in a nutshell. I really hope there's someone out there who could help me out, 'cause I've searched everywhere on the Internet without any results.


~Jan

missing-score
11-20-2003, 06:11 PM
Great.

There is nothing wrong with using external files, but a mysql database will speed up selecting the external file.

ok, lets make an example, here is a simple database plan. Very little data input. Run this query in PhpMyAdmin or something similar. (You will need a database to put this table in)

CREATE TABLE `pages` (

`Id` BIGINT NOT NULL auto_increment,
`PageFile` VARCHAR(128) NOT NULL
UNIQUE KEY(`Id`)

)


ok, lets assume we have some information in our database

Id | PageFile

1 article1.html
2 article2.html
3 article3.html


so, 3 rows, 3 articles... lets write a small piece of code to find the required article and print it to the page...





$dbcom = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename", $dbcom);

function load_page(){

if(isset($_GET['article']) && strlen(trim($_GET['article'])) != 0){

$find_article = "
SELECT PageFile
FROM pages
WHERE Id = '".$_GET['article']."'
";

if($article = mysql_query($find_article)){

if(mysql_num_rows($article) == 1){

$article_data = mysql_fetch_row($article);
include('/articles/'.$article_data[0]);
return true;

} else {

return '404';

}

} else {

return '500';

}

} else {

return '200';

}

}

$page = load_page();
if($page !== true){

if($page == '404'){

include('notfound.html');

} elseif($page == '500'){

include('mysqlerror.html');

} else {

include('defualtpage.html');

}

}



Note that the code is untested, but should work.

The load_page() functions checks the article information (page.php?article=2) and attempts to load the article. If the article is not found, it returns '404', which is the notfound error. If mysql query fails, it returns '500' which is internal server error, and returns '200' if there is no article specified, meaning 'ok'.

The numbers are used merely to relate it more to the general HTTP error messages.

After the function is finished, the function is called and the return value is tested. If it is not true, then it checks for any one of the error numbers and displays the page respectively.

Jan
11-20-2003, 06:34 PM
Thanks missing-score! You're a saviour! But, one question though: how would I do this without the database? Is it much trouble for you if you could show me that also? I'd appreciate it!


~Jan

missing-score
11-20-2003, 06:39 PM
if you can garuntee that the article id will always be the same as the filename of the article, then you could very simply do this:

if(file_exists($_GET['article'].".php")){

include("/articles/".$_GET['article'].".php");

} else {

// work out errors

}

Jan
11-20-2003, 06:48 PM
Yes, the article id will always be the same as the filename. But, what would I need for the visitor to get an error message if he/she tries to acces a page that doesen't exist? And how about the default page?

I hope I'm not making too much of a fuzz about this, but I really need your help, since you clearly know these kinds of stuff! Also, would it in any way be possible to define the title of the external article (in the article itself), so that it would appear in the title bar (eg. My website > Articles > Bla bla bla bla...)?

Thanks again for your priceless help!


~Jan

missing-score
11-20-2003, 07:06 PM
Ok, error messages :)



if(isset($_GET['article']) && strlen(trim($_GET['article'])) != 0){

if(file_exists($_GET['article'].".php")){

include("/articles/".$_GET['article'].".php");

} else {

include("404error.php");

}

} else {

include("defaultpage.php");

}


Before knowing how your articles are formatted (where the title is ect) i cant say how you could show the title. In a database the title could be stored in another field, but as you wish to use flat-file, could i see what the article pages will look like?

Jan
11-20-2003, 07:12 PM
Thanks for that missing-score! The external files wouldn't be anything special, raw text mainly but with some tags like <p>'s <h1...2...3>'s and stuff like that. The files would in other words just contain the article itself, and I would then format them through CSS. Example:

<h1>Header</h1>

<p>First paragraph</p>

<p>Second paragraph</p>

and so on...

Thanks, thanks, thanks for everything!


~Jan

missing-score
11-20-2003, 07:34 PM
let me re-phrase that

where is the article title stored. Is it in the article included file?

Jan
11-20-2003, 07:36 PM
Yes, the title should be IN the external file.


~Jan

Jan
11-20-2003, 08:07 PM
BTW, I got this message from the code you gave me:

Warning: main(/articles/031120.php): failed to open stream: No such file or directory in /home/jan/public_html/test/index.php on line 18

Warning: main(): Failed opening '/articles/031120.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/jan/public_html/test/index.php on line 18

Here's the index.php code:



<?

if (isset($_GET['article']) && strlen(trim($_GET['article'])) != 0){

if (file_exists($_GET['article'].".php")){

include ("/articles/".$_GET['article'].".php");

} else {

include ("404error.php");

}

} else {

include ("defaultpage.php");

}

?>


The default and 404 pages work great. But there seems to be a problem finding the article, even though it is in the articles -folder. Do you know what's wrong?


~Jan

PS. I need to go now, but I hope you can answer my questions when you have time! Thanks again for all your help!

missing-score
11-20-2003, 09:12 PM
try using:

include ("./articles/".$_GET['article'].".php");

Jan
11-21-2003, 11:50 AM
missing-score: I got it all to work by using this code:



<?php

if (isset($_GET['article']) && strlen(trim($_GET['article'])) != 0){
if (file_exists("articles/".$_GET['article'].".php")){
include ("articles/".$_GET['article'].".php");
} else {
include ("404error.php");
}
} else {
include ("defaultpage.php");
}

?>


Otherwise, it assumed there should be two identical files (or atleast with the same name) both in the root directory and in the articles directory. Weird huh? If you find anything that's totally off in this code, pleas let me know!

About the titles: I'd there is some kind of way of defining the title that will appear in the title bar of the browser in the external file. By putting some code snippet to the file, the browser should be able to collect that information from the file, and then display it. I don't know if I'm right here, but, perhaps that should help? So, I'd really appreciate it if you could help me with that dilemma still? Thanks again!


~Jan

missing-score
11-21-2003, 12:29 PM
Oh :o , i see i missed a directory in the file_exists() part. :p

What you are saying could be done, but i think the easiest way if you want the title to be displayed in the browser title bar, is to do this:

<!DOCTYPE...>
<html>
<head>
<style... <!-- styles ect -->

and then, without closing the <head> tag, use the code to find the article, and make the article start with:

<title>Page title</title>
</head>
<body...

Jan
11-21-2003, 01:20 PM
Okay, cool that we got the code to work! Once again about the title: the solution you gave me would work fine, but the fact is, that I will be including other parts to the page (header, footer...). You don't happen to know how to do it the other way? Anyways, I'm really glad you have helped me with this issue!


~Jan

missing-score
11-21-2003, 02:25 PM
try this, its a little more complex, but should get the job done...

First make sure your include files look something like this, eg:



article1.php
---------------

<?php
$title = "Article 1";
?>
<h1>Article 1</h1>
<p>This is the text for article 1</p>

----------------




<?php

ob_start(); // start output buffer

if (isset($_GET['article']) && strlen(trim($_GET['article'])) != 0){
if (file_exists("articles/".$_GET['article'].".php")){
include ("articles/".$_GET['article'].".php");
} else {
include ("404error.php");
}
} else {
include ("defaultpage.php");
}

$body_data = ob_get_contents();

ob_end_clean();

?>


use the above code before the <html> tag.

then, in your header/footer templates you can use the variable $title to get the page title, as defined in the article include file.

Use:

<?php echo $body_data; ?> where you want the included article to be.


the ob_start(), ob_get_contents() and ob_end_clean(); are output buffer functions. Any output (include, echo ect) called between ob_start() and ob_end_clean() will be stored rather than printed to the page. ob_get_contents() gets everything that has been stored as output in the output buffer. variables set inside the output buffer remain set outside of it.

Jan
11-21-2003, 03:23 PM
Thanks missin-score! I made another version of the code you gave me, and I wanted you to take a look at it:



<?php

ob_start(); // start output buffer

if (isset($_GET['article']) && strlen(trim($_GET['article'])) != 0){
if (file_exists("articles/".$_GET['article'].".php")){
include ("articles/".$_GET['article'].".php");
} else {
include ("404error.php");
}
} else {
include ("defaultpage.php");
}

$body_data = ob_get_contents();

ob_end_clean();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<title>Test | Articles | <?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<?php include("header.php") ?>
<?php echo $body_data; ?>
<?php include("footer.php") ?>

</body>
</html>


Is this okay? Anyways it works just exactly how I wanted it to! :) Thanks a million!


~Jan

missing-score
11-21-2003, 03:30 PM
it seems you got the right idea :), thats how i intended it to be used. glad you got it working.

Jan
11-21-2003, 03:33 PM
Thanks again man! One question though: does it make any real diffrence where the PHP code is in the document? Just wondering... :D


~Jan

missing-score
11-21-2003, 03:36 PM
yes and no... you cannot use the $title variable anywhere else until you have called the first bit of code (the ob_start() ect.) becuase until that code has been ran, the $title var is not set.

Jan
11-21-2003, 03:40 PM
missing-score: okay, I think I understand. Well, thanks for all your help, and hope I don't have to bother you anymore ;)


~Jan

missing-score
11-21-2003, 03:50 PM
im not bothered :), im happy to help out anyone who needs help with php ect. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum