搜索查询与表格显示

时间:2015-07-30 10:48:55

标签: php mysql

我在这里有table table.php'其搜索以表格格式显示数据库表。我下面的代码可以搜索并显示数据,但不能以表格格式显示。请帮我编码,以表格形式从搜索中进行显示       

   $output='';
   $noresult='';                                        
   //collect
   if (isset($_POST['search'])) 
    {
        $searchq = $_POST['search'];
        $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

        $query = mysql_query("SELECT * FROM transaction where patientid like '%$searchq%' ") or die ("could not search");
        $count = mysql_num_rows($query);    

        if($count == 0)
        {
            $noresult = 'There was no search results!';
            } else 
                {           
                while($row = mysql_fetch_array($query)) 
                    {
                    $tranid = $row['tranid'];
                    $trandate = $row['trandate'];
                    $trandescription = $row['trandescription'];
                    $tranquantity = $row['tranquantity'];
                    $tranunitprice= $row['tranunitprice'];
                    $tranamount = $row['tranamount'];

                    $output .= '<div>'.$tranid.''.$trandescription.'</div>';
        }
      }
    }

    ?>          

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

echo "<table>
    <tr>
        <th scope='col'>id</th>
        <th scope='col'>date</th>
        <th scope='col'>description</th>
        <th scope='col'>quantity</th>
        <th scope='col'>unit price</th>
        <th scope='col'>amount</th>
    </tr>";
while($row = mysql_fetch_array($query)) {
    echo "
        <tr>
            <td>".$row['tranid']."</td>
            <td>".$row['trandate']."</td>
            <td>".$row['trandescription']."</td>
            <td>".$row['tranquantity']."</td>
            <td>".$row['tranunitprice']."</td>
            <td>".$row['tranamount']."</td>
        </tr>
    ";
}
echo "</table>";

答案 1 :(得分:0)

以下代码必须在else块中实现。

                $ouput .= '<table>
                 <thead>
                  <tr>
                   <th>id</th>
                   <th>date</th>
                   <th>description</th>
                   <th>quantity</th>
                   <th>unit price</th>
                   <th>amount</th>                   
                  </tr>
                  </thead>
                  <tbody>';

                while($row = mysql_fetch_array($query)) 
                {
                $row = "<tr>
                        <td>".$row['tranid']."</td>
                        <td>".$row['trandate']."</td>
                        <td>".$row['trandescription']."</td>
                        <td>".$row['tranquantity']."</td>
                        <td>".$row['tranunitprice']."</td>
                        <td>".$row['tranamount']."</td>
                        </tr>";

                $output .= $row;
                }
                $output .= "</tbody></table>";
                echo $output;