Where can I put the TH in this table?

0

Hi, I have this table where I show a result, but when I put the TH, the TH is repeated the number of times according to the result.

<head>
<!--<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
--><script src="jquery-3.2.1.min.js"> </script>

<style>
tr:hover {background-color:#CBCBCB;}
div.scroll{
    width: 100%;
    height: 50%;
    overflow-y: scroll;
    overflow-x: hidden;

    }


</style>


<script language="javascript">
$(document).ready(function(){
    $('.llamar').submit(function(){

    var x = confirm("Are you sure you want to update?");
      if (x){
        $.ajax({
          type: "POST",
          url: "buscar2.php",
          data: $(this).serialize(),
          cache: false,
          success: function(data) {
                //$('#result').show(3000);

                $('#result').html(data).
            fadeIn().delay(1000).fadeOut();

            }
        });//end ajax
        return false;
      }

    });
});
</script>

</head>
<div class="scroll">

<?php

      $buscar = $_POST['b'];

      if(!empty($buscar)) {
            buscar($buscar);
      }

      function buscar($b) {
            $con = mysql_connect('localhost','root', 'master3.1416');
            mysql_select_db('tel', $con);

            $sql = mysql_query("SELECT * FROM tel WHERE nombre LIKE '%".$b."%' OR tel LIKE '%".$b."%' LIMIT 30" ,$con);

            $contar = @mysql_num_rows($sql);

            if($contar == 0){
                  echo "No se han encontrado resultados para '<b>".$b."</b>'.";
            }else{
              while($row=mysql_fetch_array($sql)){
                $id = $row['id'];
                $nombre = $row['nombre'];
                $tel = $row['tel'];

             echo "<center>";

              echo"<form  autocomplete='off' class='llamar' onsubmit='return false'  > \n";

             echo"<table cellpadding='10' cellspacing='0' > \n";



                echo"<tr> \n";

                echo"<td  width='0' > <input type='text' size='0' name='id[]' value='$id' hidden /> </td> \n";

                echo"<td  width='200' > <input type='text' size='50' name='nombre[]' value='$nombre' required /> </td> \n";

                echo"<td width='20' > <input type='text' size='20' name='tel[]' value='$tel' required /> </td> \n";
                echo"</tr> \n";
                echo"</table> \n";

              }
                                        echo"<input name='submit' type='submit' value='Editar' />";

                echo"</form>";

                echo"</center>";




        }
  }

?>
</div>
<div id="result"> </div>
    
asked by Leonardo 12.07.2017 в 06:08
source

2 answers

1

If you draw the table inside the while, obviously you will have several repeated tables. Instead, do it like this:

<?php

$buscar = $_POST['b'];

if(!empty($buscar)) {
  buscar($buscar);
}

function buscar($b) {
  $con = mysql_connect('localhost','root', 'master3.1416');
  mysql_select_db('tel', $con);

  $sql = mysql_query("SELECT * FROM tel WHERE nombre LIKE '%".$b."%' OR tel LIKE '%".$b."%' LIMIT 30" ,$con);

  $contar = @mysql_num_rows($sql);

  if($contar == 0){
    echo "No se han encontrado resultados para '<b>".$b."</b>'.";
  }else{

    echo "<center>";

    echo"<form  autocomplete='off' class='llamar' onsubmit='return false'  > \n";

    echo"<table cellpadding='10' cellspacing='0' > \n";

    while($row=mysql_fetch_array($sql)){

     $id = $row['id'];
     $nombre = $row['nombre'];
     $tel = $row['tel'];

     echo"<tr> \n";

     echo"<td  width='0' > <input type='text' size='0' name='id[]' value='$id' hidden /> </td> \n";

     echo"<td  width='200' > <input type='text' size='50' name='nombre[]' value='$nombre' required /> </td> \n";

     echo"<td width='20' > <input type='text' size='20' name='tel[]' value='$tel' required /> </td> \n";

     echo"</tr> \n";


   }

   echo"</table> \n";

   echo"<input name='submit' type='submit' value='Editar' />";

   echo"</form>";

   echo"</center>";


 }
}

?>

Once you have the code so, you can draw a th that does not repeat as long as you do it within else but before while

    
answered by 12.07.2017 в 08:32
1

The TH must go before the while so that it does not repeat:

<?php

$buscar = $_POST['b'];

if(!empty($buscar)) {   buscar($buscar); }

function buscar($b) {   $con = mysql_connect('localhost','root', '*****');   mysql_select_db('tel', $con);

  $sql = mysql_query("SELECT * FROM tel WHERE nombre LIKE '%".$b."%' OR tel LIKE '%".$b."%' LIMIT 30" ,$con);

  $contar = @mysql_num_rows($sql);

  if($contar == 0){
    echo "No se han encontrado resultados para '<b>".$b."</b>'.";   }else{

    echo "<center>";

    echo"<form  autocomplete='off' class='llamar' onsubmit='return false'  > \n";

    echo"<table cellpadding='10' cellspacing='0' > \n";
    echo "<tr><th>ID</th><th>Nombre</th><th>Teléfono</th></tr>\n";

    while($row=mysql_fetch_array($sql)){

     $id = $row['id'];
     $nombre = $row['nombre'];
     $tel = $row['tel'];

     echo"<tr> \n";

     echo"<td  width='0' > <input type='text' size='0' name='id[]' value='$id' hidden /> </td> \n";

     echo"<td  width='200' > <input type='text' size='50' name='nombre[]' value='$nombre' required /> </td> \n";

     echo"<td width='20' > <input type='text' size='20' name='tel[]' value='$tel' required /> </td> \n";

     echo"</tr> \n";


   }

   echo"</table> \n";

   echo"<input name='submit' type='submit' value='Editar' />";

   echo"</form>";

   echo"</center>";


 } }

?>

Also, I suggest you hide the credentials when you post.

Greetings.

    
answered by 12.07.2017 в 15:33