dongziya9863 2014-05-22 08:28
浏览 24
已采纳

搜索查询到html表

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 ?

  • 写回答

2条回答 默认 最新

  • dougu3290 2014-05-22 08:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致