...

View Full Version : Resolved "While" data sorting?



weir-07
05-10-2009, 03:28 PM
Hey guys!

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


<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... :confused:

Fumigator
05-11-2009, 05:13 PM
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:



//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...;
}

weir-07
05-11-2009, 09:04 PM
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!

weir-07
05-12-2009, 01:03 PM
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:



//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;

//assign current Level to our previousLevel variable
$previousLevel = $row_members['cl'];
I would use something like;

//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...

Fumigator
05-12-2009, 04:52 PM
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:



$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...;
}

weir-07
05-12-2009, 05:21 PM
Fantastic work.

Thank you very very much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum