将多个表结果合并到php中的一个表中

时间:2015-09-21 06:17:46

标签: php html mysql search html-table

我遵循了一个关于如何从我的数据库构建基本搜索引擎的网络教程问题是,当我得到显示的结果时,它会在我的页面上显示每个结果在自己的表中?我想合并结果,所以它们都显示在html中的一个表下。

<?php
include("dbconnect.php");

if(!isset($_POST['search'])) {
header("Location:www.bacons.me");
}
$search_sql="SELECT * FROM users WHERE username OR FirstName LIKE '%".$_POST['search']."%'";
$search_query=mysql_query($search_sql);
if(mysql_num_rows($search_query) !=0) {
$search_rs=mysql_fetch_assoc($search_query);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="Bacons.me">
<meta name="keywords" content="HTML,CSS,JavaScript">
<meta name="author" content="James Narey">
<meta name=viewport content="width=device-width, initial-scale=1">

<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />


<link rel="stylesheet" type="text/css" href="main.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<title>Bacons.me</title>
</head>
<body>
    <h1 class="logo"><a href="http://www.bacons.me/" class="logo-    a">BACONS.ME</a></h1>
    <nav>
        <ul>
            <li><a href="http://www.bacons.me" class="nav-a">Home</a></li>
            <li><a href="#" class="nav-a">About</a></li>
            <li><a href="search" class="nav-a">Search</a></li>
            <li><a href="contact" class="nav-a">Contact</a></li>
        </ul>
        <div class="handle">Menu<span class="arrow"> ▾</span></div>
    </nav>

<p>Search Results</p>
<?php if(mysql_num_rows($search_query) !=0) {
do { ?>
<table>
<tr>
    <th>Username</th>
    <th>First Name</th>
    <th>Surname</th>
    <th>Shift</th>
    <th>Agency</th>
</tr>
<tr>
        <td><?php echo $search_rs['username']; ?></td>
        <td><?php echo $search_rs['FirstName']; ?></td>
        <td><?php echo $search_rs['LastName']; ?></td>
        <td><?php echo $search_rs['Shift']; ?></td>
        <td><?php echo $search_rs['Agency']; ?></td>
</tr>
<br>
</table>
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));

} else {
    echo "No results found";
    }


 ?>

 <html>
 <title>results</title>
 <head>
 </head>
 <body>
 </body>
 </html>
      <footer>
    <p class="footer">Website created by James Narey 2015.</p>
  </footer>
</body>
</html>}

3 个答案:

答案 0 :(得分:1)

将表标记放在do while循环之外,否则它会在循环的每次迭代中创建新表。 你的代码结构应该是这样的

<?php if(mysql_num_rows($search_query) !=0) {?>
<table>
<?php do { ?>
<tr>
    <th>Username</th>
    <th>First Name</th>
    <th>Surname</th>
    <th>Shift</th>
    <th>Agency</th>
</tr>
<tr>
    <td><?php echo $search_rs['username']; ?></td>
    <td><?php echo $search_rs['FirstName']; ?></td>
    <td><?php echo $search_rs['LastName']; ?></td>
    <td><?php echo $search_rs['Shift']; ?></td>
    <td><?php echo $search_rs['Agency']; ?></td>
</tr>
<br>
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));
?>
</table>

答案 1 :(得分:0)

...} while ($search_rs=mysql_fetch_assoc($search_query));

我怀疑这是问题所在。您每次获取下一条记录时都会创建一个表格。你可能想做这样的事情:

<table>
<?php if(mysql_num_rows($search_query) !=0) {
do { ?>

// fill in table...

<?php
} while ($search_rs=mysql_fetch_assoc($search_query)); 
?>
</table>

请注意,表格标签位于循环之外,因此您只需为每个获取的记录创建一个新行

答案 2 :(得分:0)

只需将表格标签放在循环结构之外:

<table> do { ?> <tr>
    <th>Username</th>
    <th>First Name</th>
    <th>Surname</th>
    <th>Shift</th>
    <th>Agency</th> </tr> <tr>
        <td><?php echo $search_rs['username']; ?></td>
        <td><?php echo $search_rs['FirstName']; ?></td>
        <td><?php echo $search_rs['LastName']; ?></td>
        <td><?php echo $search_rs['Shift']; ?></td>
        <td><?php echo $search_rs['Agency']; ?></td> </tr> <br> <?php } while ($search_rs=mysql_fetch_assoc($search_query)); </table>