help on Ordering MySql Query

Posted on Sunday 9th November 2008 at 07:17 PM
jambomb
jambomb's Avatar
Ok i have big problems trying to order my database, im making a hall of fame and so far its working but just not ordering the users level in order

this is my code

Code
$get_users = mysql_query("SELECT * FROM `members` ORDER BY `level` DESC);

$class1 = "#333333";
$class2 = "#1E1E1E";
$class = $class1;

$m = "1";

{
while($user = mysql_fetch_array($get_users))
{
echo ("<table width="75%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" width='20%' bgcolor="$class">$m</td>
<td align="center" width='24%' bgcolor="$class"><a href='members.php?user=$online[username]'>$user[id]</a></td>
<td align="center" width='36%'bgcolor="$class"><a href='gang/view_gang.php?view=$user[gangname]' class='tag'>$user[gangtag]</a> <a href='members.php?user=$user[username]'>$user[username]</a></td>
<td align="center" width='20%' bgcolor="$class">$user[level]</td>
</tr>

</table>");

if($class == $class1){
$class = $class2;
}else{
$class = $class1;
}

$m++;

}
}
Posted on Sunday 9th November 2008 at 11:27 PM
Matt
Matt's Avatar
you're going the wrong way about this.
you can also do this with for() and foreach() but

hers a function i use:
PHP Code
  1. <?php
  2. function row2color($id)
  3. {
  4. if ($id/2 == round($id/2)) $color = "#333333";
  5. else $color = "1E1E1E";
  6.  
  7. return $color;
  8. }


to use it:
PHP Code
  1. <?php
  2. $query = mysql_query("SELECT id FROM data");
  3. while ($result = mysql_fetch_array($query)) {
  4. $color = row2color($result[id]);
  5. echo "<div style="background-color: $color"></div>";
  6. }
  7. ?>
Posted on Sunday 9th November 2008 at 11:42 PM
jambomb
jambomb's Avatar
the colours fine mate, but its just the Ordering what isnt right doesnt DESC Propaly
Posted on Monday 10th November 2008 at 06:39 AM
Matt
Matt's Avatar
sorry man, actually, your code should be returning an error, since you have:

$get_users = mysql_query("SELECT * FROM `members` ORDER BY `level` DESC);

there should be a last " ad the end of desc.


$get_users = mysql_query("SELECT * FROM `members` ORDER BY `level` DESC");
try that.
Posted on Monday 10th November 2008 at 10:56 AM
jambomb
jambomb's Avatar
yeh it works man but just doesnt order! :S

go on www.crime-network.com and register mate then go to the city then Hall of Fame then you will know what im on about :)


thanks x
Posted on Tuesday 11th November 2008 at 01:28 PM
Dava
Dava's Avatar
change it to

$get_users = mysql_query("SELECT * FROM `members` ORDER BY `level` ASC");
Dava
Posted on Tuesday 11th November 2008 at 01:34 PM
jambomb
jambomb's Avatar
okay that works good coz on this page

https://www.crime-network.com/main/hall_of_fame.php?page=73

its ordered correct... but i need it so its highest first if u understand xD

thanks for help so far!!
Posted on Tuesday 11th November 2008 at 04:09 PM
Matt
Matt's Avatar
change ASC back to DESC just to see if anything happens, because that should work 100%, there's no reason why that shouldn't work.
Posted on Tuesday 11th November 2008 at 06:24 PM
jambomb
jambomb's Avatar
It kinda works.. but im level 9 and its missed me out lol and gone to 8, 5, 2 2 2 2 etc... :S

but it kinda working :S rofl dno why its fucked up tho
Posted on Tuesday 11th November 2008 at 11:16 PM
Dava
Dava's Avatar
it wouldnt just miss out a row u must be a lower level
Dava
Login or register to respond to this forum topic.