Athirah Hazira
Athirah Hazira

Reputation: 473

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 :

enter image description here

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

Upvotes: 0

Views: 55

Answers (2)

Droa
Droa

Reputation: 465

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>';

Upvotes: 1

Michael Kunst
Michael Kunst

Reputation: 2988

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.

Upvotes: 1

Related Questions