Summarize dynamic text in PHP with jquery

Asked

Viewed 191 times

3

I managed to do the function of See More, with a code here from Stackoverflow, but the texts come from the dynamic database, or whatever I do to get each text?

Code in php:

< div class="box-body" id="texto">
    <?php
        $texto = htmlentities($row['texto']);
        $texto = preg_replace('/[\n\r]{1,}/',"\n\n",$texto);
        echo nl2br(emoticons($texto)); 
    ?>
</ div>

Code in Jquery:

var wordLimit = 50;
$(function() {
  //trata o conteúdo na inicialização da página
  $('#texto').each(function() {
    var post = $(this);
    var text = post.text();
    //encontra palavra limite
    var re = /[\s]+/gm, results = null, count = 0;
    while ((results = re.exec(text)) !== null && ++count < wordLimit) { }
    //resume o texto e coloca o link
    if (results !== null && count >= wordLimit) {
      var summary = text.substring(0, re.lastIndex - results[0].length);
      post.text(summary + '...');
      post.data('original-text', text);
      post.append('<br/><a href="#" class="read-more">Leia mais</a>');
    }
  });

  //ao clicar num link "Leia mais", mostra o conteúdo original
  $('.read-more').on('click', function() {
    var post = $(this).closest('#texto');
    var text = post.data('original-text');
    post.text(text);
  });

}); 
  • Florida, can you help me?

  • Sorry, I can’t, but if you wait a little while for someone with more experience than I might help you, as it’s the weekend, maybe they’re not around. Remember, the more details you give about what you’re doing and how you want it to look, the easier it will be for someone interested to understand and help you.

  • @Florida, look at hard, it’s the third post I do here and only in vacuum :\

  • We can’t force anyone to answer anything. While someone doesn’t answer, you try to solve it, and if you do, you can answer your own question.

  • A question, what do you mean by dynamic texts? It means that this text may be of varying size or that there may be entries in the table where Voce saves this text and that they should be pulled simultaneously?

  • That will be several texts coming from the database. I even managed to put a different id for each DIV, but then I would have to put each script for each div, it would look terrible, I wanted only one for all.

  • 1

    You cannot have the same id on more than one element. Use a class and swap #texto for .sua-classe in jquery.

  • But there will be several Ivs with the same class for example

  • < div class="hi">text 1</ div> < div class="hi">text 2</ div>

  • And so it doesn’t work :\

  • I got it, thank you very much, that’s right, I don’t know why

  • @Viniciushenzel now you can answer your own question if you want, will help others in the future. :)

  • 1

    Thank you @Florida

  • Just an addendum about having several elements with the same class, well, that’s the function of the class, grouping similar elements that are the same, you see, class...haha

Show 9 more comments

1 answer

1

Whenever I have to do it, I do it that way, which I find simpler:

$(function(){
  // escondo todos os textos completos
  $('.more').hide();
  
  // quando clicar em ver mais
  $('.see').click(function(){
    // pego o ID
    var id = $(this).prop('id');
    // mostro a publicação completa do post
    $('section#p'+id+' > .more').show('fast');
    // oculto o resumo daquele post
    $('section#p'+id+' > .abstract').hide('fast');
  });
  
  // quando clicar em ver menos
  $('.hide').click(function(){
    // pego o ID
    var id = $(this).prop('id');
    // oculto a publicação completa do post
    $('section#p'+id+' > .more').hide('fast');
    // mostro o resumo daquele post
    $('section#p'+id+' > .abstract').show('fast');
  });
});
.post { 
  border: 1px solid #000;
  padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<section class="post" id="p1">
  <div class="abstract">
    Texto resumido pelo php est...
    <a href="#" class="see" id="1" >Ver Mais</a>
  </div>

  <div class="more">
    <p>Texto resumido pelo php está completo agora</p>
    <a href="#" class="hide" id="1" >Ver Menos</a>
  </div>
</section>

<section class="post" id="p4">
  <div class="abstract">
    Mais um post com descrição ...
    <a href="#" class="see" id="4" >Ver Mais</a>
  </div>

  <div class="more">
    <p>Mais um post com descrição resumida com PHP que agora 
    está sendo exibido inteiro</p>
    <a href="#" class="hide" id="4" >Ver Menos</a>
  </div>
</section>

  • very good, it is much better like this in PHP, because it does not take long to load

Browser other questions tagged

You are not signed in. Login or sign up in order to post.