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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17318670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:40:51  来源:igfitidea点击:

How to display data with in a table format

phphtmlmysql

提问by user2520162

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

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

form.php

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

<?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.

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

采纳答案by casraf

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

$output只是在输出数据本身。让它也输出一个HTML 表格

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.

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

回答by Pruthviraj Nayak

<?php
include("include/connect.php");
$emp_id = $_POST['emp_id'];
$sqlStr = "SELECT * FROM `employee` ";
//echo $sqlStr . "<br>";
$result = mysql_query($sqlStr);
$count = mysql_num_rows($result);


echo '<table>
    <tr>
        <th>Employee ID</th>
        <th>Name</th>
        <th>date of joining</th>
        <th>date of living in service</th>
        <th>salary</th>
    </tr>';

while ($row = mysql_fetch_array($result)) {
    echo '
        <tr>
            <td>'.$row['emp_id'].'</td>
            <td>'.$row['name'].'</td>
            <td>'.$row['DOJ'].'</td>
            <td>'.$row['DLS'].'</td>
            <td>'.$row['salary'].'</td>
        </tr>';

}

echo '
</table>';
?>