gpt4 book ai didi

php - 如何以表格格式显示数据

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:12 25 4
gpt4 key购买 nike

我想从mysql中查询数据,需要用表格的形式展示出来。此处数据搜索成功,但数据未显示在表格中,而是以直线显示。这里我使用 $output 方法。我的编码是

表单.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>

搜索.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;
?>

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

最佳答案

您的$output 仅输出数据本身。让它也输出 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>';

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

关于php - 如何以表格格式显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17318670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com