|
| Author Details |
|---|
| All Tutorials by phpdeveloper |
1
|
|---|
Simple News-Driven Site
Lern to make your very own news driven site.
A Simple News-Driven Site
by enygma
The goal of this tutorial, as you probably guessed by the title, is to help you (yes you) create your very own news-driven web site. Now, this doesn't have to be a system as glorified as something like Slashdot or some of the other big news sites. All you may need is a simple system for displaying news and for editing/adding news to the front page. Well, look no further - I will show you how to create a nice simple news-driven site using nothing but some pretty simple PHP and a MySQL database. Now, this can be done without a database by using a text file or something, but that's a bit more difficult. You'd have to write something to parse out the info you needed and something to put it in the right place when you were adding to it. For the sake of ease, we're going to use a simple MySQL database. I'm not going to get into the basics of setting up a database, though. I'm going to assume that you have a good grasp on this and that I can move on. Here's the database you need to create: CREATE TABLE news( author VARCHAR(20), date INT, content TEXT, ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID) ) If you make a database and put this table in it, that should be all that you need as far as the MySQL backend. This simple layout can be changed a bit depending on what you want to do with it, but this is usually a good basic structure. The author is, obviously, the person writing the news item and can be put in several ways. I have a setup on one of my sites that automagically puts the author in based on the username entered when you access one of the files behind the .htaccess protected directory. The date field is a very fun one for later on. It will help us to order the entries and make them a bit more orderly. The integer stored in this field will actually be a UNIX timestamp. PHP can work with these much more easily than some other strangely formatted numbers. Plus, it has the added benefit of being able to be evaluated with the normal math functions too. It would be much harder to find the time difference of something formatted like 00:00:00 versus a nice timestamp formatted with just numbers and no special characters. UNIX timestamps are done in seconds, so you can easily multiply it out and get the hours, days, weeks, etc for any comparison that you'd want to make. The content field allows us to put in a great deal of text. If you're just going to need it for short front page notes or something, you might consider lowering the requirements and using something like VARCHAR or the like instead of TEXT. That last part on the end there is simply an auto-increment field. I, by habit, stick one on every database that I make. It helps to keep things a bit more organized and helps you keep your sanity when you just need an easy field to key off of. Well, that should be the database setup - you really don't need much else on this end to get the backend going. Now, on to the actual PHP code. I'm going to give you an example and then explain it so that you can see the thoughts behind it: $db=mysql_connect("localhost","user","pass") or die ("cant connect"); mysql_select_db("database",$db) or die ("cant change"); $news=mysql_query("select * from news ORDER BY date DESC LIMIT 8") or die ("cant get em"); while($rows=mysql_fetch_array($news)){ $date=date("F d, Y H:i ",$rows["date"]); $author=$rows["author"]; $number="100%"; printf("n",$number); echo " $title n"; echo " $date
n"; echo " by $author
$content
n"; } We'll step through this code, giving a bit of explanation as we go along for each part. First off: $db=mysql_connect("localhost","user","pass") or die ("cant connect"); mysql_select_db("database",$db) or die ("cant change"); $news=mysql_query("select * from news ORDER BY date DESC LIMIT 8") or die ("cant get em"); This code is what's used to connect to the database, select it and send the query to the server. The query basically says to "select all of the matches from news, order them by the date field in descending order, but only get eight." Pretty simple, eh? ORDER BY can be a very powerfulthing. In fact, before I knew about it (back when even I was a newbie), I had to try to write my own function in PHP to compensate. Don't do that - it's not fun. Anyway, on to the next block of code: while($rows=mysql_fetch_array($news)){ $date=date("F d, Y H:i ",$rows["date"]); $author=$rows["author"]; $number="100%"; printf("n",$number); echo " $title n"; echo " $date
n"; echo " by $author
$content
n"; } Now, while this might look a little bit scary, it's really not. It's all encased inside a nice while loop that goes until it's gotten all of the info out of the "select" that we did earlier. Now, since we put the timestamp in the date field, we can pull it out and use the date() function in PHP to format it. In this case, "Month day, year hour:minutes" - there are several different ways to format all o
| | Hits:170 Rate: 0.0(out of 5) Vote:0 Submit Date :2006-03-18 Rate It | Error | Review | | | Category: Home > PHP > Content Management |
|
|
|
|