我正在使用以下逻辑对从SQL数据库中读取的数据进行排序 -
<a href='string_functions.php?sort=desc'>Sort By Type</a>
<?php
if( isset( $_GET['sort'] ) and $_GET['sort'] == 'desc') {
$query = "SELECT * FROM `function list` ORDER BY `function type` DESC";
} else {
#Read from Database
$query = "SELECT * FROM `function list`";
}
$data_buffer = $conn -> query($query);
?>
然后我使用 $data_buffer
来显示表格 function list
我想做 -
<a href='string_functions.php?sort=desc'>Sort By Type</a>
切换 ASC 和 DESC 操作。是否可以这样做?
我知道我可以使用两个链接,如 -
<a href='string_functions.php?sort=asc'>Sort By Type</a>
<a href='string_functions.php?sort=desc'>Sort By Type</a>
每个用于不同的操作,但是只使用一个链接是否可以这样做?
答案 0 :(得分:0)
我没有测试过此代码,但它会对您有所帮助 并且我使用了会话,因为它会帮助您进行分页, 但它不是强制性的,您也可以根据需要使用简单变量
<?php
if( isset( $_GET['sort'] ) and $_GET['sort'] == 'desc')
{
$_SESSION['sort_type']='DESC'; //you can set using normal variable also instead of SESSION
$_SESSION['sort_arrow']='v';
$_SESSION['sort_link']='asc';
}
else
{
$_SESSION['sort_type']='ASC';
$_SESSION['sort_arrow']='^';
$_SESSION['sort_link']='desc';
}
$query = "SELECT * FROM `function list` ORDER BY `function type` '".$_SESSION['sort_type']."'";
?>
<a href="string_functions.php?sort=<?php echo $_SESSION['sort_link'];?>">
<?php echo $_SESSION['sort_arrow'];?>
</a>
答案 1 :(得分:0)
在CSS的帮助下,我能够做到的代码很少
<强> PHP:强>
<?php
if(isset( $_GET['sort'] ) and $_GET['sort'] == 'desc')
$hide = 'hide';
else
$hide = '';
?>
<a href='string_functions.php?sort=desc' class="<?php echo $hide;?>">Sort By Type(DESC)</a>
<?php
if( isset( $_GET['sort'] ) and $_GET['sort'] == 'desc' ) {
$query = "SELECT * FROM `function list` ORDER BY `function type` DESC";
?>
<a href='string_functions.php?sort=asc'>Sort By Type(ASC)</a>
<?php
} else if(isset( $_GET['sort'] ) and $_GET['sort'] == 'asc' ) { ?>
<?php
#Read from Database
$query = "SELECT * FROM `function list` ORDER BY `function type` ASC";
} else {
$query = "SELECT * FROM `function list`";
}
$data_buffer = $conn -> query($query);
?>
<强> CSS:强>
<style>
.hide {
display: none;
}
</style>
答案 2 :(得分:-1)
试试这个:
<a href='string_functions.php?sort=desc'>Sort By Type</a>
替换为:
if(isset( $_GET['sort'] ) and $_GET['sort'] == 'desc')
echo "<a href='string_functions.php?sort=asc'>ASC</a>";
else
echo "<a href='string_functions.php?sort=desc'>DESC</a>";