这更像是一个查询而不是一个问题.我想为我的mysql数据库结果创建动态排序.我目前用查询输出结果.我想创建一个2链接,它将按所选链接对结果进行排序,再次单击并链接ASC或DSEC结果.
我一直在寻找,但无法找到正确的方法来做这件事.
我甚至下载了一个框架,看看这是如何完成的,但没有成功.
示例如下:
TITLE TOTAL
这听起来很简单,我无法在网上找到动态的例子.
其他任何人发现谷歌提供更多的结果到过时和论坛比实际有用的页面?
即使你按去年和相关性排序.希望有人能给我一些建议,谢谢
解决方法:
<?php
// build the basis for the query
$sql = '
SELECT
`id`,
`title`,
`total`
FROM
`my_table`
';
// check for sort field
$sort_by = isset($_GET['s']) ? $_GET['s'] : false;
// validate the sort field (avoid Bobby Tables!) and provide default
switch ($sort_by) {
case 'title':
case 'id':
case 'total':
break;
default:
$sort_by = 'id';
}
$sql .= ' ORDER BY '.$sort_by.' ';
// get the direction, or use the default
$direction = isset($_GET['d']) ? $_GET['d'] : false;
if ($direction != 'ASC' && $direction != 'DESC')
$direction = 'DESC';
$sql .= $direction;
// execute query, get results
$res = mysql_query($sql);
$results = array();
if ($res) {
while ($r = mysql_fetch_assoc($res)) {
$results[] = $r;
}
}
// used in table heading to indicate sort direciton
$sort_arrow = ($direction == 'ASC' ? '<img src="up_arrow.png" />' : '<img src="down_arrow.png" />');
// used to build urls to reverse the current sort direction
$reverse_direction = ($direction == 'DESC' ? 'ASC' : 'DESC');
?>
<table>
<thead>
<th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>">
<a href="myscript.php?s=id&d=<?php echo $reverse_direction; ?>">ID</a>
<?php echo $sort_by == 'id' ? $sort_arrow : ''; ?>
</th>
<th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>">
<a href="myscript.php?s=title&d=<?php echo $reverse_direction; ?>">Title</a>
<?php echo $sort_by == 'title' ? $sort_arrow : ''; ?>
</th>
<th scope="col" class="<?php echo $sort_by == 'id' ? 'sortColumn' : ''; ?>">
<a href="myscript.php?s=total&d=<?php echo $reverse_direction; ?>">Total</a>
<?php echo $sort_by == 'total' ? $sort_arrow : ''; ?>
</th>
</thead>
<tbody>
<?php
if (count($results) > 0) {
foreach ($results as $r) {
print '<tr>';
print '<th scope="row">'.$r['id'].'</th>';
print '<td>'.$r['title'].'</td>';
print '<td>'.$r['total'].'</td>';
print '</tr>';
}
} else {
print '<tr><td colspan=3>No results found</td></tr>';
}
?>
</tbody>
</table>
标签:php,html5,mysql 来源: https://codeday.me/bug/20190929/1831785.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。