Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Please help me create a PHP template!

    Hey all!

    I have this page with the following snippets of code:

    PHP Code:
    <title><?php echo $page ?></title>
    and

    PHP Code:
    <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
    Last edited by Jan; 11-20-2003 at 04:34 PM.

  • #2
    New to the CF scene
    Join Date
    Oct 2003
    Location
    Mesa, AZ
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #3
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Jan; 11-19-2003 at 05:07 PM.

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #5
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...


    PHP Code:

    $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.

  • #8
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    }

  • #10
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #11
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, error messages

    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");


    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?

  • #12
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    let me re-phrase that

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

  • #14
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, the title should be IN the external file.


    ~Jan

  • #15
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    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!
    Last edited by Jan; 11-20-2003 at 08:10 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •