mdnghtblue Posted July 28, 2007 Share Posted July 28, 2007 Still kind of a newbie to this stuff....I've been trying to figure this out for a while. This is what I want to do: 1. pull a query from the database, "ORDER BY disabled ASC, networth DESC" 2. check every user's info from the query, to see if they have a certain "ability" enabled 3. if they do, perform a calculation on networth, and put it back into the array/query (not sure what to call it at this point) 4. reorder the array/query by "disabled ascending, networth descending" This has been driving me nuts, I feel like it's got to be possible. I'm running into a couple problems, which is due to lack of complete knowledge of how PHP works. =/ First, the "calculations" I'm performing on those certain users aren't being stored, and I'm pretty sure the sorting isn't working either. I tried to use array_multisort, but I'm not sure it's working. $users = dbquery("SELECT disabled, networth, num, rank, ability, abilitytime FROM $playerdb ORDER BY disabled ASC, networth DESC;"); mysql_data_seek($users, 0); while($user = mysql_fetch_assoc($users)) { if($user[ability] == 50) { $fakenw = $user[networth]; for($i=0; $i < $user[abilitytime]; $i++) $fakenw -= ($fakenw * 0.01); $user[networth] = round($fakenw,0); } } mysql_data_seek($users, 0); $user = mysql_fetch_assoc($users); foreach($user as $key => $row) { $disabled[$key] = $row['disabled']; $networth[$key] = $row['networth']; } array_multisort($disabled, SORT_ASC, $networth, SORT_DESC, $user); I don't think I have any idea what I'm doing. =( Quote Link to comment https://forums.phpfreaks.com/topic/62250-solved-sorting-two-dimensional-array/ Share on other sites More sharing options...
mdnghtblue Posted July 29, 2007 Author Share Posted July 29, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/62250-solved-sorting-two-dimensional-array/#findComment-309874 Share on other sites More sharing options...
PC Nerd Posted July 29, 2007 Share Posted July 29, 2007 ok, withouth going through your code, i hope hte below code works. $Query = QUERY STUFF; $New_Array = array(); if($QUERY = "TEST") { $New_Array[''][''] = ""; # rearranging your indexes, or simply adding the content } then there is sort($New_Array, ASC) or something, but im not sure about eh actual arguments gdlk Quote Link to comment https://forums.phpfreaks.com/topic/62250-solved-sorting-two-dimensional-array/#findComment-309922 Share on other sites More sharing options...
Barand Posted July 29, 2007 Share Posted July 29, 2007 try <?php $users = dbquery("SELECT disabled, networth, num, rank, ability, abilitytime FROM $playerdb ORDER BY disabled ASC, networth DESC;"); $data = array(); while($user = mysql_fetch_assoc($users)) { if($user['ability'] == 50) { $fakenw = $user['networth']; for($i=0; $i < $user['abilitytime']; $i++) $fakenw -= ($fakenw * 0.01); $user['networth'] = round($fakenw,0); } /** * put data in array */ $data[] = $user; } /** * sort the data using custom mysort() function */ usort ($data, 'mysort'); /** * view the sorted array */ echo '<pre>', print_r($data, true), '</pre>'; /** * mysort function * if a should sort above b, return -1 * if a and be are cosidered equal, return 0 * if a should sort below b, return 1 */ function mysort($a, $b) { if ($a['disabled'] == $b['disabled']) { // sort networth DESC if ($a['networth'] == $b['networth']) return 0; return $a['networth'] > $b['networth'] ? -1 : 1; } return $a['disabled'] < $b['disabled'] ? -1 : 1; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62250-solved-sorting-two-dimensional-array/#findComment-309968 Share on other sites More sharing options...
mdnghtblue Posted August 15, 2007 Author Share Posted August 15, 2007 Tried it, but it didn't work. After the array has been sorted, this code is executed: while ($user = mysql_fetch_array($data)) { $urank++; if ($urank != $user[rank]) dbquery("UPDATE $playerdb SET rank=$urank WHERE num=$user[num];"); } I think the sorting worked though. Quote Link to comment https://forums.phpfreaks.com/topic/62250-solved-sorting-two-dimensional-array/#findComment-324310 Share on other sites More sharing options...
Barand Posted August 15, 2007 Share Posted August 15, 2007 You can't use the $data array like that. mysql_fetch_array() gets a row of data from a mysql result set. try <?php $urank=0; foreach ($data as $user) { $urank++; if ($urank != $user['rank']) dbquery("UPDATE $playerdb SET rank=$urank WHERE num={$user['num']}"); } Quote Link to comment https://forums.phpfreaks.com/topic/62250-solved-sorting-two-dimensional-array/#findComment-324444 Share on other sites More sharing options...
mdnghtblue Posted August 15, 2007 Author Share Posted August 15, 2007 It worked! =D Thanks very much. Quote Link to comment https://forums.phpfreaks.com/topic/62250-solved-sorting-two-dimensional-array/#findComment-324713 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.