I have a blog, In index page it's show all posts. I want a 'show more' button/text when my post message are 24 characters and if i click 'show more' button/text then it's should be show my full message to another page. My index page code are bellow which is get every post from the database:
<?php
include "db/db.php";
$upload_path = "user/manage/userpostimg";
$sql_2 = mysql_query("SELECT * FROM user_post ORDER BY post_id DESC");
while ($rel_2 = mysql_fetch_assoc($sql_2))
{
$idu = $rel_2['post_id'];
$subu = $rel_2['subject'];
$imgnameu = $rel_2['img_name'];
$imgu = $rel_2['image'];
$msgu = $rel_2['message'];
$dateu = $rel_2['date'];
$posteru = $rel_2['poster'];
$cat_nameu = $rel_2['cat_name'];
echo "$subu" . "<br/>";
echo '<img src="' . $upload_path . '/' . $imgnameu . '" width="75" /> ';
echo "$msgu" . "<br/>";
}
?>
How can i do this? Can you please tell me, that will better for me. Thanks in Advance.
You can use this function
<?php
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function truncateLongText ($string, $limit, $break=".", $pad="...") {
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit)
return $string;
// is $break present between $limit and the end of the string?
if ( false !== ($breakpoint = strpos($string, $break, $limit)) ) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
?>
With the function above you can truncate the message at 24th character and then add a "Read more..." link, pointing to the full message.
The easiest would be to use jquery .click().
Here's a plugin you could use to truncate it:
http://papermashup.com/truncate-text-with-the-jtruncate-jquery-plugin/
You would need to truncate your description at the desired 24 characters and then add the rest of the text into another div with a class name on it from there you can reference that in your jquery and show it. The biggest problem you'll run into is truncating the text at a word break unless you don't care where it's cut off.
Here's a reference for the juery: