如何在paginate php中过滤第二页

时间:2016-12-06 17:44:24

标签: php html pagination

我的分页只显示存储在数据库中的所有文件,我想根据日期过滤数据。当我在第一页中过滤数据时,它可以正常工作,但是当我更改为第二页时,过滤器再次无效。

我的问题是当我点击分页时,第二页将再次检索存储在数据库中的所有文件。我希望第二页是过滤结果。

<div class="row">
    <div class="col-xs-12 col-md-7"></div>
    <div class="col-xs-6 col-md-5" style="padding-right: 40px; padding-bottom: 20px;">
      <header><h4 class="page-header"><b>Cari Berdasarkan Tanggal</b></h4></header>

      <form class="form-horizontal" method="POST" action="">

        <div class="form-group" style="padding-bottom: 0px; margin-bottom: 5px;">
          <label class="col-sm-3 control-label"><b>Dari Tanggal :</b></label>
          <div class="col-sm-5" style="padding-left: 0px;">
            <input type="date" class="form-control" name="tgl_awal">
          </div>
        </div>

        <div class="form-group" style="padding-bottom: 0px; margin-bottom: 5px;">
          <label class="col-sm-3 control-label"><b>Sampai Tanggal :</b></label>
          <div class="col-sm-5" style="padding-left: 0px;">
            <input type="date" class="form-control" name="tgl_akhir">
          </div>
        </div>

        <div class="form-group" style="padding-bottom: 0px; margin-bottom: 5px;">
          <label class="col-sm-3 control-label"></label>
          <div class="col-sm-7" style="padding-left: 0px;">
            <input type="submit" name="submit" value="Cari">
          </div>
        </div>

      </form>
    </div>
  </div>

  <!-- Database Fillter berdasarkan tanggal -->
  <?php
  include '../koneksi.php';

  @$tgl_awal = $_POST['tgl_awal'];
  @$tgl_akhir = $_POST['tgl_akhir'];
  @$submit = $_POST['submit'];
  echo $tgl_awal;
  echo $tgl_akhir;

  /*Filter Tanggal*/
  if ($submit) {
      $query1="SELECT * FROM item_aruskas i join aruskas a on i.id_aruskas=a.id WHERE noref like '%CI%' AND tgl between '$tgl_awal' AND '$tgl_akhir'  order by tgl DESC "; 

  }else{
      $query1="SELECT * FROM item_aruskas i join aruskas a on i.id_aruskas=a.id where noref like '%CI%' order by noref DESC ";
  }

  /*sort data*/
  $pola='asc';
  $polabaru='asc';
  if(isset($_GET['orderby'])){
    $orderby=$_GET['orderby'];
    $pola=$_GET['pola'];

    $query1="SELECT * FROM item_aruskas i join aruskas a on i.id_aruskas=a.id order by $orderby $pola ";
    if($pola=='asc'){
      $polabaru='desc';

    }else{
      $polabaru='asc';
    }
  }

  /*Pagingging dan cek filter data*/
  error_reporting(0);
  $batas = 10;
  $halaman = $_GET['halaman'];
  $posisi = null;
  if (empty($halaman)) {
    $posisi=0;
    $halaman=1;
  }else{
    $posisi = ($halaman-1)* $batas;
  }

  $result=mysqli_query($koneksi, $query1 .= "limit $posisi,$batas") or die(mysqli_error());

  $cek = mysqli_num_rows($result);
  if ($cek < 1) {
    ?>
    <div class="row">
      <div class="col-md-4"></div><div class="col-md-8"></div>
    </div>
    <div class="row">
      <div class="col-md-4"></div><div class="col-md-8"></div>
    </div>
    <div class="row">
      <div class="col-md-4"></div><div class="col-md-8"></div>
    </div>
    <div class="row">
      <div class="col-md-4"></div>
      <div class="col-md-4" style="text-align: center;"><h4><b>Data Tidak Ditemukan.<br><a href="lap_km.php">Kembali Laporan Kas Masuk</a></b></h4></div><div class="col-md-4"></div>
    </div>

    <?php
  }else{
    if (!isset($tgl_awal,$tgl_akhir)) {

      ?>

      <!-- Baris Cetak Data -->
      <div class="row">
        <div class="col-xs-12 col-md-7" style="margin-left: 40px; padding-bottom: 20px;">
          <a href="cetak_lap_km.php" target="_blank">
            <button class="btn btn-default btn-lg"><img src="../img/print16.png" style="padding-right: 10px;"><b>Cetak </b></button>
          </a>
        </div>
        <div class="col-xs-6 col-md-5"></div>
      </div>
      <?php }else{
       ?>
       <div class="row">
        <div class="col-xs-12 col-md-7" style="margin-left: 40px; padding-bottom: 20px;">
          <form method="POST" action="../laporan/cetak_lap_km.php" target="_blank" > 
            <input type="hidden" name="tgl_awal" value="<?php echo $tgl_awal; ?>">
            <input type="hidden" name="tgl_akhir" value="<?php echo $tgl_akhir; ?>">
            <button name="submit" class="btn btn-default "><img src="../img/print16.png" style="padding-right: 10px;"><b>Cetak </b></button>
          </form>
        </div>
        <div class="col-xs-6 col-md-5"></div>
      </div>
      <?php } ?>

      <div class="row">
        <div class="col-lg-12">
          <section class="panel">
            <header class="panel-heading" style="text-align: center;">
              <b>Laporan Kas Masuk</b>
            </header>

            <div class="table-responsive">  
              <table class="table table-striped table-advance table-hover table-bordered">

                <thead>
                  <tr>
                   <td style="text-align: center; width: 5%;">No.</td>
                   <td style="width: 25%; text-align: center;">
                    <a href='lap_km.php?orderby=nama&pola=<?=$polabaru;?>'>Nama</a>
                  </td>
                  <td align="center">Memo</td>
                  <td style="text-align: center;width: 10%;">
                    <a href='lap_km.php?orderby=noref&pola=<?=$polabaru;?>'>No. Referensi</a>
                  </td>
                  <td align="center" style="width: 10%;">
                    <a href='lap_km.php?orderby=tgl&pola=<?=$polabaru;?>'>Tanggal</a>
                  </td>
                  <td align="center" style="width: 12%;">
                    <a href='lap_km.php?orderby=nilai&pola=<?=$polabaru;?>'>Nilai</a>
                  </td>
                </tr>
              </thead>

              <?php
              include '../koneksi.php';

              function format_rupiah($angka){
                $rupiah="Rp  " . number_format($angka,0,',','.');
                return $rupiah;
              }



              $no = $posisi + 0;

                while($record = mysqli_fetch_array($result)){
                  $no++;
                  ?>

                  <tbody>
                    <tr>
                      <td style="text-align: center;"><?php echo $no; ?></td>
                      <td style="text-align: center;"><?php echo $record['nama'];?></td>
                      <td><?php echo $record['memo'];?></td>
                      <td style="text-align: center;"><?php echo $record['noref']; ?></td>
                      <td style="text-align: center;"><?php echo date('d-m-Y', strtotime($record['tgl']));?></td>
                      <td style="text-align: right;"><?php echo format_rupiah($record['nilai']);?></td>
                    </tr> 
                    <?php }
                    ?>                            
                  </tbody>
                </table>

                <?php
                /*Menghitung Jumlah data*/
                if (!isset($tgl_awal,$tgl_akhir)) {
                  $sql_paging = mysqli_query($koneksi, "SELECT nama  from item_aruskas i join aruskas a on i.id_aruskas=a.id where noref like '%CI%' ");                    
                }else{
                  $sql_paging = mysqli_query($koneksi, "SELECT nama  from item_aruskas i join aruskas a on i.id_aruskas=a.id WHERE noref like '%CI%' AND tgl between '$tgl_awal' AND '$tgl_akhir'  order by tgl DESC ");
                }


                $jmldata = mysqli_num_rows($sql_paging);
                $jmlhalaman = ceil($jmldata / $batas);
                ?>

                <div class="row">  
                  <div class="col-md-8" style="font-size: large; text-align: left; padding-left: 27px;">
                    <?php
                    echo "<b>Halaman : </b>";
                    for($i = 1; $i <= $jmlhalaman; $i++)
                      if($i != $halaman) {
                        echo "<a href=lap_km.php?halaman=$i>$i</a>|";
                      } else {
                        echo "<b>$i</b>|";
                      }
                      mysqli_close($koneksi);?>
                    </div> 
                    <div class="col-md-4" style="font-size: large; text-align: right; padding-right: 28px;">
                      Jumlah data :<?php echo $jmldata;?>
                    </div> 
                  </div>


                    <?php } ?>

                  </div>  

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

我做了哪些更改。

1)method='POST'更改为method='GET'

2)根据要求更改了整段代码。

<!-- Database Fillter berdasarkan tanggal -->
  <?php
  include '../koneksi.php';

  $tgl_awal = $_GET['tgl_awal'];
  $tgl_akhir = $_GET['tgl_akhir'];
  $submit = $_GET['submit'];

  $search_query = "&tgl_awal=".$tgl_awal."&tgl_akhir=".$tgl_akhir."&submit=".$submit;

  $main_query = "SELECT * FROM item_aruskas i join aruskas a on i.id_aruskas=a.id ";
  $query_condition = "";
  /*Filter Tanggal*/
  if ($submit) {
    $query_condition = " WHERE noref like '%CI%' AND tgl between '$tgl_awal' AND '$tgl_akhir'  order by tgl DESC ";
  } else {
    $query_condition = " WHERE noref like '%CI%' order by noref DESC ";
  }

  /*sort data*/
  $pola = 'asc';
  $polabaru = 'asc';
  if(isset($_GET['orderby'])){
    $orderby = $_GET['orderby'];
    $pola = $_GET['pola'];

    $search_query = $search_query."&pola=".$pola;
    $query_condition = " order by $orderby $pola";

    if($pola == 'asc'){
      $polabaru='desc';
    } else {
      $polabaru='asc';
    }
  }

  /*Pagingging dan cek filter data*/
  error_reporting(0);
  $batas = 10;
  $halaman = isset($_GET['halaman']) ? $_GET['halaman'] : 1;
  $posisi = ($halaman-1)* $batas;

  $query1 = $main_query.$query_condition." limit $posisi,$batas";
  $result=mysqli_query($koneksi, $query1) or die(mysqli_error());
  $cek = mysqli_num_rows($result);

3)更改了分页网址/链接。附加搜索URL(GET Paramaeters)

if($i != $halaman) {
  echo "<a href=lap_km.php?halaman=$i.$search_query>$i</a>|";
} else {
  echo "<b>$i</b>|";
}

4)已更改

if (!isset($tgl_awal,$tgl_akhir)) {
    $sql_paging = mysqli_query($koneksi, "SELECT nama  from item_aruskas i join aruskas a on i.id_aruskas=a.id where noref like '%CI%' ");                    
  }else{
    $sql_paging = mysqli_query($koneksi, "SELECT nama  from item_aruskas i join aruskas a on i.id_aruskas=a.id WHERE noref like '%CI%' AND tgl between '$tgl_awal' AND '$tgl_akhir'  order by tgl DESC ");
  }

$sql_paging = mysqli_query($koneksi,$query1);

5)已更改

<thead>
  <tr>
    <td style="text-align: center; width: 5%;">No.</td>
    <td style="width: 25%; text-align: center;">
      <a href='lap_km.php?orderby=nama&pola=<?=$polabaru;?>'>Nama</a>
    </td>
    <td align="center">Memo</td>
    <td style="text-align: center;width: 10%;">
      <a href='lap_km.php?orderby=noref&pola=<?=$polabaru;?>'>No. Referensi</a>
    </td>
    <td align="center" style="width: 10%;">
      <a href='lap_km.php?orderby=tgl&pola=<?=$polabaru;?>'>Tanggal</a>
    </td>
    <td align="center" style="width: 12%;">
      <a href='lap_km.php?orderby=nilai&pola=<?=$polabaru;?>'>Nilai</a>
    </td>
  </tr>
</thead>

<thead>
  <tr>
    <td style="text-align: center; width: 5%;">No.</td>
    <td style="width: 25%; text-align: center;">
     <a href='lap_km.php?orderby=nama<?=$search_query;?>'>Nama</a>
    </td>
    <td align="center">Memo</td>
    <td style="text-align: center;width: 10%;">
      <a href='lap_km.php?orderby=noref<?=$search_query;?>'>No. Referensi</a>
    </td>
    <td align="center" style="width: 10%;">
      <a href='lap_km.php?orderby=tgl<?=$search_query;?>'>Tanggal</a>
    </td>
    <td align="center" style="width: 12%;">
      <a href='lap_km.php?orderby=nilai<?=$search_query;?>'>Nilai</a>
    </td>
  </tr>
</thead>

更新代码

<div class="row">
  <div class="col-xs-12 col-md-7"></div>
  <div class="col-xs-6 col-md-5" style="padding-right: 40px; padding-bottom: 20px;">
    <header><h4 class="page-header"><b>Cari Berdasarkan Tanggal</b></h4></header>
    <form class="form-horizontal" method="GET" action="">
      <div class="form-group" style="padding-bottom: 0px; margin-bottom: 5px;">
        <label class="col-sm-3 control-label"><b>Dari Tanggal :</b></label>
        <div class="col-sm-5" style="padding-left: 0px;">
          <input type="date" class="form-control" name="tgl_awal">
        </div>
      </div>
      <div class="form-group" style="padding-bottom: 0px; margin-bottom: 5px;">
        <label class="col-sm-3 control-label"><b>Sampai Tanggal :</b></label>
        <div class="col-sm-5" style="padding-left: 0px;">
          <input type="date" class="form-control" name="tgl_akhir">
        </div>
      </div>
      <div class="form-group" style="padding-bottom: 0px; margin-bottom: 5px;">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-7" style="padding-left: 0px;">
          <input type="submit" name="submit" value="Cari">
        </div>
      </div>
    </form>
  </div>
</div>

  <!-- Database Fillter berdasarkan tanggal -->
  <?php
  include '../koneksi.php';

  $tgl_awal = $_GET['tgl_awal'];
  $tgl_akhir = $_GET['tgl_akhir'];
  $submit = $_GET['submit'];

  $search_query = "&tgl_awal=".$tgl_awal."&tgl_akhir=".$tgl_akhir."&submit=".$submit;

  $main_query = "SELECT * FROM item_aruskas i join aruskas a on i.id_aruskas=a.id ";
  $query_condition = "";
  /*Filter Tanggal*/
  if ($submit) {
    $query_condition = " WHERE noref like '%CI%' AND tgl between '$tgl_awal' AND '$tgl_akhir'  order by tgl DESC ";
  } else {
    $query_condition = " WHERE noref like '%CI%' order by noref DESC ";
  }

  /*sort data*/
  $pola = 'asc';
  $polabaru = 'asc';
  if(isset($_GET['orderby'])){
    $orderby = $_GET['orderby'];
    $pola = $_GET['pola'];

    $search_query = $search_query."&pola=".$pola;
    $query_condition = " order by $orderby $pola";

    if($pola == 'asc'){
      $polabaru='desc';
    } else {
      $polabaru='asc';
    }
  }

  /*Pagingging dan cek filter data*/
  error_reporting(0);
  $batas = 10;
  $halaman = isset($_GET['halaman']) ? $_GET['halaman'] : 1;
  $posisi = ($halaman-1)* $batas;

  $query1 = $main_query.$query_condition." limit $posisi,$batas";
  $result=mysqli_query($koneksi, $query1) or die(mysqli_error());
  $cek = mysqli_num_rows($result);

  if ($cek < 1) {
    ?>
    <div class="row">
      <div class="col-md-4"></div><div class="col-md-8"></div>
    </div>
    <div class="row">
      <div class="col-md-4"></div><div class="col-md-8"></div>
    </div>
    <div class="row">
      <div class="col-md-4"></div><div class="col-md-8"></div>
    </div>
    <div class="row">
      <div class="col-md-4"></div>
      <div class="col-md-4" style="text-align: center;"><h4><b>Data Tidak Ditemukan.<br><a href="lap_km.php">Kembali Laporan Kas Masuk</a></b></h4></div><div class="col-md-4"></div>
    </div>
    <?php } else {
      if (!isset($tgl_awal,$tgl_akhir)) {?>
        <!-- Baris Cetak Data -->
        <div class="row">
          <div class="col-xs-12 col-md-7" style="margin-left: 40px; padding-bottom: 20px;">
            <a href="cetak_lap_km.php" target="_blank">
              <button class="btn btn-default btn-lg"><img src="../img/print16.png" style="padding-right: 10px;"><b>Cetak </b></button>
            </a>
          </div>
          <div class="col-xs-6 col-md-5"></div>
        </div>
      <?php } else {?>
        <div class="row">
          <div class="col-xs-12 col-md-7" style="margin-left: 40px; padding-bottom: 20px;">
            <form method="POST" action="../laporan/cetak_lap_km.php" target="_blank" > 
              <input type="hidden" name="tgl_awal" value="<?php echo $tgl_awal; ?>">
              <input type="hidden" name="tgl_akhir" value="<?php echo $tgl_akhir; ?>">
              <button name="submit" class="btn btn-default "><img src="../img/print16.png" style="padding-right: 10px;"><b>Cetak </b></button>
            </form>
          </div>
          <div class="col-xs-6 col-md-5"></div>
        </div>
      <?php } ?>

      <div class="row">
        <div class="col-lg-12">
          <section class="panel">
            <header class="panel-heading" style="text-align: center;">
              <b>Laporan Kas Masuk</b>
            </header>

            <div class="table-responsive">  
              <table class="table table-striped table-advance table-hover table-bordered">
                <thead>
                  <tr>
                    <td style="text-align: center; width: 5%;">No.</td>
                    <td style="width: 25%; text-align: center;">
                     <a href='lap_km.php?orderby=nama<?=$search_query;?>'>Nama</a>
                    </td>
                    <td align="center">Memo</td>
                    <td style="text-align: center;width: 10%;">
                      <a href='lap_km.php?orderby=noref<?=$search_query;?>'>No. Referensi</a>
                    </td>
                    <td align="center" style="width: 10%;">
                      <a href='lap_km.php?orderby=tgl<?=$search_query;?>'>Tanggal</a>
                    </td>
                    <td align="center" style="width: 12%;">
                      <a href='lap_km.php?orderby=nilai<?=$search_query;?>'>Nilai</a>
                    </td>
                  </tr>
                </thead>
              <?php
              include '../koneksi.php';
              function format_rupiah($angka){
                $rupiah="Rp  " . number_format($angka,0,',','.');
                return $rupiah;
              }
              $no = $posisi + 0;
              while($record = mysqli_fetch_array($result)){
                $no++;
                ?>

                <tbody>
                  <tr>
                    <td style="text-align: center;"><?php echo $no; ?></td>
                    <td style="text-align: center;"><?php echo $record['nama'];?></td>
                    <td><?php echo $record['memo'];?></td>
                    <td style="text-align: center;"><?php echo $record['noref']; ?></td>
                    <td style="text-align: center;"><?php echo date('d-m-Y', strtotime($record['tgl']));?></td>
                    <td style="text-align: right;"><?php echo format_rupiah($record['nilai']);?></td>
                  </tr> 
                  <?php }
                  ?>                            
                </tbody>
              </table>

              <?php
              /*Menghitung Jumlah data*/

              $sql_paging = mysqli_query($koneksi,$query1);

              $jmldata = mysqli_num_rows($sql_paging);
              $jmlhalaman = ceil($jmldata / $batas);
              ?>

              <div class="row">  
                <div class="col-md-8" style="font-size: large; text-align: left; padding-left: 27px;">
                  <?php
                  echo "<b>Halaman : </b>";
                  for($i = 1; $i <= $jmlhalaman; $i++)
                    if($i != $halaman) {
                      echo "<a href=lap_km.php?halaman=$i.$search_query>$i</a>|";
                    } else {
                      echo "<b>$i</b>|";
                    }
                    mysqli_close($koneksi);?>
                  </div> 
                  <div class="col-md-4" style="font-size: large; text-align: right; padding-right: 28px;">
                    Jumlah data :<?php echo $jmldata;?>
                  </div> 
                </div>
                <?php } ?>
              </div>

我尽力给你想要的结果。看看。