2 回答
TA贡献1830条经验 获得超3个赞
该方法应该是这样的:您应该明确定义用户单击链接时将使用的排序顺序。例如,可以这样做:
$currentSort = isset($_GET['sort']) ? $_GET['sort'] : 'asc';
$newSort = $currentSort === 'asc' ? 'desc' : 'asc';
接下来$currentSort在查询中使用,并$newSort传递给链接:
$homework6 = $mysqli->query("SELECT * FROM homework6 WHERE manufacturer_hebrew LIKE '%$x%' order by $order $currentSort");
echo '<th colspan="1">'. '<a href="?order=id&sort=' . $newSort .'">x</a>'.'</th>';
TA贡献1806条经验 获得超8个赞
我认为,它会是这样的:(未测试)
if ($order == '') {
$order = id;
}
$sort = strpos($order, '-') ? 'desc' : 'asc';
$orderBy = str_replace('-', '', $order);
$homework6 = $mysqli->query("SELECT * FROM homework6 WHERE manufacturer_hebrew LIKE '%$x%' order by $orderBy $sort");
$rows = $homework6->fetch_all(MYSQLI_ASSOC);
$headColumns = [
['sort' => 'id', 'name' => 'x'],
['sort' => 'manufacturer_hebrew', 'name' => 'x'],
['sort' => 'manufacturer_english', 'name' => 'x'],
['sort' => 'Models_number', 'name' => 'x'],
['sort' => 'made_in', 'name' => 'x'],
];
echo '<div >';
echo '<table border= "1" >';
foreach ($headColumns as $headColumn) {
$direction = strpos($order, $headColumn['sort']) && strpos($order, '-') === false ? '-' : '';
echo '<th colspan="1"><a href="?order='.$direction.$headColumn['sort'].'">'.$headColumn['name'].'</a></th>';
}
foreach ($rows as $paz) {
...
- 2 回答
- 0 关注
- 122 浏览
添加回答
举报