如何以表格格式显示数据 [英] How to display data with in a table format

查看:130
本文介绍了如何以表格格式显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从mysql中搜索数据并需要将其显示在表中。这里数据的搜索是成功的,但数据没有显示在表格中,而是显示在一条直线上。这里使用$输出方法。我的编码是

form.php



 < html dir = ltrlang =en-US> 
< head>
< meta charset =UTF-8/>
< title>搜寻< / title>
< / head>
< body>
< h1> Seacrh By Name< / h1>
< form action =search.phpmethod =get>
< label>名称:
< input type =textname =keyname/>
< / label>
< input type =submitvalue =Search/>
< / form>
< / body>
< / html>



search.php



 <?php 

//捕获搜索词并在其两端删除空格如果是
$ searchTerm = trim($ _ GET ['keyname']] );

//检查被解析的名称是否为空
if($ searchTerm ==)
{
echo输入要搜索的名称。
exit();
}

//数据库连接信息
$ host =xx; //服务器
$ db =xx; //数据库名称
$ user =xx; // dabases用户名
$ pwd =xx; //密码

//连接到服务器并创建数据库链接
$ link = mysqli_connect($ host,$ user,$ pwd,$ db);

// MYSQL search statement
$ query =SELECT * FROM customer_details WHERE id LIKE'%$ searchTerm%';

$ results = mysqli_query($ link,$ query);

*通过计算返回结果的数量,检查表
中是否存在匹配记录* /
if(mysqli_num_rows($ results)> = 1)
{
$ output =;
while($ row = mysqli_fetch_array($ results))
{


$ output。=id:。 $ row ['id']。 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;;
$ output。=name:。 $ row ['name']。 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;;
$ output。=电话:。 $ row ['Telephone']。 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;;
$ output。=E_mail:。 $ row ['E_mail']。 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;;
$ output。=visa_category:。 $ row ['visa_category']。 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;;
$ output。=other_category:。 $ row ['other_category']。 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;;
$ output。=passport_no:。 $ row ['passport_no']。 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;;
$ output。=备注:。 $ row ['remarks']。 &安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;;

}
echo $ output;
}
else
echo名称没有匹配的记录。 $ SEARCHTERM;
?>

如何在html表格中显示我的数据。

解决方案

您的 $ output 仅输出数据本身。使其也输出 HTML表格

  echo'< table> 
< tr>
< th> ID< / th>
< th>名称< / th>
< / tr>';

while($ row = mysqli_fetch_array($ results)){
echo'
< tr>
< td>'。$ row ['id']。'< / td>
< td>'。$ row ['name']。'< / td>
< / tr>';

}

echo'

顺便说一句,不要直接向下滚动到代码,请参阅有关何时/如何使用表格的警告。


I want to search the data from mysql and need to display it in a table. Here searching of data is successful but the data is not getting displayed in table, instead it is displaying in a straight line. Here am using $output method. My codings are

form.php

<html dir="ltr" lang="en-US" >
<head>
<meta charset="UTF-8" />
<title>Search</title>
</head>
<body>
<h1>Seacrh By Name</h1>
<form action="search.php" method="get">
  <label>Name:
  <input type="text" name="keyname" />
  </label>
  <input type="submit" value="Search" />
</form>
</body>
</html>

search.php

<?php

//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);

//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}

//database connection info
$host = "xx"; //server
$db = "xx"; //database name
$user = "xx"; //dabases user name
$pwd = "xx"; //password

 //connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement
$query = "SELECT * FROM customer_details WHERE id LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);

/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
    $output = "";
    while($row = mysqli_fetch_array($results))
{


$output .= "id: " . $row['id'] . "&nbsp;&nbsp;&nbsp;&nbsp;";
$output .= "name: " . $row['name'] . "&nbsp;&nbsp;&nbsp;&nbsp;";
$output .= "Telephone: " . $row['Telephone'] . "&nbsp;&nbsp;&nbsp;&nbsp;";
$output .= "E_mail: " . $row['E_mail'] . "&nbsp;&nbsp;&nbsp;&nbsp;";
$output .= "visa_category: " . $row['visa_category'] . "&nbsp;&nbsp;&nbsp;&nbsp;";
$output .= "other_category: " . $row['other_category'] . "&nbsp;&nbsp;&nbsp;&nbsp;";
$output .= "passport_no: " . $row['passport_no'] . "&nbsp;&nbsp;&nbsp;&nbsp;";
$output .= "remarks: " . $row['remarks'] . "&nbsp;&nbsp;&nbsp;&nbsp;";

}
echo $output;
}
else
    echo "There was no matching record for the name " . $searchTerm;
?>

How can i display my data's in html table.

解决方案

Your $output is only outputting the data itself. Make it also output an HTML table.

echo '<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
    </tr>';

while ($row = mysqli_fetch_array($results)) {
    echo '
        <tr>
            <td>'.$row['id'].'</td>
            <td>'.$row['name'].'</td>
        </tr>';

}

echo '
</table>';

By the way, don't scroll down straight to the code, see the warnings about when/how to use tables as well.

这篇关于如何以表格格式显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆