简体   繁体   中英

search query into html table

This is my code :

<?php

if (isset($_GET['query']))
    $query = $_GET['query'];

$min_length = 3;

 if (isset($_GET['query']))
if(strlen($query) >= $min_length){ 

    $query = htmlspecialchars($query); 


    $query = mysql_real_escape_string($query);


    $raw_results = mysql_query("SELECT * FROM studentsroom
        WHERE (`room` LIKE '%".$query."%') OR (`name` LIKE '%".$query."%') OR (`studentId` LIKE '%".$query."%') ORDER BY room") or die(mysql_error());



    if(mysql_num_rows($raw_results) > 0){ 

        while($results = mysql_fetch_array($raw_results)){


            echo '<table border="1" cellspacing="1">';

            '<td width="100"><strong>&nbsp;Room: </strong>'.$results['room'].'</td>
            <td width="200"><strong>&nbsp;Student ID: </strong>'.$results['studentId'].'</td>&nbsp;
            <td width="250"><strong>&nbsp;Name: </strong>'.$results['name'].'</td>';
            echo '</table>';



        }

    }
    else{ 
        echo "No results";
    }

}
else{ 
    echo "Minimum length is ".$min_length;
}

?>

I want the results to be displayed in a table but without the spacing in between as below :

在此输入图像描述

Could someone help me with a better HTML code for the results displayed in the table ?

Take the table tag outside the loop, and generate a new row for each entry:

if(mysql_num_rows($raw_results) > 0){ 
  echo '<table border="1" cellspacing="1">';
  while($results = mysql_fetch_array($raw_results)){
    echo  '<tr>
      <td width="100"><strong>&nbsp;Room: </strong>'.$results['room'].'</td>
      <td width="200"><strong>&nbsp;Student ID </strong>'.$results['studentId'].'</td>
      <td width="250"><strong>&nbsp;Name: </strong>'.$results['name'].'</td>
    </tr>';
  }
  echo '</table>';
}

Edit: also note that mysql functions are deprecated , switch to mysqli or PDO instead.

try this

echo '<table border="1" cellspacing="1">';
  while($results = mysql_fetch_array($raw_results)){
    echo '<tr>';
    echo '
     <td width="100"><strong>&nbsp;Room: </strong>'.$results['room'].'</td>
     <td width="200"><strong>&nbsp;Student ID:</strong>'.$results['studentId'].'</td>
     <td width="250"><strong>&nbsp;Name: </strong>'.$results['name'].'</td>';
    echo '</tr>';
  }
echo '</table>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM