搜索表中的排序

时间:2018-12-05 09:08:38

标签: php sorting

我想重新创建我的表(最近仅按标题排序),使用按参数搜索输入。当我已经对查询进行排序时,该如何实现?

<?php
$id_prov = $_SESSION['id_prov'];
$level = $_SESSION['level'];
$search = $_POST['search'];

if($level == '1')
{

$sql="select idFormKuesionerPengelola, idprov, FormCode, kecamatan, kelurahan, provinsi, kabupaten from form_kuesioner_pengelola left join mst_province on form_kuesioner_pengelola.idprov = mst_province.id_tbl_mst_prov join mst_kab on form_kuesioner_pengelola.idkab = mst_kab.id_tbl_mst_kab WHERE mst_province.id_prov='$id_prov'";//and idkab = $search
//test
if ($_GET['sort'] == 'idform'){
    $sql .= " order by form_kuesioner_pengelola.FormCode";
}
elseif ($_GET['sort'] == 'kabupaten'){
    $sql .= " order by form_kuesioner_pengelola.idkab";
}
elseif ($_GET['sort'] == 'kecamatan'){
    $sql .= " order by form_kuesioner_pengelola.kecamatan";
}
elseif ($_GET['sort'] == 'kelurahan'){
    $sql .= " order by form_kuesioner_pengelola.kelurahan";
}
//test  
$rdata=mysqli_query($connect, $sql); //or die(mysqli_error($connect));

$no = '1';
echo "<br>";

echo "<table class='table1' border='1' cellpadding='10'>";

echo "<tr> <th>No.</th> <th><a href='list_kuesioner_pengelola.php?sort=idform'>ID Form</a></th> <th>Provinsi</th> <th><a href='list_kuesioner_pengelola.php?sort=kabupaten'>Kabupaten</a></th> <th><a href='list_kuesioner_pengelola.php?sort=kecamatan'>Kecamatan</a></th> <th><a href='list_kuesioner_pengelola.php?sort=kelurahan'>Kelurahan</a></th> <th>View</th> <th>Export</th> <th>Action</th>";

while($row = mysqli_fetch_array($rdata)) {
    echo "<tr>";
    echo '<td>' . $no . '</td>';
    echo '<td>' . $row['FormCode'] . '</td>';
    echo '<td>' . $row['provinsi'] . '</td>';
    echo '<td>' . $row['kabupaten'] . '</td>';
    echo '<td>' . $row['kecamatan'] . '</td>';
    echo '<td>' . $row['kelurahan'] . '</td>';

    echo '<td><a href="kuesioner_pengelola_view.php?id='. $row['idFormKuesionerPengelola'].'&id2='.$row['FormCode'].'">View</a></td>';
    echo '<td><a href="export_kuesioner_pengelola.php?id='. $row['idFormKuesionerPengelola'].'&id2='.$row['FormCode'].'">Export</a></td>';
    echo '<td><a href="kuesioner_pengelola_edit.php?id='. $row['idFormKuesionerPengelola'].'">Edit</a></td>';

    echo "</tr>";
    $no++;
}

echo "</table>";

}

那么我如何使它们成为一体?当我已经搜索到并可以对结果进行排序时。

谢谢

1 个答案:

答案 0 :(得分:0)

如果要搜索字段kecamatan,则进行搜索,然后在排序之前添加代码

if($search != ''){
    $sql .= "WHERE kecamatan LIKE %.$search.% ";
}