简体   繁体   中英

how to make table sorting and searching by

I a newbie with this language and I do not know how to make it. So in here I have script like this :

<?php
include_once "library/inc.sesadmin.php";
include_once "library/inc.library.php";

$row = 20;
$hal = isset($_GET['hal']) ? $_GET['hal'] : 0;
$pageSql = "SELECT * FROM kategori";
$pageQry = mysql_query($pageSql, $koneksidb) or die ("error paging: ".mysql_error());
$jml     = mysql_num_rows($pageQry);
$max     = ceil($jml/$row);
?>
<table width="700" border="0" cellpadding="2" cellspacing="1" class="table-border">
  <tr>
    <td colspan="2" align="right"><h1><b>DATA KATEGORI</b></h1></td>
  </tr>
  <tr>
    <td colspan="2"><a href="?page=Add-Kategori" target="_self"><img src="images/btn_add_data2.png" height="25" border="0" /></a></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><table class="table-list" width="100%" border="0" cellspacing="1" cellpadding="2">
      <tr>
        <th width="32" align="center"><b>No</b></th>
        <th width="93" align="center"><b>Kode</b></th>
        <th width="444"><b>Nama Kategori </b></th>
        <th width="93" align="center"><b>Jumlah Barang</b> </th>
        <td width="47" align="center" bgcolor="#CCCCCC"><b>Ubah</b></td>
        <td width="52" align="center" bgcolor="#CCCCCC"><b>Hapus</b></td>
      </tr>
      <?php
    $kategoriSql = "SELECT kategori.*, (SELECT COUNT(*) FROM barang WHERE barang.kd_kategori=kategori.kd_kategori) As qty_barang
                    FROM kategori ORDER BY kd_kategori ASC LIMIT $hal, $row";
    $kategoriQry = mysql_query($kategoriSql, $koneksidb)  or die ("Query kategori salah : ".mysql_error());
    $nomor  = 0; 
    while ($kategoriRow = mysql_fetch_array($kategoriQry)) {
    $nomor++;
    $Kode = $kategoriRow['kd_kategori'];
    ?>
      <tr>
        <td align="center"><b><?php echo $nomor; ?></b></td>
        <td align="center"><b><?php echo $kategoriRow['kd_kategori']; ?></b></td>
        <td><?php echo $kategoriRow['nm_kategori']; ?></td>
        <td align="center"><?php echo $kategoriRow['qty_barang']; ?></td>
        <td align="center"><a href="?page=Edit-Kategori&amp;Kode=<?php echo $Kode; ?>" target="_self" alt="Edit Data"><img src="images/btn_edit.png" width="20" height="20" border="0" /></a></td>
        <td align="center"><a href="?page=Delete-Kategori&amp;Kode=<?php echo $Kode; ?>" target="_self" alt="Delete Data" onclick="return confirm('ANDA YAKIN AKAN MENGHAPUS DATA PENTING INI ... ?')"><img src="images/btn_delete.png" width="20" height="20"  border="0"  alt="Delete Data" /></a></td>
      </tr>
      <?php } ?>
    </table></td>
  </tr>
  <tr>
    <td><b>Jumlah Data :</b> <?php echo $jml; ?> </td>
    <td align="right"><b>Halaman ke :</b> 
    <?php
    for ($h = 1; $h <= $max; $h++) {
        $list[$h] = $row * $h - $row;
        echo " <a href='?page=Data-Kategori&hal=$list[$h]'>$h</a> ";
    }
    ?>
    </td>
  </tr>
</table>

I want to make table sorting and search by at this table, but I do not know how to make it. Can anyone help me for this simple project?? I really appreciate your big help in here..

I'm not going to give you the exact code because it's not how we do things in SO, However I will share with you the main idea of how to accomplish it.

For each column add a link, for example:

<td><a href='?orderby=id'>ID</a></td>
<td><a href='?orderby=name'>Name</a></td>

When id and name are fields in the table (SQL & html).

Before the query you check if there's a LEGIT value for orderby ( $_GET['orderby'] ) and if so add it to the ORDER BY ... in the query.

Please notice that you're using mysql_* which is not safe (especially in this case), consider using mysqli_* or PDO (you can google it)

EDIT You can add &direction= to the columns' links to set the direction ( DESC / ASC )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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