PHP - Sorting Total Array Values
Aplogies for a very dumb question.
Variables pulled from MySQL. $First = $First; $Last = $Last; $Member = array($Last, $First); This URL will sort by first name: www.mysite.com?sort=First ...will sort by last name: www.mysite.com?sort=Last How do I get it to sort by the complete array value? www.mysite.com?sort=Member ...not a valid MySQL result, obviously. ~Wayne Similar TutorialsSay I have a table called artists with these fields (artistid, rank).
Then say I have an array called $rankadjustments with a value for each artist (like 1, 7, 3,-3, 5, 9, etc).
I am doing a MYSQL query that gets the info from artists table and sorts according to the rank field like this...
$sql = "SELECT * FROM artists ORDER BY rank";Easy enough. But what if I would like to bring that data back sorted by (rank + rankadjustment). For example, if the artist ranked 1st had a rank of "1" from mysql, but had a rankadjustment of "5" from the rankadjustment array, his "true" rank would be "6". Again, rankadjustment is NOT a field in my table, otherwise it would obviously be simple. It's calculated on the fly and stored in an array. FYI, the array has an index with their "artistid". For example, $rankadjustment[341][8] would be the artist with an artistid of "341" has a rank adjustment of 8. Is there a way to do this IN the query itself? It doesn't seem possible but wanted to find out for sure. If not, what is best way to do? I assume... Get the data sorted JUST by rank and put it all into an array, then create a new array that adds rank to rankadjustment and reorder by the "new" rank amount? I guess that wouldn't be too bad but again, wanted to make sure I'm not missing something that would allow me to do it all in the query (or just more efficiently). Thanks! Edited by galvin, 21 December 2014 - 09:13 PM. <html> <head> <title>Sum Html Textbox Values using jQuery/JavaScript</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(function() { $(document).on('blur keyup', '.add, .sub', function(e) { var sum = 0; $('.add, .sub').each(function(i) { if (!isNaN(this.value) && this.value.length != 0) { if ($(this).hasClass('add')) { sum += parseFloat(this.value); } else { sum -= parseFloat(this.value); } } }); $('#total').text(sum.toFixed(2)); }) }) </script> </head> <body> <form method="post" action="calc.php"> Addition: <br/> Field 1: <input type="text" class="add" name="1" value="7500"/><br/> Field 2: <input type="text" class="add" name="2" value=""/><br/> Field 3: <input type="text" class="add" name="3" value=""/><br/> Field 4: <input type="text" class="add" name="4" value=""/><br/><br/> Total Addition: <br/><br/> Substraction: <br/> Field 1: <input type="text" class="sub" name="5" value=""/><br/> Field 2: <input type="text" class="sub" name="6" value=""/><br/> Field 3: <input type="text" class="sub" name="7" value=""/><br/> Field 4: <input type="text" class="sub" name="8" value=""/><br/><br/> Total Substraction: <br/><br/> Grand Total: <div id="total"></div> <input type="submit" value="submit" /> </form> </body> </html> Greetings! I would like to ask for you help sorting a couple of lists that are autogenerated with php. The page reads a content of a folder and makes a menu from the names. The folder names a a, b, c, d... etc. The other part reads the content of the selected folder and displays the files. My problem is, that on windows, using xampp, the both the folders and files appear in alphabetic order (what I want), but on the linux server they get completely messed up. Instead of a, b, its o, a, d, etc. I tried using the sort() function but there is no array to sort... Thank you for any suggestions and help! Inka ps. the server has php 4.3.9... does this matter? could help if we upgrade to 5.3? here is the code: Code: [Select] <?php $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { $files[] = $filename; if ($filename != "." && $filename != "..") { if($_SERVER['REQUEST_URI'] == "index.php?l=" . $filename . '&sec=' . $sec){ echo '<td><img src="imagini/arow.gif"></td><td style="text-align: left;">' . strtoupper($filename) . $space; } else { echo '<td style="text-align: left;"><a href="index.php?l='. $filename . '&sec=' . $sec . '">' . strtoupper($filename) . $space; }}} ?> <?php $letter = "a"; if(isset($_GET["l"])){ $letter = $_GET["l"];} $letter = $dir . $letter; if ($handle = opendir($letter)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $filename = $file; $file2 = substr($filename, 0, -4); $thelist .= '<tr><td style="width: 250px; text-align: left;"><a href="'. $letter . '/'. $file.'">'.$file2.'</a></td></tr>'; } } closedir($handle); } ?> <p><?php echo $thelist; ?></p> Hi there, I am working on a PHP web form and I have a combo box and I'm trying to sort the values in it. The values in the combo box are filled using an arraylist: $WeightArray = array('' => '', '0|100' => '100g or Less', '101|250' => '101g to 250g', '251|500' => '251g to 500g', '501|1000' => '501g to 1kg', '1000|2000' => '1kg to 2kg', '2000|3000' => '2kg to 3kg', '3000|4000' => '3kg to 4kg', '4000|5000' => '4kg to 5kg', '5000|6000' => '5kg to 6kg', '6000|7000' => '6kg to 7kg', '7000|8000' => '7kg to 8kg', '8000|9000' => '8kg to 9kg', '9000|10000' => '9kg to 10kg', 'customValues' => 'Custom Values' ); The values get properly filled in the combo box. Thats fine. Its just that the values are not sorted the way I want them to. The Grams and Kilograms are all mixed. It should output in the order defined in the array. See the ScreenShot I've taken and see the difference in the values defined and the output. I tried few sorting methods like ksort($WeightArray); But still the same output. Whats the good trick to sort values in Combo as per the array. All comments and feedback are always welcomed. Thank you. Hello I am very new to php and programming and I need a grand total of "$Total = odbc_result($result, "total");" but not sure of how to create it. I am currently building (for my first time) a shoppingcart. Its an multidimensional array in a session that looks like this. Code: [Select] Array ( [23] => Array ( [0] => 23 [1] => 11.32.097. [2] => 635 [3] => 2 [4] => 89 ) [800] => Array ( [0] => 800 [1] => 20.40.017. [2] => 100 [3] => 1 [4] => 125 ) ) The 4th key contains the price value. What i am trying to do is get all the sub-array's 4th key value and count them together to get a total price. I tried doing this with a foreach loop but its not my strong point, sadly. I you have some time can you please take a look how to solve this? Thanks in advance, Roy I have the following php array structure and couldnot able to sort it according to the overall points. My code is reading an xml file using simplexml. Any help would be appreciated. Array ( => SimpleXMLElement Object ( [played] => 0 [won] => 0 [drawn] => 0 [lost] => 0 [overallpoints] => 0 ) [1] => SimpleXMLElement Object ( [played] => 6 [won] => 3 [drawn] => 0 [lost] => 3 [overallpoints] => 9 ) [2] => SimpleXMLElement Object ( [played] => 3 [won] => 1 [drawn] => 0 [lost] => 2 [overallpoints] => 3 ) [3] => SimpleXMLElement Object ( [played] => 0 [won] => 0 [drawn] => 0 [lost] => 0 [overallpoints] => 0 ) [4] => SimpleXMLElement Object ( [played] => 2 [won] => 1 [drawn] => 0 [lost] => 1 [overallpoints] => 3 ) } Hello, i need some help with sorting arrays by it's value. lets say i want to have two different html tables listing the employees and another for the managers, then i want to sort them by Group ID's: Etc: Workers as the key / 1 as the value. Groups: Workers => 1 Managers => 2 Workers Group ID John 1 Peter 1 Lance 1 Managers Group ID Carol 2 Greg 2 Brian 2 How can i do this? i have an array from a database that looks like this (below) but what i want is for the array to be sorted assedningly by the start position so the team with start position 1 will be 1st and so on...if anyone can help me on what i should do please...thank you Code: [Select] Array ( [1] => Array ( [team_name] => A Team [team_nationality] => British [start_position] => 2 ) [2] => Array ( [team_name] => B Team [team_nationality] => British [start_position] => 1 ) [3] => Array ( [team_name] => C Team [team_nationality] => British [start_position] => 4 ) [4] => Array ( [team_name] => B Team [team_nationality] => British [start_position] => 3 ) ) Hello! I have a 2D array that look as follows: $test["name"][0] = "arnold"; $test["name"][1] = "nate"; $test["name"][2] = "steve"; $test["id"][0] = 2; $test["id"][1] = 0; $test["id"][2] = 1; This means arnold has an ID of 2, nate has an ID of 0 and steve has an ID of 1. Now I want to sort the array based on the ID value, so I want to achieve the following result: $test["name"][0] = "nate"; $test["name"][1] = "steve"; $test["name"][2] = "arnold"; How would I achieve this? Help much appreciated. How to sort PHP2d Array using upon key. eg. I have a array of $users[0]['name'] = "abc" $users[0]['age'] = "12" $users[0]['sex'] = "M"; $users[1]['name'] = "xyz" $users[1]['age'] = "15" $users[1]['sex'] = "M"; $users[2]['name'] = "pqr" $users[2]['age'] = "16" $users[2]['sex'] = "F"; Now I have to sort in on age or 'sex', I am attempting to sort an array by price but cannot seem to get php to do this. Below is the array I am attempting to sort by the value ~ price: Array ( [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992137)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561801_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 14.99 [delivery] => 4.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992137)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561801_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(767538675)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_780417_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 19.99 [delivery] => 4.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(767538675)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_780417_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(254177017)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_271987_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 15.00 [delivery] => 2.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(254177017)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_271987_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992487)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561805_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 4.99 [delivery] => 2.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992487)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561805_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823539)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_155878_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 18.00 [delivery] => 4.99 [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823539)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_155878_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(580777789)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_242553_productId_385055_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 21.99 [delivery] => Free Shipping [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(580777789)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_242553_productId_385055_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823503)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_217063_langId_-1%3Fsource%3Dtd)] => Array ( [logo] => nc0iicjxmq.gif [stock] => In Stock In Stock [price] => 24.99 [delivery] => Reserve and Collect [url] => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823503)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_217063_langId_-1%3Fsource%3Dtd) [mname] => Halfords ) [http://track.webgains.com/click.html?wgcampaignid=72782&wgprogramid=2730&product=1&wglinkid=90005&productname=Continental+Sport+Contact+Road%2FHybrid+Bicycle+Tyre&wgtarget=http://www.awcycles.co.uk/brands/Continental/Sport_Contact_Road/Hybrid_Bicycle_Tyre] => Array ( [logo] => 8yfd588e8t.jpg [stock] => In Stock In Stock [price] => 21.99 [delivery] => [url] => http://track.webgains.com/click.html?wgcampaignid=72782&wgprogramid=2730&product=1&wglinkid=90005&productname=Continental+Sport+Contact+Road%2FHybrid+Bicycle+Tyre&wgtarget=http://www.awcycles.co.uk/brands/Continental/Sport_Contact_Road/Hybrid_Bicycle_Tyre [mname] => AW Cycles ) ) Any help would be much appreciated. Thanks Simon Hi does anyone know how to sort the array below (files) echo "<br /><br /><b>Favorites</b><br />"; $d = dir("/mnt/disk/v1/mfiles/downloads/music/favorites/"); while (false !== ($entry = $d->read())) { if($entry!='.' && $entry!='..') { if(is_dir($entry)) { echo 'true'; $subdirs = get_leaf_dirs($entry); if ($subdirs) $array = asort(array_merge($array, $subdirs)); else $array[] = $entry; } echo '<a href="/scripts/download.php?actie=favorites/'.$entry.'" title="Click here to download this song" target="_blank">'.left($entry,(strlen($entry)-4)).'</a><br />'; } } $d->close(); I have an array that looks like this... Code: [Select] Array ( [0] => Array ( [0] => 2 [1] => Dylan Cross ) [1] => Array ( [0] => 5 [1] => Bob Smith ) [2] => Array ( [0] => 1 [1] => Sarah Park ) [3] => Array ( [0] => 7 [1] => Jane Knowles ) [4] => Array ( [0] => 6 [1] => Harry Hill ) [5] => Array ( [0] => 3 [1] => Jack Rock ) ) How do I sort this array so it is ordered alphabetically by the names so the result will be... Code: [Select] Array ( [0] => Array ( [0] => 5 [1] => Bob Smith ) [1] => Array ( [0] => 2 [1] => Dylan Cross ) [2] => Array ( [0] => 6 [1] => Harry Hill ) [3] => Array ( [0] => 3 [1] => Jack Rock ) [4] => Array ( [0] => 7 [1] => Jane Knowles ) [5] => Array ( [0] => 1 [1] => Sarah Park ) ) Thanks The following code works, but I need to take it a step further and I'm not sure how. What I'd like to do is sort the output so the highest point totals get printed first (along with the associated information). Can anyone help? Here's the site: http://www.beat-the-spread.net/seasontotals.php Here's the code: Code: [Select] $i=1; $w=1; $result = mysql_query( " SELECT team_name, owner FROM teams" ); while ($row = mysql_fetch_array($result)) { echo "<tr>\n", "</td><td>", $row['owner'], "</td><td>", $row['team_name']; $result1 = mysql_query( " SELECT bye, owner_pts FROM schedule WHERE team = $i"); $num_rows = mysql_num_rows($result1); while ($row1 = mysql_fetch_array($result1)) { $totalpts = $totalpts + $row1['owner_pts']; if (empty($row1['bye'])) { echo "</td><td>", $row1['owner_pts'], "</tr>\n"; } else { echo "</td><td>","0","</tr>\n"; } } while ($num_rows<=16) { echo "</td><td>","","</tr>"; $num_rows++; } echo "</td><td>","$totalpts","</tr>"; $totalpts=0; $w++; $i++; } echo "</table>\n"; mysql_close($con); Thanks! Hi - I'm modifying some PHP code that someone else wrote. Below is a snippet of code that basically takes mp3 files files within all the folders in a directory, and creates an RSS feed. In the resulting feed, the files are intermingled in date order. Instead, I'd like them sorted by the directory that they are in. So, if there are files in these directories: 01-First 02-Second 03-Third I'd like all the files in the "01-First" directory to come before the "02-Second" directory and so on. Within each directory the files can be sorted as they currently are. The name of the directory in the code is signified by $dir_name. I tried sort() and array_multisort() but I couldn't get either of them to work the way I wanted. Any thoughts? if ($url_params['mode'] == 'rss2') { print getRSSHeader('UTF-8', $device, $vm_config, $vm_name, $web_master, $logo_image,$context); $mailboxes = array(); if ($dev_dir = @opendir($VMHome . $device)) { //might fail if mailbox has not received first message yet while ($f = readdir($dev_dir)) { if (!eregi("tmp","$f") && !eregi("\.+","$f") && !eregi("Exclude-From-CD", "$f")) { //skip tmp directory and Exclude-From-CD Directories array_push($mailboxes,"$f"); #collect all existing mailboxes } } array_multisort($mailboxes, SORT_DESC); foreach ($mailboxes as $mailbox) { $files=array(); $dir_name = $VMHome . $device . '/'. $mailbox .'/'; $dir = opendir($dir_name); while ($f = readdir($dir)) { if (eregi("\.txt",$f)){ #if filename matches .txt in the name array_push($files,"$f"); #push into $files array } } foreach($files as $file){ $props = getProperties($dir_name, $file); $uid = getMP3File($dir_name, $file, $props, $device, $mailbox, $vm_config, $vm_name,$context); print getRSSItem ($props, $uid, $dir_name, $device, '/' . $mailbox . '/', $context); } } } print ' </channel> </rss>'; } Hi. I have a multidimensional array and i need sort it by two different key. I've tried almost every sort function but no chance. Code: [Select] Array ( [0] => Array ( [0] => 1 [id] => 1 [11] => this is test [main_txt] => this is test [12] => 943965420 [add_time] => 943965420 [13] => 1323356400 [add_date] => 1323356400 ) [1] => Array ( [0] => 3 [id] => 3 [11] => another test [main_txt] => another test [12] => 943965120 [add_time] => 943965120 [13] => 1323702000 [add_date] => 1323702000 ) [2] => Array ( [0] => 32 [id] => 32 [11] => oppss one more test [main_txt] => oppss one more test [12] => 943944900 [add_time] => 943944900 [13] => 1323702000 [add_date] => 1323702000 ) [3] => Array ( [0] => 5 [id] => 5 [11] => okay, this is last one. Seriously [main_txt] => okay, this is last one. Seriously [12] => 943937160 [add_time] => 943937160 [13] => 1323615600 [add_date] => 1323615600 ) ) I need sort by add_date(Newer to Older) first then add_time(Later to Earlier) later. I hope i made my self clear. Generally I am able to help my self on the forums. But, this one has me stumped. I've done it before and I don't remember it being this hard. The array looks something like so $users['id'] = '00001','00002','0003' $users['jobs'] = '22','322','112' $users['connected'] = '12','210','89' $users['percent'] = '54','65','89' I've used so much different things to try and take those, keep the data together (index 0 might be moved to 2, but all values from 0 are moved to 2) and sort by 'percent'. So I can display the person that has the highest percentage first. Basically the data was originally a CSV that is now in a database and is pulled, percentages calculated and then displayed. But the order is off. There is a bunch more data then show but I'm using PHP to do all the math for adding up jobs, seeing whats connected right and then coming up with a percentage, sticking it all in an array so I can sort it; but for some reason I cannot remember how I've done this before. Hi All I'm new to php and I have a multidimensional array set up which I need to sort for a results list from an assessment. The array is $gradout['name']['userid']=score. I need to sort the array by score in a decending order. ['name'] is a string and ['userid'] is a numeric. I've tried looking at the php manual but It's just confusing me. Can anyone explain to me in laymans terms what I need to do and why. I have an array that has data in it and its being sorted like this: Code: [Select] Source 1 (zip): filename.zip Source 1 (jpg): filename.jpg Source 1 (png): filename.png Source 2 (zip): filename.zip Source 2 (jpg): filename.jpg Source 2 (png): filename.png Source 3 (zip): filename.zip Source 3 (jpg): filename.jpg Source 3 (png): filename.png Id like the data to be sorted like this instead, but i dont know how to get it to sort this way: Code: [Select] Source 1 (zip): filename.zip Source 2 (zip): filename.zip Source 3 (zip): filename.zip Source 1 (jpg): filename.jpg Source 2 (jpg): filename.jpg Source 3 (jpg): filename.jpg Source 1 (png): filename.png Source 2 (png): filename.png Source 3 (png): filename.png Here's my code: any help is appreciated. thanks. for ($i = 1; $i <= 5; $i++) { $src[$i] = 'Source '.$i.' ('.$ext.'): '.$filename.''; array_multisort($src, SORT_ASC, SORT_STRING); } |