0

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 :

Screenshot of my application

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.

3 Answers 3

1

Move <table> tag outside your while loop and also add a <TR> tag inside the while loop and dont forget to close tag. For Serial Number you have to introduce another variable as a counter. In the code given below i added $i. Change it if you had already used $i in your code. 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.";
}
Sign up to request clarification or add additional context in comments.

3 Comments

hey, it works! Thank you, can I have one more question? How to add numbers for each results? hehe
check the answer :) Always Welcome
happy to help.. and always use proper indentations
1

Move the <table> tag outside of your while loop.

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>

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.