SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot DBallerz01's Avatar
    Join Date
    Jan 2004
    Location
    CT
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using sum in while

    PHP Code:
    $calcs $db->sql_query("select crew_name, crew_id ,sum(user_power) as
    sumpower, count(*) as numusers, sum(user_power) / count(*) as avgpowerfrom
    nuke_users where user_crew = '
    $crew_name' ORDER BY numusers DESC LIMIT
    0,5"
    ); 

    $queryResult mysql_query($calcs); 
    $row4 mysql_fetch_assoc($queryResult); 

    while(list(
    $crew_id$crew_name$numusers) = sql_fetch_row($queryResult$dbi))

                                                                    
    $numusers number_format($numusers); 
                    
    $content .= "<b><font
    color=\"
    $numtextcolor\">$topcrew.</font></b>&nbsp; <a
    href=\"crewprofile.php?user_id=
    $user_id\">$username</a> <b>$numusers</b><br>
    "

    $topcrew++; 

    ?> 

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Umm, you don't provide any description for your code but let me have a stab and post back how it goes.
    PHP Code:
    $sql "select crew_name
                , crew_id
                , sum(user_power) as sumpower
                , count(*) as numusers
                , sum(user_power) / count(*) as avgpower
            from
                nuke_users
            where
                user_crew = '
    {$crew_name}'
            order by
                numusers desc
            limit 0, 5" 
    ;
    $rs $db->sql_query($sql);

    while ( 
    $row $db->sql_fetch_row($rs) ) {
        
    $numusers number_format($row['numusers']);
        
        
    $content .= '<strong><span style="color: ' $numtextcolor ';">' $topcrew '</span></strong>
                     &nbsp;
                     <a href="crewprofile.php?user_id=' 
    $user_id '">' $username '</a>
                     &nbsp;
                     <strong>' 
    $numusers '</strong>' ;
        
    $topcrew++;

    You don't show us where any of the variables used within the loop come from and I don't know what you're trying to do (hint: it would be helpful if you told us!) but my guess is that it won't work the way you are trying.

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    This is what I think you're hopoing to achieve, correct me if I'm wrong though. At least a little closer...
    PHP Code:
    $sql "select crew_name
                , crew_id
                , sum(user_power) as sumpower
                , count(*) as numusers
                , sum(user_power) / count(*) as avgpower
            from
                nuke_users
            where
                user_crew = '
    {$crew_name}'
            order by
                sumpower desc
            limit 0, 5" 
    ;
    $rs $db->sql_query($sql);

    $rank 0;
    while ( 
    $row $db->sql_fetch_row($rs) ) {
        
    $numusers number_format($row['numusers']);
        
        
    $content .= '<strong>' $rank '.</strong>
                     &nbsp;
                     <a href="crewprofile.php?user_id=' 
    $row['crew_id'] . '">' $row['crew_name'] . '</a>
                     &nbsp;
                     <strong>' 
    $numusers ' / ' $row['sumpower'] . '</strong>' ;
        
    $rank++;


  4. #4
    SitePoint Zealot DBallerz01's Avatar
    Join Date
    Jan 2004
    Location
    CT
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, Ill try that

  5. #5
    SitePoint Zealot DBallerz01's Avatar
    Join Date
    Jan 2004
    Location
    CT
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i get the following error parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'
    on this line
    PHP Code:
    <?php
     
    <a href="crewprofile.php?user_id=' . $row['crew_id'] . '">' . $row['crew_name'] . '</a
    ?>

  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 
       
    echo '<a href="crewprofile.php?user_id=' $row['crew_id'] . '">' $row['crew_name'] . '</a>';
      
    ?>
    And about the query: You need a group by:

    Code:
     $sql = "select crew_name 
      			, crew_id 
      			, sum(user_power) as sumpower 
      			, count(*) as numusers 
      			, sum(user_power) / count(*) as avgpower 
      		from 
      			nuke_users 
      		where 
      			user_crew = '{$crew_name}' 
      		order by 
      			sumpower desc 
       	    group by
       	            crew_name, crew_id
      		limit 0, 5" ;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •