Thread: Sorting a SUM
View Single Post
Old 01-18-2013, 02:29 PM   PM User | #19
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You have two questions here.
The first is by fetching values by get. Yes that would be trivial to do; you simply need to check if your provided value is available, and if so, you then go ahead and fetch it. That's simply a matter of structure like so:
PHP Code:
if (isse($_GET['Team_ID']))
{
    
// All of this code you have now goes here.
    
if (get_magic_quotes_gpc())
    {
        
$_GET['Team_ID'] = stripslashes($_GET['Team_ID']);
    }
    
$team_id mysql_real_escape_string($_GET['Team_ID']);
    
$sql 'SELECT Pos, FName, LName, ROUND((SUM(H)/SUM(AB)),3) AS AVG '
        
' FROM retired_batters '
        
' WHERE Team = \'' $team_id '\' '
        
' GROUP BY Pos, FName, LName'
        
' ORDER BY AVG DESC '
        
' LIMIT 10';
    
// continue with the rest of your code


I don't know what kind of results that AVG would be, but what you describe to what is currently in the code would indicate that AVG would be a fractional value: AVG=0.885 for example.
In the printf code:
PHP Code:
    printf('<tr class="%s">
        <td align="right">%d.</td>
        <td>%s %s</td>
        <td align="right">%d</td>'
, ...); 
Those replacements are %s = string, and %d = integer. If you have 0.885 for example, casting that to an integer would result in 0. What you do is use %f to indicate that it is a double value instead, and you can autoround it with the modifiers. So instead of %d you can use %0.3f which would show three significant digits and the 0.
That is a guess as to what the issue is since it sounds like the AVG pulls the correct results, its simply display as int.
This is one reason why printf is often not liked by people, but I personally feel that the ability to modify the output structure and perform argument location swapping trumps that of accidentally providing the incorrect datatype.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
bayken37 (01-18-2013)