Jump to content


Photo

Php Ksort Help


  • Please log in to reply
4 replies to this topic

#1 blakogre

blakogre
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 27 June 2006 - 08:33 PM

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")

#2 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 27 June 2006 - 08:45 PM

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

#3 blakogre

blakogre
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 27 June 2006 - 09:54 PM

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;
sort($level);
foreach ($level as $member) {

sort($level);
$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.

#4 blakogre

blakogre
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 27 June 2006 - 10:25 PM

The initial array:

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

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


#5 blakogre

blakogre
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 27 June 2006 - 11:39 PM

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)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users