Php

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.

Related Post

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

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories