Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts

    "While" data sorting?

    Hey guys!

    I have the following bit of code for a "Members List" type thing at the moment.

    PHP Code:
    <table width="100%" border="0" cellspacing="2" cellpadding="1" style="border:#036 solid 1px;">
            <tr>
              <td bgcolor="#003366"><strong>Rank</strong></td>
              <td bgcolor="#003366"><strong>Handle</strong></td>
              <td bgcolor="#003366"><strong>Affiliation</strong></td>
              <td bgcolor="#003366"><strong>Status</strong></td>
              </tr>
            <?php 
    mysql_select_db
    ($database_STC$STC);
    $query_members "SELECT accounts.*,rank FROM accounts join ranks on accounts.cl = ranks.cl WHERE accounts.cl > '0' ORDER BY ranks.cl DESC";
    $members mysql_query($query_members$STC) or die(mysql_error());
    $totalRows_members mysql_num_rows($members);  

    while (
    $row_members mysql_fetch_assoc($members)) { ?><tr>
              
              <td width="190" align="left" valign="middle" style="border:#069 solid 1px;"><?php echo ucwords($row_members['rank']); ?> (CL<?php echo $row_members['cl']; ?>)</td>
              <td align="left" valign="middle" style="border:#069 solid 1px;">
                <a href=profile.php?username=<?php echo $row_members['username']; ?> target=_blank style="text-decoration:none; color:#39F"><?php echo ucwords($row_members['handle']); ?></a>
                </td>
              <td width="100" align="left" valign="middle" style="border:#069 solid 1px;"><?php echo ucwords($row_members['affiliation']); ?></td>
              <td width="100" align="left" valign="middle" style="border:#069 solid 1px;">
                <font color="<?   if ($row_members['status'] == "active") { echo "#33CC33"; } 
                                    if (
    $row_members['status'] == "registered") { echo "#6699FF"; }
                                  if (
    $row_members['status'] == "confirmed") { echo "#6666CC"; }
                                  if (
    $row_members['status'] == "suspended") { echo "#CC0000"; }
                                  if (
    $row_members['status'] == "on leave") { echo "#999999"; }
                                  if (
    $row_members['status'] == "extended leave") { echo "#666666"; } ?>">
                  <?php echo ucwords($row_members['status']); ?></font></td>
              
              </tr><?php ?>
    And would like to apply a "Clearance Level" filter without having to create separate database entries and just using the data already pulled.

    What i mean by this is instead of the current set up where all the data just displays i would like to have headings of:

    Clearance Level 19:
    - List members
    Clearance Level 18:
    - List members
    Does anybody know if this is possible?
    I had a fiddle with it but i'm not too sure if it can even be done...
    Last edited by weir-07; 05-11-2009 at 09:04 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If I understand you right, you want to subdivide the output, creating a header for every clearance level. Since it appears your query is sorting by clearance level (I guess that's what your column named "cl" represents), this is a simple matter of breaking the output on level. To break means to interrupt the normal detail output every time the level changes from the previous level. Example:

    PHP Code:
    //initialize $previousLevel so the first result will produce a header
    $previousLevel "";
    while (
    $row_members mysql_fetch_assoc($members)) {
        
    //check to see if this current row's Level is different than previous row's Level
        
    if ($row_members['cl'] != $previousLevel) {
            echo 
    "Clearance Level {$row_members['cl']}<br />\n";
        }
        
    //assign current Level to our previousLevel variable
        
    $previousLevel $row_members['cl'];
        
    //normal row output here
        
    echo blah .... blah .... blah...;


  • Users who have thanked Fumigator for this post:

    weir-07 (05-11-2009)

  • #3
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Ooo thank you very very much!! Didn't realise it was quite that simple :O I have a tendancy to over think things :P Thank you!

  • #4
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Fumigator View Post
    If I understand you right, you want to subdivide the output, creating a header for every clearance level. Since it appears your query is sorting by clearance level (I guess that's what your column named "cl" represents), this is a simple matter of breaking the output on level. To break means to interrupt the normal detail output every time the level changes from the previous level. Example:

    PHP Code:
    //initialize $previousLevel so the first result will produce a header
    $previousLevel "";
    while (
    $row_members mysql_fetch_assoc($members)) {
        
    //check to see if this current row's Level is different than previous row's Level
        
    if ($row_members['cl'] != $previousLevel) {
            echo 
    "Clearance Level {$row_members['cl']}<br />\n";
        }
        
    //assign current Level to our previousLevel variable
        
    $previousLevel $row_members['cl'];
        
    //normal row output here
        
    echo blah .... blah .... blah...;

    Hmm - might it be possible to put in the headings for CL's that do not appear (as there are no user accounts at that level...) and just put "No users at this level" underneath perhaps?

    Was thinking instead of;
    PHP Code:
     //assign current Level to our previousLevel variable
        
    $previousLevel $row_members['cl']; 
    I would use something like;
    PHP Code:
     //assign current Level to our previousLevel variable
        
    $previousLevel = ($previousLevel-1); 
    But im not too sure how to check if any users are actually at that level...
    Last edited by weir-07; 05-12-2009 at 01:17 PM.

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I don't think you want to mess with the $previousLevel variable for this, but rather keep a counter going independant of $previousLevel. This counter will handle the missing Levels. Throw a little loop in there that catches $counter up to the Level coming from the database-- something like this:

    PHP Code:
    $previousLevel "";
    $counter 20//the highest level
    while ($row_members mysql_fetch_assoc($members)) {
        
    //check to see if this current row's Level is different than previous row's Level
        
    if ($row_members['cl'] != $previousLevel) {
            
    //Iterate header output until the $counter variable matches the current CL
            
    for ($i $counter$i >= $row_members['cl']; $i--) {
                echo 
    "Clearance Level $i<br />\n";
                if (
    $i $row_members['cl']) {
                    echo 
    "No users at this clearance level.<br />\n";
                }
            }
            
    $counter $i;
        }
        
    //assign current Level to our previousLevel variable
        
    $previousLevel $row_members['cl'];
        
    //normal row output here
        
    echo blah .... blah .... blah...;

    Last edited by Fumigator; 05-12-2009 at 05:47 PM.

  • Users who have thanked Fumigator for this post:

    weir-07 (05-12-2009)

  • #6
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Fantastic work.

    Thank you very very much!


  •  

    Posting Permissions

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