/    Sign up×
Community /Pin to ProfileBookmark

Dropdown Sort With Combined Options

I’ve got an html table set up displaying MySQL data with a dropdown that allows you to choose whether to sort the data by Subgroup, Name or Partner. You can see it [URL=”http://myklwebs.com/myklmecha/database/g1sort.php”]here[/URL].
What I would like to do is add sort options like ‘sort by Subgroup then Name’ but I don’t know how to do this. I know what php code to use achieve this in an un-sortable page but I don’t know how to put that into a dropdown. I don’t even know what keywords to use to search online! Can this code be edited to add some combined sort options or do I need totally different code?

Here’s the code I’m using:

[code=php]
<?php include(‘config.php’);
$query = “SELECT * FROM TransformersLog WHERE Series = ‘Generation 1’ Order By Faction”;
$res = mysql_query($query);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Mykl’s Transformers Log</title>
<script src=”http://code.jquery.com/jquery-1.9.1.min.js”></script>
<script>
function dynamic_Select(dropdown) {
$.ajax({
type: “GET”,
url: ‘g1values.php’,
data: “ch=” + dropdown,
dataType: “html”,
success: function(html){ $(“#txtResult”).html(html); $(“#firstresult”).css(“display”, “none”); }
});
}
</script>
</head>

<body bgcolor=”#999999″>
<div style=”margin-left: 100px;”>
<form name=”ajaxlearning” id=”ajaxlearning”>
Sort By:<select name=”dropdown” id=”dropdown” onchange=”dynamic_Select(this.value)”>
<option value=””>Select Filter</option>
<option value=”Subgroup”>Subgroup</option>
<option value=”Name”>Name</option>
<option value=”Partner”>Partner</option>
</select>
</form>
<div id=”txtResult”>

</div>
<div id=”firstresult”>
<table id=”mytable” cellspacing=”0″ summary=”Mykl’s Transformers Log”>
<caption>Mykl’s Transformers Log</caption>
<tr>
<th>Year</th>
<th>Series</th>
<th>Faction</th>
<th>Subgroup</th>
<th>Name</th>
<th>Partner</th>
</tr>

<?php
while($row = mysql_fetch_array($res))
{
echo ‘<tr class=”alt”>
<td>’.$row[‘Year’].'</td>
<td>’.$row[‘Series’].'</td>
<td>’.$row[‘Faction’].'</td>
<td>’.$row[‘Subgroup’].'</td>
<td>’.$row[‘Name’].'</td>
<td>’.$row[‘Partner’].'</td>
</tr>’;
}
?>

</table></div>
</div>
</body>
</html>
[/code]

[code=php]
<?php
include(‘config.php’);
$order = $_GET[‘ch’];
if($order!=”)
{
$query = “SELECT * FROM TransformersLog WHERE Series = ‘Generation 1’ Order By Faction , $order ASC”;
}
else
{
$query = “SELECT * FROM TransformersLog WHERE Series = ‘Generation 1’ Order By Faction “;
}
$res = mysql_query($query);
?>
<table id=”mytable” cellspacing=”0″ summary=”Mykl’s Transformers Log”>
<caption>Mykl’s Transformers Log</caption>
<tr>
<th scope=”col”>Year</th>
<th scope=”col”>Series</th>
<th scope=”col”>Faction</th>
<th scope=”col”>Subgroup</th>
<th scope=”col”>Name</th>
<th scope=”col”>Partner</th>
</tr>

<?php
while($row = mysql_fetch_array($res))
{
echo ‘<tr>
<td>’.$row[‘Year’].'</td>
<td>’.$row[‘Series’].'</td>
<td>’.$row[‘Faction’].'</td>
<td>’.$row[‘Subgroup’].'</td>
<td>’.$row[‘Name’].'</td>
<td>’.$row[‘Partner’].'</td>
</tr>’;
}
?>

</table>
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJan 07.2015 — I would create the dropdown and a 'Sort' button that submits the page and let the server script re-generate it in the new sequence as decided by the dropdown choice.
Copy linkTweet thisAlerts:
@MyklauthorJan 07.2015 — Do you mean set up a page for each sort option so the dropdown sends you to a new page?
Copy linkTweet thisAlerts:
@ginerjmJan 07.2015 — Have a default sort option in your script when you first send the page. Then if the user wishes, he clicks the dropdown to select a diff order and hits the 'sort' button next to it. Your script receives this, sees that the 'sort' button was clicked and re-generates the page except this time with a diff order by clause in the query.
Copy linkTweet thisAlerts:
@MyklauthorJan 12.2015 — Thank you! That's enabled me to do what I want!
Copy linkTweet thisAlerts:
@ginerjmJan 12.2015 — Hth!
×

Success!

Help @Mykl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...