我有一点问题,我看不到解决方案。应该很容易。 我想制作一个做类似事情的liitle脚本。 当我点击:
类 - >转到classes.php?sort = class-desc
然后再次上课 - >转到classes.php?sort = class-asc
然后再次上课 - >转到classes.php?sort = class-desc
等等......
if (isset($_GET['sort'])) {
$item = $_GET['sort'];
$pos = strpos($item, '-');
$crit = substr($item, 0, $pos);
$ordine = substr($item, $pos+1);
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=class-asc>Class</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=avg_grades-asc>General</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=abscences-asc>Nr Abscences</a></div>";
echo "<div style='float:left;width: 180px;'><a href=classes.php?sort=nr_students-asc>Nr Students</a></div><br><br>";
}else{
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=class-desc>Class</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=avg_grades-desc>General</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=absente-desc>Nr Abscences</a></div>";
echo "<div style='float:left;width: 180px;'><a href=classes.php?sort=nr_students-desc>Nr Students</a></div><br><br>";
$item = 'avg_grades';
}
对不起,我知道这很简单,但我根本不知道把循环放在哪里。
谢谢
答案 0 :(得分:1)
您需要另一个if else
语句来检查$ordine
变量:
if (isset($_GET['sort'])) {
$item = $_GET['sort'];
$sort = explode('-', $item);
$crit = $sort[0];
$ordine = $sort[1];
if ($ordine === 'desc') {
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=class-asc>Class</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=avg_grades-asc>General</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=abscences-asc>Nr Abscences</a></div>";
echo "<div style='float:left; width: 180px;'><a href=classes.php?sort=nr_students-asc>Nr Students</a></div><br><br>";
} else {
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=class-desc>Class</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=avg_grades-desc>General</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=absente-desc>Nr Abscences</a></div>";
echo "<div style='float:left; width: 180px;'><a href=classes.php?sort=nr_students-desc>Nr Students</a></div><br><br>";
$item = 'avg_grades';
}
}
或者您可以通过在$ordine
代码a
中应用href
变量来缩短时间:
if (isset($_GET['sort'])) {
$item = $_GET['sort'];
$sort = explode('-', $item);
$crit = $sort[0];
$ordine = $sort[1] === 'desc' ? 'asc' : 'desc';
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=class-$ordine>Class</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=avg_grades-$ordine>General</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=absente-$ordine>Nr Abscences</a></div>";
echo "<div style='float:left; width: 180px;'><a href=classes.php?sort=nr_students-$ordine>Nr Students</a></div><br><br>";
}
答案 1 :(得分:0)
试试这段代码,我删除了代码重复:
if (isset($_GET['sort'])) {
$item = $_GET['sort'];
$submitted_direction_exploded = explode('-',$item);
$submitted_direction = isset($submitted_direction_exploded[1]) ? $submitted_direction_exploded[1] : '';
$direction = ($direction=='desc')?'asc' : $submitted_direction;
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=class-$direction>Class</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=avg_grades-$direction>General</a></div>";
echo "<div style='float:left; width: 150px;'><a href=classes.php?sort=abscences-$direction>Nr Abscences</a></div>";
echo "<div style='float:left;width: 180px;'><a href=classes.php?sort=nr_students-$direction>Nr Students</a></div><br><br>";
}