Create A Dynamic ‘Read More’ Link Using PHP

Today I am working to create dashboard like wordpress, means there are number of stories and each story has too much description. I have a problem how to avoid lengthy description on dashboard. I have seen many website if the description text is larger than few characters, the extra words are hide and a show more link is appeared to user.
So I need to create “Read More…” link and if the users is interested then he can click on more link and see the full content.


You can also check other tutorial of dynamic read more Using jQuery

Here is a simple tutorial to achieve “Read More..” link functionality.

To create link dynamically i need first to create connection with database and get all records with description from database.

Select Code
1
2
3
4
5
6
7
8
9
<?php  
include_once "connect.php";  
$sql = "SELECT * FROM stories";  
$result = mysql_query($sql);  
while($row = mysql_fetch_array($result))  
{  
echo "n";  
echo readMoreFunction($row['story_desc'],"story.php","story_id",$row['story_id']);  
}

Now i am creating a readMoreFunction(...) general function to display link on dashboard.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php 
/*********************************************************************
	 Purpose			: function to truncate text and show read more links.
	 Parameters		    : @$story_desc : story description
	 @$link : story link
	 @$targetFile : target redirect file name
	 @$id : story id
	 Returns			: string
	 ***********************************************************************/
function readMoreFunction($story_desc,$link,$targetFile,$id) {  
//Number of characters to show  
$chars = 25;  
$story_desc = substr($story_desc,0,$chars);  
$story_desc = substr($story_desc,0,strrpos($story_desc,' '));  
$story_desc = $story_desc." <a href='$link?$targetFile=$id'>Read More...</a>";  
return $story_desc;  
}  
?>
Above functionally has been created using of core php ,you can also create this functionality with jquery and PHP

.

I hope this will help!

Demo and Download source Code Of Read More Link Using PHP

  • Rajesh Namase

    This is what I really want, I was looking for such method from long time, thanks for sharing this method to create read more link dynamically.

  • umer dar

    very nice brother. I learnt a lot

  • budi

    nice gan
    thanks for sharing

  • Atrugeek1977

    Hi,

    Looks like great code! how do i intergrate it with my code! Currently, my code is

    get_description($properties)); ?>

    An it shows too much description.

    Thank you

    • You need to call readMoreFunction() function on each description object/array.