PHP - Puzzled By Array_multisort
I am using a multiple array with the layout:
userarray['score'][00] userarray['id][00] userarray['name'][00] etc where the first column contains properties and the second column is the number for each user (shown as 00). After putting the data into the array, I want to sort the array by the score with the highest at the top. I have used the code: array_multisort($userarray['score'], SORT_NUMERIC, SORT_DESC); but it seems to sort the 'score' column while leaving the other columns in the original order. Obviously I want all the rows to remain related to each other. Can anyone help? Similar TutorialsHello I'm a noob when it comes to writing code so please understand. My site is pvpstreams.com It's in development and the goal is to make it into a portal of different games that are being streamed on the web. So gamers can find streams with stats for certain games easy. Now my problem is that I'm trying to make a new column for my table which will display an image for when a user's stream is online, like a lightbulb. When stream is offline it will not show the row for the user. The main stream sites I'm creating the table for are xfire.com and livestream.com Here is my table.php file. Code: [Select] <script language="javascript"> imageX1='plus'; imageX2='plus'; imageX3='plus'; function toggleDisplay(e){ imgX="imagePM"+e; tableX="table"+e; imageX="imageX"+e; tableLink="tableHref"+e; imageXval=eval("imageX"+e); element = document.getElementById(tableX).style; if (element.display=='none') {element.display='block';} else {element.display='none';} if (imageXval=='plus') {document.getElementById(imgX).src='http://www.pvpstreams.com/icons/wowicon.png';eval("imageX"+e+"='minus';");document.getElementById(tableLink).title='Hide WoW';} else {document.getElementById(imgX).src='http://www.pvpstreams.com/icons/wowicon.png';eval("imageX"+e+"='plus';");document.getElementById(tableLink).title='Show WoW';} } </script> <?php $username="myusername"; $password="mypassword"; $tmp="myserver"; mysql_connect(localhost,$username,$password); @mysql_select_db($tmp) or die( "Unable to select database"); $query="SELECT * FROM users"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <table class="sortable" id="wow" border="1" cellpadding="0" cellspacing="0" style="margin-left:auto;margin-right:auto;text-align:left"> <tr> <th><font face="Arial, Helvetica, sans-serif">Character</font></th> <th><font face="Arial, Helvetica, sans-serif">Xfire</font></th> <th><font face="Arial, Helvetica, sans-serif">Livestream</font></th> <th><font face="Arial, Helvetica, sans-serif">Team Members</font></th> <th><font face="Arial, Helvetica, sans-serif">Realm</font></th> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"wowcharacter"); $f2=mysql_result($result,$i,"username"); $f3=mysql_result($result,$i,"livestream"); $f4=mysql_result($result,$i,"wowserver"); $f5=mysql_result($result,$i,"wowplayer1"); $f6=mysql_result($result,$i,"wowplayer2"); $f9=mysql_result($result,$i,"wowplayer3"); $f7=mysql_result($result,$i,"wowrealm"); $f8=mysql_result($result,$i,"xfire"); ?> <tr> <td><font face="Arial, Helvetica, sans-serif"><?php Echo "<a class=blue href=http://$f9.battle.net/wow/en/character/$f7/$f1/simple>$f1</a>"; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php Echo "<a class=blue href=http://www.pvpstreams.com/xfire.php?user=$f2>$f8</a>"; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo "<a class=blue href=http://www.pvpstreams.com/livestream.php?user=$f2>$f3</a>"; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo "<img src=wclassicons/$f5.png><img src=wclassicons/$f6.png><img src=wclassicons/$f9.png>" ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td> </tr> <?php $i++; } ?> </table> I'm thinking that I have to somehow request xfire or livestream's server if it's streaming for the user. From browsing the web I think this can be done with php and curl but I'm not sure how and I'm a beginner in coding. Any help would be greatly appreciated. Hi, I'm trying to combine and sort two tables so I can display the output as one. And I'm getting the error "PHP Warning: array_multisort() [<a href='function.array-multisort'>function.array-multisort</a>]: Array sizes are inconsistent in ... on line 282". Line 282 is "array_multisort($tmp_name, SORT_ASC, $details);". I'm a self taught beginner coder and I know that my coding is often very clumsy and so that is probably the problem. Any help to solve this error would be greatly appreciated. Cheers $sql = "SELECT * FROM db_user LEFT JOIN db_details ON db_details.details_id = db_user.user_details_id WHERE db_user.user_id = '" . $user_id . "'"; $result = mysql_query($sql); $num = mysql_num_rows($result); if ($num != NULL) { for ($i = 0; $i < $num; $i++) { $user_group_id = mysql_result($result, $i, "db_user.user_group_id"); $details_id = mysql_result($result, $i, "db_details.details_id"); $details_name = mysql_result($result, $i, "db_details.details_name"); $details[] = array($details_id, $details_name, $user_group_id); } } $sql = "SELECT * FROM db_members LEFT JOIN db_details ON db_details.details_id = db_members.members_details_id WHERE db_members.members_user_id = '" . $user_id . "'"; $result = mysql_query($sql); $num = mysql_num_rows($result); if ($num != NULL) { for ($i = 0; $i < $num; $i++) { $members_group_id = mysql_result($result, $i, "db_members.members_group_id"); $details_id = mysql_result($result, $i, "db_details.details_id"); $details_name = mysql_result($result, $i, "db_details.details_name"); $details[] = array($details_id, $details_name, $members_group_id); } } foreach ($details as $key => $val) { $tmp_name[$key] = $val[1]; } array_multisort($tmp_name, SORT_ASC, $details); Hi, I have a function where I want to sort an an array of this type: 0 = {'fruit' => 'banana', 'color' => 'yellow', 'age' => '1', 'weight' => '2', 'brand' => 'dole', 'store' => shaws'} 1 = {'fruit' => 'apple', 'color' => 'red', 'age' => '3', 'weight' => '5', 'brand' => 'fuji', 'store' => 'foodmaster'} etc etc. I then want to array_multisort this array with different parameters. One way is to sort by fruit, then color, then brand. The second way by age, then store. Code: [Select] //This is how I sort it for my first way //Let's call the array $rows function sort($rows) { foreach($rows as $key => $value) { $fruit[$key] = $value['fruit']; $color[$key] = $value['color']; $brand[$key] = $value['brand']; } array_multisort($fruit, SORT_ASC, $color, SORT_ASC, $brand, SORT_ASC $rows); } I want to try to reuse this function by passing in the keys that I want to sort with. Like doing sort($rows, array('fruit', 'color', 'brand')), then sort($rows, array('age', 'store')). Is this possible? |