我有一个分页列表,向页面显示10个项目,但我想为某人提供更改列表顺序的选项,例如,按性别或年龄。
分页工作,在排序列表方面,到目前为止我有:
$orderby = ($_GET['orderby'] == 'gender')? $_GET['orderby'] : 'age';
if ($result = $mysqli->query("SELECT * FROM people ORDER BY $orderby"))
有2个基本链接:性别|年龄
echo "<a href='{$_SERVER['PHP_SELF']}?orderby=gender'>gender</a> | <a href='{$_SERVER['PHP_SELF']}?orderby=age'>age</a>";
当我点击其中一个链接时,它会根据需要更改列表的顺序,但是当我转到分页的另一个页面时,它不起作用。
有人可以告诉我解决此问题的最佳方法。感谢。
答案 0 :(得分:0)
尝试我的功能genPagnation()
,请参阅下面的代码:
<?
$totalPage = 23;
$currentPage = isset($_GET['p']) && is_numeric($_GET['p']) && $_GET['p'] >= 1 ? (int)$_GET['p'] : 1;
$params = array();
$devices = array();
if( isset($_POST['order']) || isset($_GET['order']) ){
$params['order'] = isset($_POST['order']) ? $_POST['order'] : $_GET['order'];
}
if( isset($_POST['device']) || isset($_GET['device']) ){
$params['device'] = isset($_POST['device']) ? $_POST['device'] : $_GET['device'];
$devices = $params['device'];
}
for( $i = 1; $i <= $totalPage; $i++ ){
if( $i == $currentPage )
print(" <b><u>{$i}</u></b> ");
else
print(" <a href='" . genPagnation($i, $params) . "'>{$i}</a> ");
}
print("<p><hr /></p>\n\n");
function genPagnation($p, $param = array(), $pname = null, $pvalue = null){
$param['p'] = $p;
if( !empty($pname) && !empty($pvalue) )
$param[ $pname ] = $pvalue;
$query = http_build_query($param);
$query = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $query);
return '?' . $query;
}
?>
<p>
Order By:
<a href='<?=genPagnation($currentPage, $params, "order", "male")?>'>Male</a> |
<a href='<?=genPagnation($currentPage, $params, "order", "female")?>'>Female</a>
</p>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
<p>
Filtering:
<label><input type="checkbox" name="device[]" value="iphone" <? if( !empty($devices) && in_array('iphone', $devices) ){ echo 'checked="checked"'; } ?> />iPhone</label>
<label><input type="checkbox" name="device[]" value="ipad" <? if( !empty($devices) && in_array('ipad', $devices) ){ echo 'checked="checked"'; } ?> />iPad</label>
<label><input type="checkbox" name="device[]" value="ipod" <? if( !empty($devices) && in_array('ipod', $devices) ){ echo 'checked="checked"'; } ?> />iPod</label>
<label><input type="checkbox" name="device[]" value="android" <? if( !empty($devices) && in_array('android', $devices) ){ echo 'checked="checked"'; } ?> />Android</label>
</p>
<p>
Ordering:
<label><input type="radio" name="order" value="male" <? if( isset($_REQUEST['order']) && $_REQUEST['order'] == 'male'){ echo 'checked="checked"'; } ?> />Male</label>
<label><input type="radio" name="order" value="female" <? if( isset($_REQUEST['order']) && $_REQUEST['order'] == 'female'){ echo 'checked="checked"'; } ?> />Female</label>
</p>
<p><input type="submit" value="search"></p>
</form>