Create A Dynamic ‘Read More’ Link Using PHP

I am working on a dashboard similar to WordPress today, therefore there are many stories and each story has an excessive amount of details. I’ll learn here how to add ‘Read more’ link on each story.

I’m having trouble figuring out how to keep the dashboard page’s descriptions short. A show more link will appear to the user on many websites where the further words are hidden if the description text is longer than a few characters.

So I need to create “Read More…” link and if the user is interested then he can click on more link and see the full content.

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

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

To create a link dynamically, I need first to create a connection with the database.

Created Database Connection With MySQLi

Created connect.php file into php application, We will pass MySQL server hostname, username, dbname and password into mysqli_connect() method.

$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

Get All Records With MySqli

We will get all records with description data from the database.

<?php  
include_once "connect.php";  
$sql = "SELECT * FROM stories";  
$result = mysqli_query($con, $sql);  
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))  
{  
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 the dashboard.

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

Demo and Download Source Code

7 thoughts on “Create A Dynamic ‘Read More’ Link Using PHP

  1. 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.

  2. 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

Leave a Reply

Your email address will not be published. Required fields are marked *