..a dose of zero-day know-hows ..

4/16/2008

How to Make 'Read More' Links (A very simple sample implementation for PHP/MySQL)

Scenario: You have a php/mysql dynamically driven pages, you want a 'Read More' link displayed with your articles. This sample implementation might just give you the very basic analogy.

Method 1: For Articles stored on a single DB Field

A.) The DB Structure: (Use MySQL CLI or PHPMyAdmin to execute query)

CREATE DATABASE `test`;
CREATE TABLE `test`.`article_table` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`article` mediumtext NOT NULL,
`created` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)

B.) The DB Sample Content: (Use MySQL CLI or PHPMyAdmin to execute query)

INSERT INTO `test`.`article_table` VALUES
(1, 'Article 1', 'Its been a while since i posted an article, busy months have passed and
im still within timeframes and deadlines.. well most of the time real life intervenes eh?
anyways heres my article about Sharing Hashes between Mambo 4.x and SugarCRM
4.5.1 Using MySQL Views', '2008-01-01 12:00:00'),
(2,'Article 2', 'The "foo: bar: not found" error message does not indicate that bar could
not be found, but rather bar exists but is calling something that could not be found.
This is the case with Perl scripts when the script cannot find where Perl is.
I have noticed that most of out "out-of-the-box" Perl scripts point to "/usr/local/bin/perl"
whereas some of the OS pre-bundled Perl is preinstalled at "/bin/perl", hence, when you
run a Perl script, the "foo: bar: not found" pops out the terminal.', '2008-01-01 12:00:00')

C.) The Script to Display the Articles in Summary and Full View Depending on the Task. Save as readmore.php


Heres whats happening above:
  • All Articles Are displayed as Summary
  • We used Substring to show only first 60 characters, of course you can adjust this to any value, just hack the code.
  • Read More Link Per article will point to "readmore.php?task=Full&id={ID OF ARTICLE}"
  • Full View is Displayed
Important Hints:
  • The Full Article View needs the ID of the Article to Display
  • This makes it necessary for ReadMore link to specify the ID of the Article it is a part of

2 comments:

gedebux said...

hmm,,,
nice post boz !!!

i'm very helped...
thank's

Anonymous said...

hello, could explain me a bit more about the php code

where do i insert it on my html page, do i need to give a special name to my links...

you're also talking about task, what is it ? i'm sorry for being such a noob and maybe asking some stupids question