this is more of a query 开发者_JS百科than a problem. I would like to create dynamic sorting for my mysql database results. I currently output the results with a query. I would like to create a 2 links which would sort the results by that selected link, click again and the link ASC or DSEC the results.
I have been searching but am unable to find the right way about doing this.
I even downloaded a framework to see how this is done but no to success. the example is like follows:
TITLE TOTAL
As simple as this sounds I am unable to find a dynamic example online.
ANyone else finding google supplies more results to outdated and forums than actual helpful pages? Even if you sort by last year and relevance. Hope someone could give me some advice on this, thanks
<?php
$order = ($_GET['order'] == 'asc') ? 'asc' : 'desc';
$sql = "SELECT .... FROM ... WHERE ... ORDER BY TITLE $order";
mysql_query($sql) or die(mysql_error());
...
$new_order = ($order == 'asc' ) ? 'desc' : 'asc';
?>
<table>
<thead><tr>
<th><a href="scriptname.php?order=<?php echo $new_order ?>">TITLE</a></th>
<th>Total</th>
</tr></thead>
etc....
<?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>
this is how I did it in C#, you can use session instead of viewstate rest of it will be same.
if (Convert.ToString(ViewState["sortField"]) == SortExpression){
if (ViewState["sortDir"].ToString() == "ASC")
{ ViewState["sortDir"] = "DESC"; }
else
{ ViewState["sortDir"] = "ASC"; }
}
else
{ ViewState["sortDir"] = "ASC"; }
ViewState["sortField"] = SortExpression;
You could take the sort variable from the URL with $_GET['sort'] eg. site.php?sort=ASC and pass it to the query, remember about sanity checks!
精彩评论