如何制作“else”来制作desc

我正在“php my admin”中建立一个该位置的表,我在第一次点击其中一个“th”的asc时做到了这一点,现在我试图在第二次点击中制作desc..有什么想法吗?


嗯...很多我不记得对不起...


if($order == '' ) {


    $order= id;


}


$homework6 = $mysqli->query("SELECT * FROM homework6 WHERE manufacturer_hebrew LIKE '%$x%' order by $order asc"); 

$rows = $homework6->fetch_all(MYSQLI_ASSOC); 




echo '<div >';

echo '<table border= "1" >';

echo '<th colspan="1">'. '<a href="?order=id">x</a>' .'</th>';

echo '<th colspan="1">'. '<a href="?order=manufacturer_hebrew">x</a>' .'</th>';

echo '<th colspan="1">'. '<a href="?order=manufacturer_english">x</a>' .'</th>';

echo '<th colspan="1">'. '<a href="?order=Models_number">x</a>' .'</th>';

echo '<th colspan="1">'. '<a href="?order=made_in">x</a>' .'</th>';

foreach ($rows as $paz) 

{  if ($paz['Models_number'] < 2 )  {

    $color = "bg-danger";

}

elseif ($paz['Models_number'] < 10){

    $color = "bg-warning";

}   

 elseif ($paz['Models_number'] < 15){

    $color = "bg-success";

}


else {$color = "bg-primary";} 

echo '<tr class='.$color.'>';

echo '<td >' .$paz['id']. '</td>';

echo '<td>' .$paz['manufacturer_hebrew']. '</td>';

echo '<td>' .$paz['manufacturer_english']. '</td>';    

echo '<td>' .$paz['Models_number']. '</td>';

echo '<td >' .$paz['made_in']. '</td>';

echo '</tr>';

        }

echo '</table>';

echo '</div>';

现在只是'asc' 工作。


红糖糍粑
浏览 123回答 2
2回答

牛魔王的故事

该方法应该是这样的:您应该明确定义用户单击链接时将使用的排序顺序。例如,可以这样做:$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");&nbsp;echo '<th colspan="1">'. '<a href="?order=id&sort=' . $newSort .'">x</a>'.'</th>';

慕森卡

我认为,它会是这样的:(未测试)if ($order == '') {&nbsp; &nbsp; $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 = [&nbsp; &nbsp; ['sort' => 'id', 'name' => 'x'],&nbsp; &nbsp; ['sort' => 'manufacturer_hebrew', 'name' => 'x'],&nbsp; &nbsp; ['sort' => 'manufacturer_english', 'name' => 'x'],&nbsp; &nbsp; ['sort' => 'Models_number', 'name' => 'x'],&nbsp; &nbsp; ['sort' => 'made_in', 'name' => 'x'],];echo '<div >';echo '<table border= "1" >';foreach ($headColumns as $headColumn) {&nbsp; &nbsp; $direction = strpos($order, $headColumn['sort']) && strpos($order, '-') === false ? '-' : '';&nbsp; &nbsp; echo '<th colspan="1"><a href="?order='.$direction.$headColumn['sort'].'">'.$headColumn['name'].'</a></th>';}foreach ($rows as $paz) {...
打开App,查看更多内容
随时随地看视频慕课网APP