472,352 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

How to break text from database and have "Read More" button?

ilya Kraft
134 100+

I came over this problem.
I'll try to make it as simple as I can.
Say there is a page called submit-article.php where users enter their text in the field. It goes in database and so on...
Than I display it in div on index.php page. How can I limit text that displays on index.php page to 200 characters? And than have a "Read More button" < When clicked expands div to show all text.
I will attach image that shows what I mean.

Thank You
Attached Images
File Type: jpg Text_break_example.jpg (91.2 KB, 7560 views)
May 2 '11 #1
11 32180
12,516 Expert Mod 8TB
You could store it in a variable and then display only the first 200 characters. Then, when they click the button to see more, it just replaces what's in the div with the full text.
May 2 '11 #2
ilya Kraft
134 100+
Yeh, I kind of know the logic of it, my problem is in code. Here is what I have got so far:

Expand|Select|Wrap|Line Numbers
  1. $sql_text = mysql_query("SELECT * FROM memberPosts WHERE id='$id'");
  2. $text = $row["text_body"];
  4. $textdisplaylist = '<div>'.$text.'<br><a href="#">Read More</a></div>';
My question is, how to limit text to 200, than how to expan div when Read More is clicked, than How to insert full text instead of mini text. Quiet a lot of questions huh ;) But this are the parts I cant understand.

Thank You
May 2 '11 #3
if(strlen($text) > 200)
$textdisplaylist = '<div>'.substr($text,0,200).'<br><a href="#">Read More</a></div>';
//no point of having read more button here cuz there nothing more to read :)
$textdisplaylist = '<div>'.$text.'</div>';
May 2 '11 #4
ilya Kraft
134 100+
This shows how to limit it to 200 characters right?
What is that strlen function?
But this just limits text to 200 right?
What I meant was. Say I have 500 char text, I want to show first 200 characters of it on the first page, but when someone clicks Read More button it expands down and shows all text, like on this image here

May 2 '11 #5
12,516 Expert Mod 8TB
The strlen function tells you how many characters are in a string.

What you need to do is write it into the javascript of the page. You'll also need an onclick function on the read more that will a) put in the rest of the text and b) hide the "read more" or toggle it to "show less"
May 2 '11 #6
why are you processing it in php??
use Mysql function for that(I dont remember d function name but there is a one..guranteed..plz google it)
and when user click Read more, then retrieve d whole field data
May 2 '11 #7
12,516 Expert Mod 8TB
You can't just use JavaScript for it. At some point you're going to need a server side script to retrieve the actual value. Which is what they're doing.
May 3 '11 #8
ilya Kraft
134 100+
I found A solution for this.

First you want to setup the basic structure of your content so that you can enable this functionality.
Expand|Select|Wrap|Line Numbers
  1. <div class="more-less">
  2.     <div class="more-block">
  3.         <p>The Content</p>
  4.     </div>
  5. </div>
Anything that you place into the “more-block” div will be expandable. The “more-less” div is needed to hold the More/Less link and the [...], which is added using the jQuery, saving you the time of adding those small parts.

For explanation of the jQuery, view the comments throughout the code below.

Expand|Select|Wrap|Line Numbers
  1. $(function(){
  3. // The height of the content block when it's not expanded
  4. var adjustheight = 80;
  5. // The "more" link text
  6. var moreText = "+  More";
  7. // The "less" link text
  8. var lessText = "- Less";
  10. // Sets the .more-block div to the specified height and hides any content that overflows
  11. $(".more-less .more-block").css('height', adjustheight).css('overflow', 'hidden');
  13. // The section added to the bottom of the "more-less" div
  14. $(".more-less").append('
  15. […]
  17. ');
  18. // Set the "More" text
  19. $("a.adjust").text(moreText);
  21. $(".adjust").toggle(function() {
  22.         $(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
  23.         // Hide the [...] when expanded
  24.         $(this).parents("div:first").find("p.continued").css('display', 'none');
  25.         $(this).text(lessText);
  26.     }, function() {
  27.         $(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
  28.         $(this).parents("div:first").find("p.continued").css('display', 'block');
  29.         $(this).text(moreText);
  30. });
  31. });
May 3 '11 #9
$sql_text = mysql_query("SELECT * FROM memberPosts WHERE id='$id'");
$text = $row["text_body"];
<?php echo substr($text,0,200);?><a href="#"?id=<?php echo $row['id']?>>Read More</a>

check this one
Mar 15 '12 #10
@ilya Kraft.......If you use javascript to show more or less text, then that will be time consuming. Bcz with javascript solution you are rendering the whole document.

As I told in one of my earlier posts, why dont use use MySql string function to retreive only 200 or 300 characters? When user clicks "Read More", then retreive the whole document.
Apr 7 '12 #11
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  3. $(document).ready(function(){
  5.     $('#read').click(function() {
  6.         $('#req').show()
  7.         $('#read').hide()    
  10.     })
  11.     $('#unread').click(function() {
  12.         $('#req').hide();
  13.         $('#read').show();
  15.     })
  17. });
  20. </script>
  24. //html code
  29. <div class="text-block">
  30.                             <p>content......prewious <a href="javascript:void(0)" id="read" onClick="showDiv();">Read More &gt;</a></p>
  32. <p style="display:none;" id="req">content all.<br> 
  34. <a href="javascript:void(0)" id="unread" onClick="showDiv();">Close&gt;</a>
  35.  </p>
  36.                         </div>
Feb 26 '14 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

by: php newbie | last post by:
I am trying to write a cursor to update certain rows in a particular order as follows: (I need the cursor version, not SQL, as the update logic...
by: Rich | last post by:
Hi, I'm having problems with changing the Read Only properties. Running Winxp and i cannot get the "read only" to clear off a folder. The folder...
by: PeteCresswell | last post by:
I guess the good-right-and-holy path is a Read-Only user ID in the backend DB...but that's not in the cards right here right now. Meanwhile, I'd...
by: Vic | last post by:
Dear All, I have a database of laboratory records in Access 2000. There is one form which acts as an interface to input experimetal data. This...
by: Stinky Pete | last post by:
I've mananged to get Access Run-time working on a Win 98 PC. I was stoked at that achievement, however I'm now stuck in that the files are opening...
by: Johannes Zellner | last post by:
Hi, can I make an object read-only, so that x = new_value fails (and x keeps it's orginal value)? This would be especially of interest...
by: JanP | last post by:
Hello I'm new here and hope someone can help solve this problem. I built a database in MS Access 2000 and split it. Since I was upgraded to...
by: ascll | last post by:
Hi, Do you guy know how to make a ASP.net 2.0 checkbox "read-only"? Thanks.
by: libish | last post by:
hi all, can any one help me in converting a directory to read only format. and also making a read only directory/file to read/write directory ...
by: UDontKnowJack | last post by:
Hey! I would like to make a copy of my database a "Read only" version. Without going into every form/table and at properties make it a read only....
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.