如何使“其他”使desc

时间:2019-08-03 12:43:49

标签: php mysql

我正在“ php my admin”中建立一个该位置的表,我首先在“ th”之一的asc上单击了该位置,现在我试图在第二次单击中进行desc。 / p>

嗯..我不记得很抱歉...

efs-utils

现在只有“ asc”有效。

2 个答案:

答案 0 :(得分:0)

方法应该是这样的:您应该明确定义用户单击链接时将使用的排序顺序。例如,可以这样做:

$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>';

答案 1 :(得分:0)

我认为,可能是这样的:(未经测试)

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) {
...