0

I'm making a comment section in my site and I have this problem, everything goes fine in the php code but it keeps increasing the height of the td, I've tried to set a max-height but didn't worked, I thought about making that it only echoes the ten last comments but i don't have any idea how to do this. This wont be a professional site, is just an activity from high school.

<tr ><td colspan="4"> 

    <?php
      $conexao = mysqli_connect("127.0.0.1","root","","db_tcc") or die ("Não foi possível se conectar com o servidor.");
      $varsql = "SELECT * FROM tb_comentarios WHERE estado='pe' ORDER BY id DESC";
      $carregar_comentarios = mysqli_query($conexao, $varsql);
      while ($linha = mysqli_fetch_array($carregar_comentarios))
      {
        $user = $linha["user"];
        $texto = $linha["texto"];
        $data = $linha["data"];
        echo "$user: $texto - $data<BR>";
      }
    ?>

  </td></tr>
  • The reason why the td's height keeps increasing is because the while loop is echoing inside the same td as many times are the results are returned. Return **$user: $text - $data** in it's own td. Remove the opening and closing td class, then do this instead: **echo "$user: $texto - $data
    ";** Boa sorte.
    – Stark Nov 03 '17 at 21:24

2 Answers2

0

I thought about making that it only echoes the ten last comments but i don't have any idea how to do this

To limit the result set of a mysql query, you must add a LIMIT clause:

$varsql = "SELECT * FROM tb_comentarios WHERE estado='pe' ORDER BY id DESC LIMIT 10";

the_nuts
  • 5,634
  • 1
  • 36
  • 68
0

Try this.

<?php
  $conexao = mysqli_connect("127.0.0.1","root","","db_tcc") or die ("Não foi possível se conectar com o servidor.");
  $varsql = "SELECT * FROM tb_comentarios WHERE estado='pe' ORDER BY id DESC LIMIT 10";
  $carregar_comentarios = mysqli_query($conexao, $varsql);
  while ($linha = mysqli_fetch_array($carregar_comentarios))
  {
    $user = $linha["user"];
    $texto = $linha["texto"];
    $data = $linha["data"];
    echo '<tr><td colspan="4"><strong>'.$user.'</strong>: '.$texto.' - '.$data.'</td></tr>';
  }
?>

If you want to set a maximum height of the td then use the CSS height property to do so. Setting the height in the td property's (e.g height="400px") will always act as a minimum and resize if the content is larger.

Having said that, in my view the best way to achieve what I think you want would be to return part of the comment using substr() and then have a "view full comment" link which dynamically changes the content using AJAX. In this scenario you would in fact want to set the height in the td property's as you want it to resize.

Andrew
  • 39
  • 8