Reputation: 197
I have tried all the solutions from other threads but nothing seems to work for me. I am trying to show the table with descending Weight Score (which is the last column of my table).
I have written the following code which doesn't work with PHP provided table but works perfectly with html page.
view-source:http://hidden.com/diversefund.php
The html page where it does work: http://hidden.com/new.html
I am working with database provided table so this is a problem.
Important lines of code from my php script:
<style>
#form {
border: 5px solid violet;
}
input[type=submit] {padding:5px 15px; background:white; border:1px solid blue;
cursor:pointer;
-webkit-border-radius: 5px;
border-radius: 5px; }
</style>
<script type='text/javascript' src='http://code.jquery.com/jquery-compat-git.js'></script>
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type='text/javascript'>
$(window).load(function(){
var $tbody = $('table tbody');
$tbody.find('tr').sort(function (a, b) {
var tda = parseFloat($(a).find('td:eq(7)').text()); //the column to sort by
var tdb = parseFloat($(b).find('td:eq(7)').text()); //the column to sort by
// if a < b return 1
return tda < tdb ? 1
// else if a > b return -1
:
tda > tdb ? -1
// else they are equal - return 0
:
0;
}).appendTo($tbody);
}); </script>
$queryx1 = mysql_query("select * from analytics_divequ where aum >= '$aum1' AND fiveyr >= '$fiveyr1' AND oneyr >= '$lastyr1' ", $linkx);
echo '<table cellpadding="5" border="1px" align="center" id="caltbl"><thead><tr><th><b>Mutual Fund Scheme</b></th><th><b>AUM</b></th><th><b>Last Year %</b></th><th><b>Two Years %</b></th><th><b>Three Years %</b></th><th><b>5 Years %</b></th><th><b>Weight Score</b></th></tr></thead><tbody>';
$i=1;
while ($row2x = mysql_fetch_array($queryx1)) {
$mfsc=$row2x['mfscheme'];
$aum=$row2x['aum'];
$fiveyr=$row2x['fiveyr'];
$lastyr=$row2x['oneyr'];
$threeyr=$row2x['threeyr'];
$twoyr=$row2x['twoyr'];
$wscore=(($aum*$aumc1)+($fiveyr*$fiveyrc1)+($lastyr*$oneyrc1))/100;
?>
<tr><td><? echo $mfsc ?></td><td><? echo $aum ?></td><td><? echo $lastyr ?></td><td><? echo $twoyr ?></td><td><? echo $threeyr ?></td><td><? echo $fiveyr ?></td><td class="sortnr"><? echo $wscore ?></td></tr>
echo '</tbody></table> <br>';
?>
Also to add I can't use Mysql here, as you can see the last column is not part of the database.
Upvotes: 0
Views: 69
Reputation: 969
First Multi Dimensional create an array with your Mysql data and push Your last column value in that array. After that You can sort with that array in any language. And if you change that array to JSON you can easily sort with JQUERY
Upvotes: 0
Reputation: 1859
Can you use plugin?
Or PHP to do what you need to? You can accomplish required task with different MySQL statement, using "ORDER BY", if data stored in the database.. or create a new PHP array that will contain data in correct order and than display data using a loop. (http://php.net/manual/en/function.usort.php)
For table management (sorting, search and so on) I would recommend using datatables (https://datatables.net). From last time i was searching around this was the best jquery plugin for tables on the market... However, in my experience datatables can't handle a lot of data, once you have 1000+ rows it may take 10-20 second for page to load.
Upvotes: 2