简体   繁体   English

如何使用PHP和MySQL在一张表中显示搜索结果?

[英]How to display search results in one table with PHP & MySQL?

I have the code to do the search. 我有执行搜索的代码。 But the search results are not in the same table. 但是搜索结果不在同一表中。 All search results appear in a different table. 所有搜索结果显示在不同的表中。 How do I make them appear in one table? 如何使它们出现在一张桌子上?

Screenshot : 屏幕截图:

我的应用程序的屏幕截图

And here's my code : 这是我的代码:

> search.php

<?php

    $query = $_GET['query'];


    $min_length = 1;


    if(strlen($query) >= $min_length){ 

        $query = htmlspecialchars($query);

        $query = mysql_real_escape_string($query);

        $raw_results = mysql_query("SELECT * FROM barang WHERE (`tanggal` LIKE '%".$query."%')") or die(mysql_error());


        if(mysql_num_rows($raw_results) > 0){ 

            while($results = mysql_fetch_assoc($raw_results)){
                ?>

                <table width="107%" class="view">
        <thead>
            <tr>
        <th width="180">Tanggal</th>
        <th width="150">Barang Masuk</th>
        <th width="90">Bijih Keluar</th>
        <th width="120">Kantong Hitam Keluar</th>
        <th width="120">Kantong Putih Keluar</th>
        <th width="90">Stok Bijih</th>
        <th width="90">Stok Kantong Hitam</th>
        <th width="90">Stok Kantong Putih</th>
        <th width="130">Catatan</th>
            </tr>
        </thead>



                    <td><?php echo $results['tanggal']; ?></td>



                <td><?php echo $results['barang_in']; ?></td>


                        <td><?php echo $results['bijih_out']; ?></td>


                <td><?php echo $results['htm_out']; ?></td>

                <td><?php echo $results['pth_out']; ?></td>

                <td><?php echo $results['bijih']; ?></td>

                <td><?php echo $results['kantong_htm']; ?></td>

                <td><?php echo $results['kantong_pth']; ?></td>

                <td><?php echo $results['note']; ?></td>
   <?php             
            }

        }
        else{ // if there is no matching rows do following
            echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
        }

    }
    else{ 
        echo "Minimum length is ".$min_length;
    }
?>

So how to make the search results appear just in one table? 那么如何使搜索结果仅出现在一个表中? Am I wrong to put the table code? 我把表格代码放错了吗? Or something else? 或者是其他东西? And one more question, How to add numbers for each result? 还有一个问题,如何为每个结果加数字? Thank you in advance for your time and the help. 预先感谢您的时间和帮助。

Move the <table> tag outside of your while loop. <table>标记移到while循环之外。

Should be like this.. 应该是这样..

  echo "<table width="107%" class=/"view/">";
  while($results = mysql_fetch_assoc($raw_results)){
                ?>

  <!-- Comment this 
                <table width="107%" class="view">

  -->
  <thead>

Move <table> tag outside your while loop and also add a <TR> tag inside the while loop and dont forget to close tag. <table>标记移至while循环之外,并在while循环内添加<TR>标记,不要忘记关闭标记。 For Serial Number you have to introduce another variable as a counter. 对于序列号,您必须引入另一个变量作为计数器。 In the code given below i added $i . 在下面给出的代码中,我添加了$i Change it if you had already used $i in your code. 如果您已经在代码中使用$ i,请更改它。 I think after changing your code will look like 我认为更改代码后,

?>
    <table width="107%" class="view">
      <thead>
        <tr>
          <th>SN</th>//new Line
          <th width="180">Tanggal</th>
          <th width="150">Barang Masuk</th>
          <th width="90">Bijih Keluar</th>
          <th width="120">Kantong Hitam Keluar</th>
          <th width="120">Kantong Putih Keluar</th>
          <th width="90">Stok Bijih</th>
          <th width="90">Stok Kantong Hitam</th>
          <th width="90">Stok Kantong Putih</th>
          <th width="130">Catatan</th>
        </tr>
      </thead>
<?php
    $i=1;//new line
    while($results = mysql_fetch_assoc($raw_results)){
?>
    <tr>
      <td><?php echo $i; ?> </td>//new line
      <td><?php echo $results['tanggal']; ?></td>
      <td><?php echo $results['barang_in']; ?></td>
      <td><?php echo $results['bijih_out']; ?></td>
      <td><?php echo $results['htm_out']; ?></td>
      <td><?php echo $results['pth_out']; ?></td>
      <td><?php echo $results['bijih']; ?></td>
      <td><?php echo $results['kantong_htm']; ?></td>
      <td><?php echo $results['kantong_pth']; ?></td>
      <td><?php echo $results['note']; ?></td>
    </tr>
<?php             
    $i++;//new line
    }
?>
</table>
<?php
}
else{ // if there is no matching rows do following
  echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
}

use this 用这个

<?php
    $query = $_GET['query'];
    $min_length = 1;
    if(strlen($query) >= $min_length)
    { 
        $query = htmlspecialchars($query);
        $query = mysql_real_escape_string($query);
        $raw_results = mysql_query("SELECT * FROM barang WHERE (`tanggal` LIKE '%".$query."%')") or die(mysql_error());
        if(mysql_num_rows($raw_results) > 0)
        { 
?>
        <table width="107%" class="view">
        <thead>
            <tr>
                <th width="180">Tanggal</th>
                <th width="150">Barang Masuk</th>
                <th width="90">Bijih Keluar</th>
                <th width="120">Kantong Hitam Keluar</th>
                <th width="120">Kantong Putih Keluar</th>
                <th width="90">Stok Bijih</th>
                <th width="90">Stok Kantong Hitam</th>
                <th width="90">Stok Kantong Putih</th>
                <th width="130">Catatan</th>
            </tr>
        </thead>
<?php
        while($results = mysql_fetch_assoc($raw_results))
        {
            <tr>
                <td><?php echo $results['tanggal']; ?></td>
                <td><?php echo $results['barang_in']; ?></td>
                <td><?php echo $results['bijih_out']; ?></td>
                <td><?php echo $results['htm_out']; ?></td>
                <td><?php echo $results['pth_out']; ?></td>
                <td><?php echo $results['bijih']; ?></td>
                <td><?php echo $results['kantong_htm']; ?></td>
                <td><?php echo $results['kantong_pth']; ?></td>
                <td><?php echo $results['note']; ?></td>
            </tr>
   <?php             
        }
?>
        </table>
<?php

        }
        else
        { // if there is no matching rows do following
            echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
        }

    }
    else
    { 
        echo "Minimum length is ".$min_length;
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM