...

View Full Version : Problem with sorting tables in PHP



chibul
11-24-2011, 07:33 PM
Not really sure why this isn't working. I've mirrored this webpage from a friend's page - everything is literally identical, save for the references to the databases. His works, and mine doesn't. Here's an example of one of the tables I'm trying to sort:

http://www.iblhoops.net/modules.php?name=Team&op=team&tid=6&display=ratings

The code in question for said table:


// BEGIN RATINGS TABLE

$table_ratings="<table class=\"sortable\">
<thead>
<tr bgcolor=$color1><th><font color=$color2>Pos</font></th><th><font color=$color2>Player</font></th><th><font color=$color2>Age</font></th><th><font color=$color2>Sta</font></th><th><font color=$color2>2ga</font></th><th><font color=$color2>2g%</font></th><th><font color=$color2>fta</font></th><th><font color=$color2>ft%</font></th><th><font color=$color2>3ga</font></th><th><font color=$color2>3g%</font></th><th><font color=$color2>orb</font></th><th><font color=$color2>drb</font></th><th><font color=$color2>ast</font></th><th><font color=$color2>stl</font></th><th><font color=$color2>tvr</font></th><th><font color=$color2>blk</font></th><th><font color=$color2>o-o</font></th><th><font color=$color2>d-o</font></th><th><font color=$color2>p-o</font></th><th><font color=$color2>t-o</font></th><th><font color=$color2>o-d</font></th><th><font color=$color2>d-d</font></th><th><font color=$color2>p-d</font></th><th><font color=$color2>t-d</font></th><th><font color=$color2>Foul</font></th><th><font color=$color2>Inj</font></th></tr>
</thead>
<tbody>
";

$i=0;

while ($i < $num)
{

if ($yr == "")
{

$name=mysql_result($result,$i,"name");
$team=mysql_result($result,$i,"teamname");
$pid=mysql_result($result,$i,"pid");
$pos=mysql_result($result,$i,"altpos");
$p_ord=mysql_result($result,$i,"ordinal");
$age=mysql_result($result,$i,"age");
$inj=mysql_result($result,$i,"injured");

$r_2ga=mysql_result($result,$i,"r_fga");
$r_2gp=mysql_result($result,$i,"r_fgp");
$r_fta=mysql_result($result,$i,"r_fta");
$r_ftp=mysql_result($result,$i,"r_ftp");
$r_3ga=mysql_result($result,$i,"r_tga");
$r_3gp=mysql_result($result,$i,"r_tgp");
$r_orb=mysql_result($result,$i,"r_orb");
$r_drb=mysql_result($result,$i,"r_drb");
$r_ast=mysql_result($result,$i,"r_ast");
$r_stl=mysql_result($result,$i,"r_stl");
$r_blk=mysql_result($result,$i,"r_blk");
$r_tvr=mysql_result($result,$i,"r_to");
$r_sta=mysql_result($result,$i,"sta");
$r_foul=mysql_result($result,$i,"r_foul");
$r_totoff=mysql_result($result,$i,"oo")+mysql_result($result,$i,"do")+mysql_result($result,$i,"po")+mysql_result($result,$i,"to");
$r_totdef=mysql_result($result,$i,"od")+mysql_result($result,$i,"dd")+mysql_result($result,$i,"pd")+mysql_result($result,$i,"td");
$r_oo=mysql_result($result,$i,"oo");
$r_do=mysql_result($result,$i,"do");
$r_po=mysql_result($result,$i,"po");
$r_to=mysql_result($result,$i,"to");
$r_od=mysql_result($result,$i,"od");
$r_dd=mysql_result($result,$i,"dd");
$r_pd=mysql_result($result,$i,"pd");
$r_td=mysql_result($result,$i,"td");
$r_bird=mysql_result($result,$i,"bird");

$draftyear=mysql_result($result,$i,"draftyear");
$exp=mysql_result($result,$i,"exp");
$cy=mysql_result($result,$i,"cy");
$cyt=mysql_result($result,$i,"cyt");

$yearoffreeagency=$draftyear+$exp+$cyt-$cy;

} else {

$name=mysql_result($result,$i,"name");
$team=mysql_result($result,$i,"team");
$pid=mysql_result($result,$i,"pid");

$r_2ga=mysql_result($result,$i,"r_2ga");
$r_2gp=mysql_result($result,$i,"r_2gp");
$r_fta=mysql_result($result,$i,"r_fta");
$r_ftp=mysql_result($result,$i,"r_ftp");
$r_3ga=mysql_result($result,$i,"r_3ga");
$r_3gp=mysql_result($result,$i,"r_3gp");
$r_orb=mysql_result($result,$i,"r_orb");
$r_drb=mysql_result($result,$i,"r_drb");
$r_ast=mysql_result($result,$i,"r_ast");
$r_stl=mysql_result($result,$i,"r_stl");
$r_blk=mysql_result($result,$i,"r_blk");
$r_tvr=mysql_result($result,$i,"r_tvr");
$r_totoff=mysql_result($result,$i,"r_oo")+mysql_result($result,$i,"r_do")+mysql_result($result,$i,"r_po")+mysql_result($result,$i,"r_to");
$r_totdef=mysql_result($result,$i,"r_od")+mysql_result($result,$i,"r_dd")+mysql_result($result,$i,"r_pd")+mysql_result($result,$i,"r_td");
$r_oo=mysql_result($result,$i,"r_oo");
$r_do=mysql_result($result,$i,"r_do");
$r_po=mysql_result($result,$i,"r_po");
$r_to=mysql_result($result,$i,"r_to");
$r_od=mysql_result($result,$i,"r_od");
$r_dd=mysql_result($result,$i,"r_dd");
$r_pd=mysql_result($result,$i,"r_pd");
$r_td=mysql_result($result,$i,"r_td");


}

if(($i % 2)==0) {
$bgcolor="FFFFFF";
}else{
$bgcolor="EEEEEE";
}

if ($tid == 0)
{
$table_ratings=$table_ratings." <tr bgcolor=$bgcolor><td>$pos</td><td><a href=\"http://www.iblhoops.net/modules.php?name=Player&pa=showpage&pid=$pid\">$name</a></td><td>$age</td><td>$r_sta</td><td>$r_2ga</td><td>$r_2gp</td><td>$r_fta</td><td>$r_ftp</td><td>$r_3ga</td><td>$r_3gp</td><td>$r_orb</td><td>$r_drb</td><td>$r_ast</td><td>$r_stl</td><td>$r_tvr</td><td>$r_blk</td><td>$r_oo</td><td>$r_do</td><td>$r_po</td><td>$r_to</td><td>$r_od</td><td>$r_dd</td><td>$r_pd</td><td>$r_td</td><td>$r_foul</td><td>$inj</td></tr>
";
} else {
if ($p_ord > 959)
{
$table_ratings=$table_ratings." <tr bgcolor=$bgcolor><td>$pos</td><td>(<a href=\"http://www.iblhoops.net/modules.php?name=Player&pa=showpage&pid=$pid\">$name)*</a></td><td>$age</td><td>$r_sta</td><td>$r_2ga</td><td>$r_2gp</td><td>$r_fta</td><td>$r_ftp</td><td>$r_3ga</td><td>$r_3gp</td><td>$r_orb</td><td>$r_drb</td><td>$r_ast</td><td>$r_stl</td><td>$r_tvr</td><td>$r_blk</td><td>$r_oo</td><td>$r_do</td><td>$r_po</td><td>$r_to</td><td>$r_od</td><td>$r_dd</td><td>$r_pd</td><td>$r_td</td><td>$r_foul</td><td>$inj</td></tr>
";
} elseif ($r_bird == 0) {
$table_ratings=$table_ratings." <tr bgcolor=$bgcolor><td>$pos</td><td><i><a href=\"http://www.iblhoops.net/modules.php?name=Player&pa=showpage&pid=$pid\">$name</i></a></td><td>$age</td><td>$r_sta</td><td>$r_2ga</td><td>$r_2gp</td><td>$r_fta</td><td>$r_ftp</td><td>$r_3ga</td><td>$r_3gp</td><td>$r_orb</td><td>$r_drb</td><td>$r_ast</td><td>$r_stl</td><td>$r_tvr</td><td>$r_blk</td><td>$r_oo</td><td>$r_do</td><td>$r_po</td><td>$r_to</td><td>$r_od</td><td>$r_dd</td><td>$r_pd</td><td>$r_td</td><td>$r_foul</td><td>$inj</td></tr>
";
} else if ($fayr == "" OR $yearoffreeagency == $fayr) {
$table_ratings=$table_ratings." <tr bgcolor=$bgcolor><td>$pos</td><td><a href=\"http://www.iblhoops.net/modules.php?name=Player&pa=showpage&pid=$pid\">$name</a></td><td>$age</td><td>$r_sta</td><td>$r_2ga</td><td>$r_2gp</td><td>$r_fta</td><td>$r_ftp</td><td>$r_3ga</td><td>$r_3gp</td><td>$r_orb</td><td>$r_drb</td><td>$r_ast</td><td>$r_stl</td><td>$r_tvr</td><td>$r_blk</td><td>$r_oo</td><td>$r_do</td><td>$r_po</td><td>$r_to</td><td>$r_od</td><td>$r_dd</td><td>$r_pd</td><td>$r_td</td><td>$r_foul</td><td>$inj</td></tr>
";
}
}

$i++;

}

$table_ratings=$table_ratings."
</tbody>
</table>
";

if ($tid != 0)
{

$table_totals=$table_totals."
<table class=\"sortable\">
<thead>
<tr bgcolor=$color1><th><font color=$color2>Pos</font></th><td colspan=3><font color=$color2>Player</font></th><th><font color=$color2>g</font></th><th><font color=$color2>gs</font></th><th><font color=$color2>min</font></th><th><font color=$color2>fgm</font></th><th><font color=$color2>fga</font></th><th><font color=$color2>ftm</font></th><th><font color=$color2>fta</font></th><th><font color=$color2>3gm</font></th><th><font color=$color2>3ga</font></th><th><font color=$color2>orb</font></th><th><font color=$color2>reb</font></th><th><font color=$color2>ast</font></th><th><font color=$color2>stl</font></th><th><font color=$color2>to</font></th><th><font color=$color2>blk</font></th><th><font color=$color2>pf</font></th><th><font color=$color2>pts</font></th></tr>
</thead>
<tbody>
";


Any ideas?

Fou-Lu
11-24-2011, 08:03 PM
By sorting, do you mean in which the order by is for the results? That would require your query to be provided.
Also, do not use mysql_result with this much data. Mysql_result is horrendously slow; it does not move the internal pointer so each lookup using mysql_result takes a before record pointer, seeks to the proper record, looks up the data to return, and resets the pointer seek to 0. Instead, use a fetch to grab an entire record into memory and store it as an array or object.
I won't follow the link at work, so I don't know what you are comparing to. Given some of these class names and whatnots, I'm almost leaning towards a sortable table much like a DataRowGrid. Although, this does not appear to contain any links which would make the recordset sortable from a PHP level, which would indicate instead that a javascript override would be in use.

chibul
11-24-2011, 08:22 PM
I should've clarified...when I mean sorted, I mean being able to click on the header of a column within the table, and having that sort the entire table by that column.

As far as how the data is gathered, it's worked pretty well for the last couple of years he's had his site going...really just wanting to fix the sorting issue and not make any drastic changes. :)

chibul
11-24-2011, 08:26 PM
For what it's worth, he does have a sorttable.js located in a "jslib" folder on the main portion of the site, but not in the directory that this is in. Yet his sorting works perfectly. I should link to a working example of what I'm wanting to have:

http://www.ijbl.net/modules.php?name=Team&op=team&tid=10&display=ratings

Fou-Lu
11-24-2011, 09:21 PM
For what it's worth, he does have a sorttable.js located in a "jslib" folder on the main portion of the site, but not in the directory that this is in. Yet his sorting works perfectly. I should link to a working example of what I'm wanting to have:

http://www.ijbl.net/modules.php?name=Team&op=team&tid=10&display=ratings

This sounds to me to be completely a javascript issue, and not one of PHP. PHP would require that you provide either a postable form or a querystring link in order to sort the table like a dataGridview. PHP can certainly sort, but you would be required to reload the page to do a change in the sort. Javascript can do this on the fly without needing to query the data based on sort order input by a user.

chibul
11-25-2011, 06:34 AM
I posted there originally, but was told I should post here. lol...I updated my post over there.

Fou-Lu
11-25-2011, 02:47 PM
I posted there originally, but was told I should post here. lol...I updated my post over there.

If you are intending to use Javascript to perform this task, the client developers will need to see the output of your PHP code and not the code itself. They'll need the javascript too.
A link would be better to give them so they can run it.

chibul
12-12-2011, 07:58 AM
Update:

I posted in the Javascript forum (http://www.codingforums.com/showthread.php?t=245903) with more info, and they're saying it's a PHP issue.

Can anyone help?

BluePanther
12-12-2011, 08:34 AM
Pay attention to what Old Pedant is saying to you. In the code you've posted above, $table_ratings isn't being outputted - which means the table you want sortable isn't either.

Post the WHOLE page with that code in it, and we'll see if you're outputting what you think you are. Also, make sure in your HTML you are actually linking to that javascript file.

chibul
12-13-2011, 12:30 AM
It'll take a few posts to post the whole page, as it's an entire module with multiple sections...but bear with me.

Also, on my friend's page - the one that it's working on? Nowhere in that entire page is he linking to that javascript file...and yet it works. That's why I initially thought this to be a PHP issue.

chibul
12-13-2011, 12:52 AM
OK, so the easiest way to do this was to break the file into four text files and post them that way, due to the size. Those are attached.

chibul
12-13-2011, 06:29 PM
Just wanted to make sure those were viewable...I'm at my wits' end with this issue. :( lol

chibul
12-16-2011, 08:14 PM
Anyone?

BluePanther
12-17-2011, 12:43 AM
Ok. Compare the code inside the <head> tags between this page - http://www.iblhoops.net/modules.php?name=Team&op=team&tid=6&display=ratings (the one that doesn't work) - and this page - http://www.ijbl.net/modules.php?name=Team&op=team&tid=10&display=ratings (the one that does work). The one that does work has the javascript to handle sorting the table (the <script> tag just before </head>) whereas the page that doesn't work does not have this. This is why your tables aren't being sorted.

chibul
01-04-2012, 06:46 PM
Sorry, been away for a bit.

To BluePanther - I completely understand that. Here's the issue - in that file that does work? There's ZERO reference to that script tag or anything within it. No reference to the js file, nothing. It's not there. So how is it showing up in the page source, if there's no actual reference in the file? That's what I can't figure out.

BluePanther
01-04-2012, 06:57 PM
The javascript is physically on that page, in the head tag. View source, look just before the </head> tag. It's right there - the script that's not on your page. Can't really explain it any differently :) the function for sorting is defined right there. If I remember right, its not a js file - its just a set if definitions.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum