簡體   English   中英

如何使用PHP和MySQL在一張表中顯示搜索結果?

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

我有執行搜索的代碼。 但是搜索結果不在同一表中。 所有搜索結果顯示在不同的表中。 如何使它們出現在一張桌子上?

屏幕截圖:

我的應用程序的屏幕截圖

這是我的代碼:

> 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;
    }
?>

那么如何使搜索結果僅出現在一個表中? 我把表格代碼放錯了嗎? 或者是其他東西? 還有一個問題,如何為每個結果加數字? 預先感謝您的時間和幫助。

<table>標記移到while循環之外。

應該是這樣..

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

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

  -->
  <thead>

<table>標記移至while循環之外,並在while循環內添加<TR>標記,不要忘記關閉標記。 對於序列號,您必須引入另一個變量作為計數器。 在下面給出的代碼中,我添加了$i 如果您已經在代碼中使用$ i,請更改它。 我認為更改代碼后,

?>
    <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.";
}

用這個

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