/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Sort By

Hi,

Just wondering if someone could help me with this, i want to add 2 options.

Sort Username by ascending or descending in alphabetical order, So if its ascending it would show A’s First or descending it would should Z’s First.

Sort Points by ascending or descending in numerical order, So if its ascending it would show points with 0 First or descending it would should points XXXX First.

I would like the options to be a selection box, with no submit button, so it would be onChange=”document.formid.submit() in the select tag.

Heres my code so far,

[code=php]

<BR>

<font color=’FFFFFF’ size=’-1′ face=’Tahoma’><strong>List Users</strong></font><BR>
<font color=’FFFFFF’ size=’-2′ face=’Tahoma’><strong>All Current Users Registered</strong></font><BR>
<hr color=”#FFFFFF” align=”left” width=”80%”>

<table width=”50%” border=”0″>
<tr>
<td>

<?

$sql =”SELECT * FROM bb_users”;

$result = @mysql_query($sql,$connection) or die(mysql_error());

while ($sql = mysql_fetch_object($result))

{

$points = $sql -> user_points;
$username = $sql -> username;
$email = $sql -> user_email;

?>

<table width=”100%” border=”0″>
<tr>
<td width=”50%” align=”right”><font color=’FFFFFF’ size=’-1′ face=’Tahoma’><strong>Username: </strong></font></td>
<td width=”50%” align=”left”><font color=’FFFFFF’ size=’-1′ face=’Tahoma’><strong><? echo $username; ?></strong></font></td>
</tr><tr>
<td width=”50%” align=”right”><font color=’FFFFFF’ size=’-1′ face=’Tahoma’><strong>Email Address: </strong></font></td>
<td width=”50%” align=”left”><font color=’FFFFFF’ size=’-1′ face=’Tahoma’><strong><? echo $email; ?></strong></font></td>
</tr><tr>
<td width=”50%” align=”right”><font color=’FFFFFF’ size=’-1′ face=’Tahoma’><strong>Points: </strong></font></td>
<td width=”50%” align=”left”><font color=’FFFFFF’ size=’-1′ face=’Tahoma’><strong><? echo $points; ?></strong></font></td>
</tr>
</table>
<br>

<?
}

?>

</td>
</tr>
</table>
[/code]

Thanks for any help
Aaron

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@SlappyTheFishMay 16.2006 — Hello.

Maybe you could use a sort function like this:

http://www.php.net/sort

But if you're using large datasets I'd be inclined to get your MySQL server to do the sorting as the tables should contain indexes which will speed things up dramatically. So just do two SQL Queries with something like 'ORDER BY user_points ASC;'.
Copy linkTweet thisAlerts:
@LiL_aaronauthorMay 16.2006 — Thanks for the reply... would i use the switch function for this tho?

[code=php]
<?
switch ($sort)
{
case "DESC":
$sql ="SELECT * FROM bb_users ORDER BY user_points DESC";

$result = @mysql_query($sql,$connection) or die(mysql_error());

while ($sql = mysql_fetch_object($result))

{

$points = $sql -> user_points;
$username = $sql -> username;
$email = $sql -> user_email;

?>

<table width="100%" border="0">
<tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Username: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $username; ?></strong></font></td>
</tr><tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Email Address: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $email; ?></strong></font></td>
</tr><tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Points: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $points; ?></strong></font></td>
</tr>
</table>
<br>

<?
}

break;
case "ASC":
$sql ="SELECT * FROM bb_users ORDER BY user_points ASC";

$result = @mysql_query($sql,$connection) or die(mysql_error());

while ($sql = mysql_fetch_object($result))

{

$points = $sql -> user_points;
$username = $sql -> username;
$email = $sql -> user_email;

?>

<table width="100%" border="0">
<tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Username: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $username; ?></strong></font></td>
</tr><tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Email Address: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $email; ?></strong></font></td>
</tr><tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Points: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $points; ?></strong></font></td>
</tr>
</table>
<br>

<?
}

break;
default:
$sql ="SELECT * FROM bb_users";

$result = @mysql_query($sql,$connection) or die(mysql_error());

while ($sql = mysql_fetch_object($result))

{

$points = $sql -> user_points;
$username = $sql -> username;
$email = $sql -> user_email;

?>

<table width="100%" border="0">
<tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Username: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $username; ?></strong></font></td>
</tr><tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Email Address: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $email; ?></strong></font></td>
</tr><tr>
<td width="50%" align="right"><font color='FFFFFF' size='-1' face='Tahoma'><strong>Points: </strong></font></td>
<td width="50%" align="left"><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $points; ?></strong></font></td>
</tr>
</table>
<br>

<?
}

}

[/code]


My only last question would be how would i get the selection box, to grab either case?

Thanks

Aaron

?>
Copy linkTweet thisAlerts:
@SlappyTheFishMay 16.2006 — Well, after your SQL statement the rest of the code is the same in all three cases, so I'd do a little if statement, something like this:

[code=php]
if ($sort == 'DESC')
{
$sql ="SELECT * FROM bb_users ORDER BY user_points DESC";
$ascSelectBoxState = null;
$descSelectBoxState = 'checked';
}
elseif ($sort == 'ASC')
{
$sql ="SELECT * FROM bb_users ORDER BY user_points ASC";
$ascSelectBoxState = 'checked';
$descSelectBoxState = null;
}
else
$sql ="SELECT * FROM bb_users";
$ascSelectBoxState = null;
$descSelectBoxState = null;
}
[/code]


and then put in the rest of your code. For the checkboxes (actually, probably best to use radio buttons to avoid both being selected), just put in the values $ascSelectBoxState and $descSelectBoxState respectively to each of the options:

[code=php]
$sortSelection = <<<HTML

<input type="radio" name="sort" value="asc" {$ascSelectBoxState}>
<input type="radio" name="sort" value="desc" {$descSelectBoxState}>

HTML;

[/code]


And then just echo out $sortSelection wherever you need the selection boxes.

Also, don't forget that register globals should be off so you'll need to use $_POST or $_GET to read in the submitted value of the selection box.

Hope this helps.
Copy linkTweet thisAlerts:
@LiL_aaronauthorMay 16.2006 — Hi, Thanks for that so far! ?

Only thing is now it just does the else and not the other 2 funtions, i think i may have the form post wrong.

[code=php]
<font color='FFFFFF' size='-1' face='Tahoma'><strong>List Users</strong></font><BR>
<font color='FFFFFF' size='-2' face='Tahoma'><strong>All Current Users Registered</strong></font><BR>
<hr color='#FFFFFF' align='left' width='80%'>

<form action="<? $_SERVER['PHP_SELF'] ?>" method="post">
<font color='FFFFFF' size='-1' face='Tahoma'><strong>Sort List By:</strong></font>
&nbsp;&nbsp;&nbsp;&nbsp;
<select size='1' name='sortwith'>
<option name='sortwith' value='username'>Username</option>
<option name='sortwith' value='user_points'>Points</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<font color='FFFFFF' size='-1' face='Tahoma'><strong>Ascending </strong></font><input type="radio" name="sort" value="asc" <? $ascSelectBoxState ?>>
<font color='FFFFFF' size='-1' face='Tahoma'><strong>Descending </strong></font><input type="radio" name="sort" value="desc" <? $descSelectBoxState ?>>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value=" Sort " name="B5"><BR>
</form>


<table width='50%' border='0'>
<tr>
<td>

<?

$_POST['sort'] = $sort;
$_POST['sortwith'] = $sortwith;

if ($sort == 'desc') {

$sql ='SELECT * FROM bb_users ORDER BY ".$sortwith." DESC';
$ascSelectBoxState = null;
$descSelectBoxState = 'checked';

} elseif ($sort == 'asc') {

$sql ='SELECT * FROM bb_users ORDER BY ".$sortwith." ASC';
$ascSelectBoxState = 'checked';
$descSelectBoxState = null;

} else {

$sql ='SELECT * FROM bb_users';
$ascSelectBoxState = null;
$descSelectBoxState = null;

}

$result = @mysql_query($sql,$connection) or die(mysql_error());

while ($sql = mysql_fetch_object($result))

{

$points = $sql -> user_points;
$username = $sql -> username;
$email = $sql -> user_email;

?>

<table width='100%' border='0'>
<tr>
<td width='50%' align='right'><font color='FFFFFF' size='-1' face='Tahoma'><strong>Username: </strong></font></td>
<td width='50%' align='left'><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $username; ?></strong></font></td>
</tr><tr>
<td width='50%' align='right'><font color='FFFFFF' size='-1' face='Tahoma'><strong>Email Address: </strong></font></td>
<td width='50%' align='left'><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $email; ?></strong></font></td>
</tr><tr>
<td width='50%' align='right'><font color='FFFFFF' size='-1' face='Tahoma'><strong>Points: </strong></font></td>
<td width='50%' align='left'><font color='FFFFFF' size='-1' face='Tahoma'><strong><? echo $points; ?></strong></font></td>
</tr>
</table>
<br>
<?

}

?>

</td>
</tr>
</table>
[/code]


the page it self is admin.php?id=userList

So i am not sure what i should post it too.

Cheers,

Aaron
Copy linkTweet thisAlerts:
@SlappyTheFishMay 16.2006 — On first glance I notice that you've got your single and double quotes mixed up slightly. In the firs two IF clauses ,where you have:

[code=php]
$sql ='SELECT * FROM bb_users ORDER BY ".$sortwith." DESC';
[/code]


its should be:

[code=php]
$sql ='SELECT * FROM bb_users ORDER BY '.$sortwith.' DESC';
[/code]


Does that fix things?
Copy linkTweet thisAlerts:
@LiL_aaronauthorMay 16.2006 — wow! something that simple ?

i normally do $sql=" stuff here ";

maybe thats why i got mixed up with the double quotes, but hey thanks and all working!

Aaron
Copy linkTweet thisAlerts:
@SlappyTheFishMay 16.2006 — Yeah, I know what you mean, it can be confusing. It's tempting to use double quotes for everything but the advantage with single quotes is that the PHP engine doesn't need to parse them so it slightly speeds things up a little bit.

Anyway, glad I could help!
Copy linkTweet thisAlerts:
@susansheminMay 16.2006 — I'm not sure if I am misunderstanding your question, but you sort in the database.

$sql ="SELECT * FROM bb_users ASC ORDER BY username";
×

Success!

Help @LiL_aaron 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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