当表列在PHP中排序时,搜索参数将丢失

时间:2014-08-09 09:01:39

标签: php mysql sorting pagination

我正在使用PHP中的分页进行搜索和排序。这些工作正常。但在搜索后我是否对表头列进行排序后,搜索参数将丢失,表返回其默认视图。这是我的PHP代码 -

<?php
include("includes/db-config.php");
include("includes/pagination.php");

// table name
$table = "tb_categories";

// order by column names array
$orderby_arr = array("category_id" => "Category ID", "category_name" => "Category Name");

// sorting order ASC or DESC array
$sort_arr = array("asc", "desc");

// set default values
$orderby = isset($_GET['orderby']) ? $_GET['orderby'] : "category_name";
$sort = isset($_GET['sort']) ? $_GET['sort'] : "asc";

// get opposite $sort_order value for producing toggle links in the table heading
if($sort == "asc")
{
    $sort_order = "desc";
}
else
{
    $sort_order = "asc";
}

// if search request comes
if(isset($_GET['btnsearch']))
{
    $search = isset($_GET['txtsearch']) ? $_GET['txtsearch'] : '';
    if($search != '')
    {
        // form a simple LIKE '%search_term%' comparison
        $where_clause = sprintf("WHERE `category_name` LIKE '%%%s%%'", mysql_real_escape_string($search));
    }
    else
    {
        $where_clause = '';
    }
}
// default
else
{
    $search = '';
    $where_clause = '';
}

// get total number of records
$query = "SELECT COUNT(*) FROM $table $where_clause";
$rs = mysql_query($query);
list($total_records) = mysql_fetch_row($rs);

// number of records per page
$records_per_page = 3;

// total number of pages
$total_pages = ceil($total_records/$records_per_page);


if(isset($_GET['page']) && is_numeric($_GET['page']))
{
    if($_GET['page'] >= $total_pages)
    {
        $page = $total_pages;
    }
    else if($_GET['page'] <= 0)
    {
        $page = 1;
    }
    else
    {
        $page = (int) $_GET['page'];
    }
}
else
{
    // default page on loading
    $page = 1;
}

// first row starts at 0
$start_row = ($page - 1) * $records_per_page;
$query = "SELECT * FROM $table $where_clause ORDER BY $orderby $sort LIMIT $start_row, $records_per_page";
$rs = mysql_query($query);

?>

<form method="get" action="">
Type Keyword: <input type="text" name="txtsearch" value="<?php echo $search; ?>" />
<input type="submit" name="btnsearch" value="Search" />
</form>


<table width="50%" align="center" border="1" cellpadding="5" cellspacing="0">
    <tr>
        <th>Sr. No.</th>
        <?php
        // dynamically produce table columns
        foreach($orderby_arr as $key => $value)
        {
            echo "<th><a href='?orderby=" . $key . "&sort=" . $sort_order . "'>" . $value . "</a></th>";
        }
        ?>
        <th>Category Description</th>
    </tr>

        <?php
        // fetch rows
        $srno = 1;
        while($row = mysql_fetch_array($rs))
        {
        ?>
        <tr>
            <td><?php echo $srno; ?></td>
            <td><?php echo $row['category_id']; ?></td>
            <td><?php echo $row['category_name']; ?></td>
            <td><?php echo $row['category_desc']; ?></td>
        </tr>
        <?php
        $srno++;
        }
        ?>

    <tr>
        <td colspan="4">
        <div id="pagination">
        <?php echo getPagination($page, $total_pages); ?>
        </div>
        </td>
    </tr>
</table>

在搜索关键字“cricket”时,我有以下网址 -

/?txtsearch=cricket&btnsearch=Search

但表格列标题(类别ID或类别名称)仅显示以下内容 -

/?orderby=category_id&sort=desc

那么我如何保留或保留搜索参数以便它附加到列标题。但是在默认视图中,因为没有搜索参数,所以列标题将只有第二个URL。因为我也有分页因此搜索参数以及表列标题排序参数应该附加页面ID。我认为一旦搜索参数开始使用排序,那么分页应该没有任何问题,因为我在分页函数中使用以下代码 -

// only modify the 'page number' value, all others left as is
$_GET['page'] = $i;
$q = http_build_query($_GET, '', '&amp;');
$pagination .= " <a href='?$q'>$i</a> ";

$ i的值来自for循环和分页正常工作;没问题。

我们怎么能这样做?感谢我所有的朋友们。

1 个答案:

答案 0 :(得分:0)

您必须像这样重建查询字符串 -

$txtsearch = $search;
$btnsearch = $_GET['btnsearch'];
$orderby = 'category_name';
$order = $sort_order;


    $query_string = array(
                        "txtsearch" => $txtsearch,
                        "btnsearch" => $btnsearch,
                        "orderby" => $orderby,
                        "order" => $order,
                    );


$sort_link = "?" . http_build_query($query_string, "", "&");