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.?
| SitePoint Sponsor |
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.?
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
That would work so all you have to do is edit menu.inc to update the menu on every page.PHP Code:<html>
<head></head>
<body>
<? include("menu.inc"); ?>
<? inlcude("content1.inc"); ?>
</body>
</html>
- the lid is off the maple syrup again!
I do have access to using MySQL, its accually part of my site. So how would i use that doing it?


How would you do it in MySQL?
Pants.
Join the NYFlava Fashions Contest
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
I have mine set up so it lists the 4 most recent sections i make
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.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);
Last edited by notepad_coder; Apr 29, 2002 at 13:55.
- the lid is off the maple syrup again!

Here is probably a really dumb question to this topic...
Can the connection be done in an include file as well?
like this...
saved as connect.inc?PHP Code:$connection = mysql_connect("localhost", "root", "");
mysql_select_db("db", $$connection);
RUN2Web
Has Anyone seen my Furry Little Mind?
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

Yes the reason is because inc files can be read and php files give html output.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]
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?
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 14:44.
- the lid is off the maple syrup again!
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?
Just include it as a normail stylesheet
And that should work.Code:<head><link rel="stylesheet" type="text/css" href="path_to_stylesheet.css"></head>
- the lid is off the maple syrup again!
Yes, I had that, but i didnt seem to work.
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!
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>
That should work then, unless head.inc has it also.
- the lid is off the maple syrup again!
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>


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
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!




why should you rediscover wheel?
There are cpl of templating libraries for php. Check out "Smarty", it is a joy (for me at least).
I have tried a ocuple of those template engines, but i get errorsI think I am just cursed
Bookmarks