gpt4 book ai didi

php - 从 mySQL 回显 HTML 时如何在递增和递减行之间切换

转载 作者:行者123 更新时间:2023-11-29 04:30:21 25 4
gpt4 key购买 nike

有人好心地帮助我使用 ORDER BY 显示表格中的行。然后我意识到如果需要,我也想单击相同的链接来减少。我尝试设置一个条件,但它没有像我预期的那样工作:

<?php
$orderBy = array('type', 'description', 'recorded_date', 'added_date');

$order = 'type';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}


$sql="SELECT * FROM $tbl_name ORDER BY " .$order;

?>
<table>
<tr>
<th>
<?php
if($order == 'type'){
?>
<a href="?orderBy=type_dec">Type:</a>
<?php
} else{
?>
<a href="?orderBy=type">Type:</a>
<?php
}
?>
<a href="?orderBy=type">Type:</a>
</th>
<th>
Description:
</th>
<th>
<a href="?orderBy=recorded_date">Recorded Date:</a>
</th>
<th>
<a href="?orderBy=added_date">Added Date:</a>
</th>
</tr>

我知道我没有完整的代码,但令我惊讶的是,尽管如此,“类型”链接在上面的示例中出现了两次。我认为其中一个会根据 $order 的值出现。

另外,我有一种感觉,我尝试通过递减来实现这一点的方式可以用更好的方式来完成。请随时以这种方式分享!

最佳答案

这应该有效:

$order = 'type';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}

if(isset($_GET['desc'])) {
$order .= ' DESC';
} else {
$order .= ' ASC';
}


$sql="SELECT * FROM $tbl_name ORDER BY " .$order;

?>
<table>
<tr>
<th>
<?php
if($order == 'type' && empty($_GET['desc'])){
?>
<a href="?orderBy=type&desc=1">Type:</a>
<?php
} else{
?>
<a href="?orderBy=type">Type:</a>
<?php
}
?>
</th>
<th>
Description:
</th>
<th>
<a href="?orderBy=recorded_date">Recorded Date:</a>
</th>
<th>
<a href="?orderBy=added_date">Added Date:</a>
</th>
</tr>
</table>

关于php - 从 mySQL 回显 HTML 时如何在递增和递减行之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3490384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com