Jump to content

Php Ksort Help


blakogre

Recommended Posts

Thanks to all who've helped before: [a href=\"http://www.blakogre.com/epix/altdkp.php\" target=\"_blank\"]http://www.blakogre.com/epix/altdkp.php[/a] was the result. THANKS!
I've snipped the following code from a page which I've partially modified, but it's still not quite what I want: [a href=\"http://www.blakogre.com/guildviewer.php\" target=\"_blank\"]http://www.blakogre.com/guildviewer.php[/a]

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<tr>
<td><b>Name</b></td>
<td><b>Level</b></td>
<td><b>Rank</b></td>
<td><b>Note</b></td>
</tr>
OUTPUT;

ksort($classes); <-sorting by classes? This makes my major grouping?
foreach ($classes as $ckey => $class) { <-ckey -- that's defined here as teh $class variable
ksort($class); <- but... this sorts by class... so I'm not sure why there are 2. maybe created array? lost.

echo <<<OUTPUT
<tr>
<td colspan="4"><b>{$ckey}</b></td> <-- and this line is where ckey/class is inserted at each group
</tr>
OUTPUT;

foreach ($class as $member) {
$member['Name'] = htmlentities($member['Name']);
$member['Rank'] = htmlentities($member['Rank']);
$member['Note'] = htmlentities($member['Note']);
echo <<<OUTPUT
<tr>
<td>{$member['Name']}</td>
<td>{$member['Level']}</td>
<td>{$member['Rank']}</td>
<td>{$member['Note']}</td>
</tr>
OUTPUT;

}
}

echo <<<OUTPUT
</table>
OUTPUT;

?>[/quote]

Within each "Class" I want to sort by descending level as well: can I add another ksort in there? I've tried that, but I know my synatx is off, as I don't quite 100% understand what each line does. The pure HTML I get, it's the rest. I added my notes/what I think is going on. But again, I"m not a programmer, I come from an admin background.

Thx -- any help appreciated. My goals are simply for it to work, however, understanding it is great too. I love to learn by tweaking and playing and trying to get results I want.


So, it creates major class groupings, and then within that, ideally:

Sort by descending lvl
and within lvl
Sort by ascending name

Bob 60
Charles 60
Alan 55
Trudie 30
Jack 28
Kevin 28

(The data is stored in another PHP page -- page one "parser.php" parses a text file, which creates page 2 "data.php", which feeds into this page, "viewer.php")
Link to comment
Share on other sites

if the inforamtion is in a DB, use SQL DESC to have your array automatically sorted that way.

if you have the data in an array, do a numeric sort on the second element of the second dimension and reverse results. you may have to convert the value to an integer...
Link to comment
Share on other sites

Well, I added:

OUTPUT;

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]ksort($classes);
foreach ($classes as $ckey => $class) {
ksort($class);

echo <<<OUTPUT
<tr>
<td colspan="4"><b>{$ckey}</b></td>
</tr>
OUTPUT;
[b]sort($level);[/b]
foreach ($level as $member) {

[b]sort($level);[/b]
$member['Name'] = htmlentities($member['Name']);
$member['Rank'] = htmlentities($member['Rank']);
$member['Note'] = htmlentities($member['Note']);
echo <<<OUTPUT
<tr>
<td>{$member['Name']}</td>
<td>{$member['Level']}</td>
<td>{$member['Rank']}</td>
<td>{$member['Note']}</td>[/quote]

Of course, not working. I don't have 100% understanding or even close -- the "<--" comments in the first post are mine, trying to wrap my brain around it all.
Link to comment
Share on other sites

The initial array:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$classes = [b]array();[/b]

foreach ($guilddata['Members'] as $name => $info) {
if (empty($classes[$info['Class']])) {
$classes[$info['Class']] = array();
}
$classes[$info['Class']][$name] = $info;
}[/quote]

So -- I think in the () I need values I can reference for the sort? Tried putting things in -- got errors....
Link to comment
Share on other sites

I also tried adding a line like krsort($members['Level'])

and got:
Warning: krsort() expects parameter 1 to be array, string given in C:\Inetpub\boc\guildviewer.php on line 137

added right before the last echo<<<output above (ditched the sort/$level stuff)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.