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
To create a link dynamically, I need first to create a connection with the database.
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(); }
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; } ?>
I hope this will help you!
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
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
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.
very nice brother. I learnt a lot
thanx bro
nice gan
thanks for sharing
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.
Thank you this code is working for me!