View Full Version : Displaying Articles on a Single Dynamic Page

09-12-2005, 12:34 PM
Ok, here is what i want to do. Im making a pc news website right, and i've set up the index to show the header (a brief description of the article) of the last 6 articles and that works fine. Now, what i want is this. I want 1 page, that displays 1 article at a time. So, at the end of my headers, i put a (read more) link (like so http://www.siliconjunkie.com.au). I want the (read more) link to link to the article (displaying the whole body of the article aswell. I want the article be displayed on a page which i;ve named article.php. Now how do i do this. I know that the article id or soemthing will probably be in the url. So, kind of what anantech does (excpet they dotn ahve a read more link, they just use the heading as a link.

Here's a link to 2 pages that you can look at and manipulate to make this work.


Anyhelp will be greatly appreciated...


09-12-2005, 02:58 PM
You are grabbing that from mysql? If so, here's a basic retrieval method that i use to get a certain amount of text, and then show a read more... link that links to the article. If you don't have these rows, make something like them (id is a mediumint( 8 ) UNSIGNED NOT NULL AUTO_INCREMENT; date is a DATE NOT NULL, pulled out with a specified format with DATE FORMAT). You can use the code below if you want and make any changes necessary to make it work for you!

//again feel free to edit and have fun with this code all you like to make it work for you!
$id = $HTTP_GET_VARS['id'];
if(!isset($id) || empty($id))
$id = mysql_real_escape_string($id);
$sql = 'SELECT id,title,MID(content,1,50) AS content,DATE_FORMAT(date, \'%d/%m/%Y\') AS date FROM news ORDER BY id DESC LIMIT 6';
$result = mysql_query($sql) or die('Could not gather news items from database');
while($row = mysql_fetch_object($result))
echo '<fieldset><legend>' . $row->title . ' - ' . $row->date . '</legend>' . $row->content . '<BR /><a href="?id=' . $row->id . '">(full story)</a></fieldset>';
$id = mysql_real_escape_string($id);
$sql = 'SELECT id,title,content,DATE_FORMAT(date, \'%d/%m/%Y\') AS date FROM news WHERE id = \'' . $id . '\'';
$result = mysql_query($sql) or die('Could not gather single news item from database. Check id and try again.');
$row = mysql_fetch_object(result);
echo '<fieldset><legend>' . $row->title . ' - ' . $row->date . '</legend>' . $row->content . '</fieldset>';

NOTE: I haven't checked this on a server yet, but unless there's a typo in it, in theory, it should do what you need.

Of course, it wouldn't be too hard to show the (full story) link only if the article had less than fifty characters. Just make a strlen on $row->content, and if it is less than 50, make $content = $row->content . '<BR /><a herf="...">(read more)</a>'; and if not, then $content = $row->content; Then you just replace $row->content in the echo with $content and you're done!

Hope I helped!

09-12-2005, 03:02 PM
does that code go on the display page or the (read more) page?

09-12-2005, 03:07 PM
Could you (or someone else) possibly add that into my files (link in original post), that way i can learn and understand it alot better.


09-13-2005, 02:54 AM
Please, could someone please do that for me...

09-13-2005, 08:02 AM
You don't need a "read more" file... just use that one. It'll do the "read more" part if there's a value set for ?id= in the URI. ;)

I may be able to add it for you later, but I'm kinda busy now. ;)