Jump to content

help sorting in total order DESC


EchoFool

Recommended Posts

Hey guys, i have a while loop calculating the total number of users with types of weapons in my game.. how ever i could not add an ORDER BY or at least i don't think i could... but i would like the most popular used weapon to be at the top and the worst at the bottom, in other words DESC based on how many users have it.....though i do not believe mysql can do this ORDER BY, so how can it be done via php?

 

This is my script:

<?php
$Select = mysql_query("SELECT WeaponID,Name FROM weapons ORDER BY WeaponID DESC")
	Or die(mysql_error());
While($row = mysql_fetch_assoc($Select)){
	$WeaponID = $row['WeaponID'];
	$WeaponName = $row['Name'];
$Total = 0;
$Count = mysql_query("SELECT WeaponID FROM userweapons WHERE WeaponID='$WeaponID'")
	Or die(mysql_error());
While($row2 = mysql_fetch_assoc($Count)){
$Total = $Total + 1;
}	
Echo $WeaponName.'['.$WeaponID.'] - '.$Total.' users';
Echo '<br><br>';
}
}
?>

 

Hope you can point me in the right direction :)

Thank you!

Link to comment
Share on other sites

Think what you're looking for is using a GROUP BY with your ORDER BY

 

I will try it and see what happens.

 

Edit:

I'm afraid that didn't work as you can see here:

wep46[46] - 0 users

wep45[45] - 22 users

wep44[44] - 1 users

 

As you can from above, it should be in order of:

Wep 45

Wep 44

Wep 46

 

Based on how many people own each weapon.

Link to comment
Share on other sites

Well heres all 3 of my attempts with different approaches of which all 3 failed to echo in the correct order:

 

This was attempt 1:

<?php
$Select = mysql_query("SELECT WeaponID,Name FROM weapons ORDER BY WeaponID DESC")
	Or die(mysql_error());
While($row = mysql_fetch_assoc($Select)){
	$WeaponID = $row['WeaponID'];
	$WeaponName = $row['Name'];
$Total = 0;
$Count = mysql_query("SELECT WeaponID FROM userweapons WHERE WeaponID='$WeaponID'")
	Or die(mysql_error());
While($row2 = mysql_fetch_assoc($Count)){
$Total = $Total + 1;
}	
Echo $WeaponName.'['.$WeaponID.'] - '.$Total.' users';
Echo '<br><br>';
}
}
?>

 

 

This was attempt 2 which you suggested:

 

<?php
$Select = mysql_query("SELECT WeaponID,Name FROM weapons GROUP BY WeaponID ORDER BY WeaponID DESC")
	Or die(mysql_error());
While($row = mysql_fetch_assoc($Select)){
	$WeaponID = $row['WeaponID'];
	$WeaponName = $row['Name'];
$Total = 0;
$Count = mysql_query("SELECT WeaponID FROM userweapons WHERE WeaponID='$WeaponID'")
	Or die(mysql_error());
While($row2 = mysql_fetch_assoc($Count)){
$Total = $Total + 1;
}	
Echo $WeaponName.'['.$WeaponID.'] - '.$Total.' users';
Echo '<br><br>';
}
}
?>

 

This was attempt 3:

 

<?php
$Select = mysql_query("SELECT WeaponID,Name FROM weapons GROUP BY WeaponID ORDER BY WeaponID DESC")
	Or die(mysql_error());
While($row = mysql_fetch_assoc($Select)){
	$WeaponID = $row['WeaponID'];
	$WeaponName = $row['Name'];
$Total = 0;
$Count = mysql_query("SELECT COUNT(WeaponID) AS WeaponCount FROM userweapons WHERE WeaponID='$WeaponID' ORDER BY WeaponCount")
	Or die(mysql_error());
While($row2 = mysql_fetch_assoc($Count)){	
Echo $WeaponName.'['.$WeaponID.'] - '.$row2['WeaponCount'].' users';
Echo '<br><br>';
	}
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.