SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Templates in PHP

    What are some different but effective ways of using template in PHP, so if i want to change a piece of the layout, i will not have to go to every page and do it. What has been found as the best way to do that.?

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would use MySQL to do it but if you don't have access to it try cutting the page into sections and save them as includes
    PHP Code:
    <html>
      <head></head>
      <body>
        <? include("menu.inc"); ?>
        <? inlcude("content1.inc"); ?>
      </body>
    </html>
    That would work so all you have to do is edit menu.inc to update the menu on every page.
    - the lid is off the maple syrup again!

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do have access to using MySQL, its accually part of my site. So how would i use that doing it?

  4. #4
    SitePoint Addict IsaacV's Avatar
    Join Date
    Jan 2002
    Location
    Saint Petersburg
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would you do it in MySQL?
    Pants.
    Join the NYFlava Fashions Contest

  5. #5
    SitePoint Enthusiast Monkeyboy's Avatar
    Join Date
    May 2001
    Location
    England, UK
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what notepad_coder means is that you have all your content located in the MySQL database, then a single PHP page, for example...

    article.php

    So, if you've sorted everything correctly in your MySQL database and you wanted article number one, it would be article.php?id=1

    Article 2 would be article.php?id=2

    The template would in fact be article.php. So you would just change that.

    I'm pretty much a beginner, and while I understand the theory pretty much, I'm still learning the practical side.
    Monkeyboy

  6. #6
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have mine set up so it lists the 4 most recent sections i make
    PHP Code:
    $c mysql_connect("localhost""root""");
    mysql_select_db("database"$c);
    $i mysql_query("INSERT INTO table (date, name, article) VALUES ('$date', '$name', '$article'") or die(mysql_error());
    mysql_close($c);

    // and then to output the 4 most recent sections

    $c mysql_connect("localhost""root""");
    mysql_select_db("database"$c);
    $s mysql_query("SELECT * FROM table ORDER BY date DESC LIMIT 4") or die(mysql_error());
    while(
    $r mysql_fetch_object($s)) {
        echo 
    $r->date"<br>";
        echo 
    $r->name"<br>";
        echo 
    "<p style=\"text-indent:30px;\">";
        echo 
    $r->article;
        echo 
    "</p>";
    }
    mysql_close($c); 
    And that shouls work. If you are using phpMyAdmin you can update it from there and you wouldn't need to use the insert part, or you can make a form to do it. You can have it so it shows a certian table like how Monkeyboy showed with Article 2 would be article.php?id=2. Just set up your code to work with the page.
    Last edited by notepad_coder; Apr 29, 2002 at 12:55.
    - the lid is off the maple syrup again!

  7. #7
    SitePoint Zealot Run2Web's Avatar
    Join Date
    Apr 2002
    Location
    Indiana
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is probably a really dumb question to this topic...

    Can the connection be done in an include file as well?

    like this...
    PHP Code:
    $connection mysql_connect("localhost""root""");
    mysql_select_db("db", $$connection); 
    saved as connect.inc?
    RUN2Web

    Has Anyone seen my Furry Little Mind?

  8. #8
    SitePoint Enthusiast Monkeyboy's Avatar
    Join Date
    May 2001
    Location
    England, UK
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Run2Web, yeah, you can do that. It's also a good idea to place the connect.inc file outside of your public_html directory, if your webhost allows it.

    Personally, I would rename connect.inc to connect.php. There's a reason for it which I can't quite remember, but I believe it's a security issue, in the event of PHP failing on your webhost.
    Monkeyboy

  9. #9
    SitePoint Zealot Run2Web's Avatar
    Join Date
    Apr 2002
    Location
    Indiana
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I would rename connect.inc to connect.php. There's a reason for it which I can't quite remember, but I believe it's a security issue, in the event of PHP failing on your webhost. [/B]
    Yes the reason is because inc files can be read and php files give html output.

    Thanks for the reply. I think I will start doing that, will ease that much anyway. Thanks!
    RUN2Web

    Has Anyone seen my Furry Little Mind?

  10. #10
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have a file containing an username and password you should save it as *.php because *.inc can be viewd in a browser just like a text file. I usually make a file and make it a function and and for every page I put connect("localhost", "root", "", "database");
    Last edited by notepad_coder; Apr 29, 2002 at 13:44.
    - the lid is off the maple syrup again!

  11. #11
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like how your first suggestion worked, but another question. how would i unclude my *.css file now, so it will do its job.? I tried the include function, but it just showed it. and i tried to put it in through HTML. but it didnt go eitther, so how do i do it in PHP?

  12. #12
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just include it as a normail stylesheet
    Code:
    <head><link rel="stylesheet" type="text/css" href="path_to_stylesheet.css"></head>
    And that should work.
    - the lid is off the maple syrup again!

  13. #13
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I had that, but i didnt seem to work.

  14. #14
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It shouls work. If it doesn't the path isn't right.

    What is the code you are using?
    - the lid is off the maple syrup again!

  15. #15
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <html>

    <head>
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>LynchburgEats.com</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
    <? include("head.inc"); ?>
        
    </body>
    </html>

  16. #16
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That should work then, unless head.inc has it also.
    - the lid is off the maple syrup again!

  17. #17
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what i have for head.inc

    PHP Code:
    <html>

    <
    head>
    <
    meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <
    meta name="ProgId" content="FrontPage.Editor.Document">
    <
    meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <
    title>LynchburgEats.com</title>
    <
    link rel="stylesheet" type="text/css" href="style.css">
    </
    head>
    <
    body>
    <
    table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
      <
    tr>
        <
    td width="700" colspan="2" background="bgline.gif">
        <
    img border="0" src="head.gif" width="698" height="91"></td>
      </
    tr>
      <
    tr>
        <
    td width="700" colspan="2" background="space.gif">
        <
    a href="fine.html">
        <
    img border="0" src="dining.gif" width="104" height="27"></a><a href="fast.html"><img border="0" src="fastfood.gif" width="97" height="27"></a><a href="pizza.html"><img border="0" src="pizza.gif" width="64" height="27"></a><a href="internat.html"><img border="0" src="international.gif" width="118" height="27"></a><a href="seafood.html"><img border="0" src="seafood.gif" width="85" height="27"></a><a href="cafe.html"><img border="0" src="cafe.gif" width="59" height="27"></a><a href="subs.html"></a>
    </
    body>
    </
    html

  18. #18
    SitePoint Addict IsaacV's Avatar
    Join Date
    Jan 2002
    Location
    Saint Petersburg
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I meant how could I store my HTML design elements into the database so I can edit the HTML and design of the page from the admin area I designed?
    How would I design a class to retrieve the different areas of the design?
    Pants.
    Join the NYFlava Fashions Contest

  19. #19
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have the exact same thing in header.inc that you do on the page you are including it on whay no take out all the HTML or take out include("header.inc"); ?

    I wrote a little thing on this - http://www.nathantech.com/tutorials.php?id=1 - all you would have to do is change the fopen() and stuff with MySQL to select and update the data.
    - the lid is off the maple syrup again!

  20. #20
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why should you rediscover wheel?
    There are cpl of templating libraries for php. Check out "Smarty", it is a joy (for me at least).

  21. #21
    SitePoint Enthusiast
    Join Date
    Jan 2002
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried a ocuple of those template engines, but i get errors I think I am just cursed


Bookmarks

Posting Permissions

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