Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Defining an order of table results
Old 04-27-2009, 12:54 AM Defining an order of table results
Skilled Talker

Posts: 90
Trades: 0
Hi all,

I'm very inexperienced with PHP -- so I was hoping to get help with something I'm trying to modify. I had a programmer design a script for me which handles standings for a league and then also calls these standings for view. The thing is, when the table is populated, it goes in alphabetical (or at least ID) order -- whereas I'd like it to be ordered like a real standings table might, with it going from greatest number of wins to most points scored, etc.

You can see the current setup example here:
http://www.fasports.com/manager/view..._id=25&week=-1

The code on the page with the table being generated is:
Code:
<div>
        <p style="margin-top: 0; margin-bottom: 0" align="center"><font face="Arial" size="2">
        <br/>
        </font><font face="Arial" size="5">
        <b>Current Standings</b></font><font face="Arial" size="2"><br/>
        <br/>
        </font></p>
        <div align="center">
        <table cellpadding="2" width="750" border="2" bordercolor="#003366">
            <tr>
                  <th style="width:236px;" bgcolor="#003366">
                <p style="margin-top: 0; margin-bottom: 0" align="left">
                <font color="#FFFFFF" face="Arial" size="2">Team</font></th>
                <th style="width:60px;text-align:center" bgcolor="#003366">
                <p style="margin-top: 0; margin-bottom: 0">
                <font color="#FFFFFF" face="Arial" size="2">W</font></th>
                <th style="text-align:center;" bgcolor="#003366">
                <p style="margin-top: 0; margin-bottom: 0">
                <font color="#FFFFFF" face="Arial" size="2">L</font></th>
                <th style="text-align:center;" bgcolor="#003366" width="119">
                <p style="margin-top: 0; margin-bottom: 0">
                <font color="#FFFFFF" face="Arial" size="2">PTS Scored</font></th>
                <th style="text-align:center;" bgcolor="#003366" width="143">
                <p style="margin-top: 0; margin-bottom: 0">
                <font color="#FFFFFF" face="Arial" size="2">PTS Given Up</font></th>
                <th style="text-align:center;" bgcolor="#003366" width="77">
                <p style="margin-top: 0; margin-bottom: 0">
                <font color="#FFFFFF" face="Arial" size="2">Difference</font></th>
           </tr>
    <?            
                $teams = $db->db_select("select * from team where location_id = $location_id and league_id = $league_id and division_id = $division_id order by team");
                while ( $team = mysql_fetch_object($teams) ) {
                    $standing = calculate_standing($team->team_id);                    
    ?>
            <tr>
                <td>
        <font face="Arial" size="2"><p align="left" style="margin-top: 0; margin-bottom: 0"><?=get_team_name($team->team_id)?></p></font></td>
                <td align="center">
        <p style="margin-top: 0; margin-bottom: 0">
        <font face="Arial" size="2"><?=$standing['win']?></font></td>
                <td align="center">
        <p style="margin-top: 0; margin-bottom: 0">
        <font face="Arial" size="2"><?=$standing['loose']?></font></td>
                <td align="center" width="119">
        <p style="margin-top: 0; margin-bottom: 0">
        <font face="Arial" size="2"><?=$standing['pts_scored']?></font></td>
                <td align="center" width="143">
        <p style="margin-top: 0; margin-bottom: 0">
        <font face="Arial" size="2"><?=$standing['pts_given_up']?></font></td>
                <td align="center" width="77">
        <p style="margin-top: 0; margin-bottom: 0">
        <font face="Arial" size="2"><?=$standing['difference']?></font></td>
            </tr>
    <?                    
                }
    ?>
        </table>
        </div>
    </div>
And the function is defined:
PHP Code:
    function calculate_standing $team_id ,$week = -) {
        
$result = array("win"=>0,"loose"=>0,"pts_scored"=>0,"pts_given_up"=>0,"difference"=>0);
        
$db = new cls_db();
        
$sql "select sum(win) as win, sum(loose) as loose, sum(pts_scored) as pts_scored, sum(pts_given_up) as pts_given_up, (sum(pts_scored)-sum(pts_given_up)) as difference from standing inner join game on standing.game_id = game.game_id where (game.week = $week or $week = -1) and standing.team_id = $team_id";
        
$rs $db->db_select($sql);
        if ( 
$row mysql_fetch_object($rs) ) {
            
$result['win'] = $row->win;
            
$result['loose'] = $row->loose;
            
$result['pts_scored'] = $row->pts_scored;
            
$result['pts_given_up'] = $row->pts_given_up;
            
$result['difference'] = $row->difference;
        }
        foreach(
$result as $k=>$v) {
            if (  empty(
$v) ) {
                
$result[$k] = 0;    
            }
        }
        return 
$result;        
    } 
Hopefully I've identified the right pieces of code to edit... and if anyone can help I'd greatly appreciate it!

Thanks in advance!
msaz87 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-27-2009, 04:21 AM Re: Defining an order of table results
nayes84's Avatar
Extreme Talker

Latest Blog Post:
Difference between ASP And JSP
Posts: 232
Name: John
Location: Tokyo
Trades: 0
try making the sql query which is in the fourth line in your php code like that
PHP Code:
$sql "select sum(win) as win, sum(loose) as loose, sum(pts_scored) as pts_scored, sum(pts_given_up) as pts_given_up, (sum(pts_scored)-sum(pts_given_up)) as difference from standing inner join game on standing.game_id = game.game_id where (game.week = $week or $week = -1) and standing.team_id = $team_id order by win desc"
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

if(I'm("Helpful")) Add_Talkupation("nayes84");

Last edited by nayes84; 04-27-2009 at 04:22 AM..
nayes84 is offline
Reply With Quote
View Public Profile
 
Old 04-27-2009, 04:26 AM Re: Defining an order of table results
Skilled Talker

Posts: 90
Trades: 0
Didn't seem to do anything differently...

PHP Code:
    function calculate_standing $team_id ,$week = -) {
        
$result = array("win"=>0,"loose"=>0,"pts_scored"=>0,"pts_given_up"=>0,"difference"=>0);
        
$db = new cls_db();
        
$sql "select sum(win) as win, sum(loose) as loose, sum(pts_scored) as pts_scored, sum(pts_given_up) as pts_given_up, (sum(pts_scored)-sum(pts_given_up)) as difference from standing inner join game on standing.game_id = game.game_id where (game.week = $week or $week = -1) and standing.team_id = $team_id order by win desc";  
        
$rs $db->db_select($sql);
        if ( 
$row mysql_fetch_object($rs) ) {
            
$result['win'] = $row->win;
            
$result['loose'] = $row->loose;
            
$result['pts_scored'] = $row->pts_scored;
            
$result['pts_given_up'] = $row->pts_given_up;
            
$result['difference'] = $row->difference;
        }
        foreach(
$result as $k=>$v) {
            if (  empty(
$v) ) {
                
$result[$k] = 0;    
            }
        }
        return 
$result;        
    } 
I replaced the query with the one you said and the order remained the same.

Did I have to make a change to the code when the data is called on the actual page?
msaz87 is offline
Reply With Quote
View Public Profile
 
Old 04-27-2009, 04:45 AM Re: Defining an order of table results
nayes84's Avatar
Extreme Talker

Latest Blog Post:
Difference between ASP And JSP
Posts: 232
Name: John
Location: Tokyo
Trades: 0
this should work! may be because the name win is for the field and for sum(win)
try using different name for sum(win)
ex:
PHP Code:
$sql "select sum(win) as totalwin, sum(loose) as loose, sum(pts_scored) as pts_scored, sum(pts_given_up) as pts_given_up, (sum(pts_scored)-sum(pts_given_up)) as difference from standing inner join game on standing.game_id = game.game_id where (game.week = $week or $week = -1) and standing.team_id = $team_id order by totalwin"
Quote:
Did I have to make a change to the code when the data is called on the actual page?
didn't understand what you mean!
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

if(I'm("Helpful")) Add_Talkupation("nayes84");
nayes84 is offline
Reply With Quote
View Public Profile
 
Old 04-27-2009, 04:58 AM Re: Defining an order of table results
Skilled Talker

Posts: 90
Trades: 0
That new piece of code didn't change the order -- but it did remove all instances of wins (turned any existing to 0's).

What I meant by changing code in the other area was...

The output is on one page, while the function is defined on another. I didn't know if I had to modify anything on the actual page that shows the data, or just the sql query in the function like you had given me. There's this string of php on the output page:

PHP Code:
    <?            
                $teams 
$db->db_select("select * from team where location_id = $location_id and league_id = $league_id and division_id = $division_id order by team");
                while ( 
$team mysql_fetch_object($teams) ) {
                    
$standing calculate_standing($team->team_id);                    
    
?>
So I wasn't sure if the order might be defined in there or if something else needed to be changed to go along with the new query.

Thanks for your help!
msaz87 is offline
Reply With Quote
View Public Profile
 
Old 04-29-2009, 12:53 AM Re: Defining an order of table results
Novice Talker

Posts: 11
Trades: 0
thanks for sharing
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
ishudan is offline
Reply With Quote
View Public Profile
 
Old 04-29-2009, 02:11 AM Re: Defining an order of table results
Skilled Talker

Posts: 90
Trades: 0
Quote:
thanks for sharing
Thanks for the help.

I tried modifying this code:
PHP Code:
    <?            
                $teams 
$db->db_select("select * from team where location_id = $location_id and league_id = $league_id and division_id = $division_id order by team");
                while ( 
$team mysql_fetch_object($teams) ) {
                    
$standing calculate_standing($team->team_id);                    
    
?>
To:
PHP Code:
    <?            
                $teams 
$db->db_select("select * from team where location_id = $location_id and league_id = $league_id and division_id = $division_id order by win");
                while ( 
$team mysql_fetch_object($teams) ) {
                    
$standing calculate_standing($team->team_id);                    
    
?>
But that came back with an error.

I'm not really sure what to try next -- any suggestions?

Thanks!
msaz87 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Defining an order of table results
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32578 seconds with 12 queries