【问题标题】:Combine multiple table results into one table in php在php中将多个表结果合并到一个表中
【发布时间】:2015-09-21 06:17:45
【问题描述】:

我遵循了一个关于如何从我的数据库构建基本搜索引擎的网络教程,问题是当我显示结果时,它会在我的页面上的自己的表格中显示每个结果?我想合并结果,以便它们都显示在 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>}

【问题讨论】:

    标签: php html mysql search html-table


    【解决方案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>
    

    【讨论】:

      【解决方案2】:
      ...} 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>
      

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

      【讨论】:

      • 您好,非常感谢!这完成了我希望它做的唯一问题是它在搜索中重复标题,而不仅仅是在页面顶部有一个标题。有任何想法吗?感谢您的帮助。
      • 没关系!我将 移到了 php 代码和 wallah 的上方!谢谢。
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多