473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

ilya Kraft
134 100+
Hello,

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, 7669 views)
May 2 '11 #1
11 32800
Rabbit
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"];
  3.  
  4. $textdisplaylist = '<div>'.$text.'<br><a href="#">Read More</a></div>';
  5.  
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
daonho
18
if(strlen($text) > 200)
{
$textdisplaylist = '<div>'.substr($text,0,200).'<br><a href="#">Read More</a></div>';
}
else
{
//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

http://bytes.com/attachments/attachm...ak_example.jpg
May 2 '11 #5
Rabbit
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
Rabbit
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>
  6.  
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(){
  2.  
  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";
  9.  
  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');
  12.  
  13. // The section added to the bottom of the "more-less" div
  14. $(".more-less").append('
  15. […]
  16.  
  17. ');
  18. // Set the "More" text
  19. $("a.adjust").text(moreText);
  20.  
  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. });
  32.  
May 3 '11 #9
$sql_text = mysql_query("SELECT * FROM memberPosts WHERE id='$id'");
$row=mysql_fetch_array($sql)
$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">
  2.  
  3. $(document).ready(function(){
  4.  
  5.     $('#read').click(function() {
  6.         $('#req').show()
  7.         $('#read').hide()    
  8.  
  9.  
  10.     })
  11.     $('#unread').click(function() {
  12.         $('#req').hide();
  13.         $('#read').show();
  14.  
  15.     })
  16.  
  17. });
  18.  
  19.  
  20. </script>
  21.  
  22.  
  23.  
  24. //html code
  25.  
  26.  
  27.  
  28.  
  29. <div class="text-block">
  30.                             <p>content......prewious <a href="javascript:void(0)" id="read" onClick="showDiv();">Read More &gt;</a></p>
  31.  
  32. <p style="display:none;" id="req">content all.<br> 
  33.  
  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

2
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 depends on the order of rows and some other...
7
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 is one that i want to change the data in and it...
3
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 like to put up a "Look, but don't touch" DB for...
3
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 form incorporates information from several tables....
3
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 Read only. That is, I can view all the records...
3
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 for me for an object created by a c extension.
2
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 2003, (the other user still has 2000) we cannot...
1
by: ascll | last post by:
Hi, Do you guy know how to make a ASP.net 2.0 checkbox "read-only"? Thanks.
0
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 this is to avoid deleting a directory accidently.. ...
1
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. There has to be another way to make the whole...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.