Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem with sorting tables in PHP

    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?...isplay=ratings

    The code in question for said table:

    Code:
    // 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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...isplay=ratings

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by chibul View Post
    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...isplay=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.

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I posted there originally, but was told I should post here. lol...I updated my post over there.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by chibul View Post
    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.

  • #8
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Update:

    I posted in the Javascript forum (Javascript Issue - Sorting Tables (sorttable.js)) with more info, and they're saying it's a PHP issue.

    Can anyone help?

  • #9
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • #10
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #11
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Attached Files Attached Files

  • #12
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Just wanted to make sure those were viewable...I'm at my wits' end with this issue. lol

  • #13
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Anyone?

  • #14
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Ok. Compare the code inside the <head> tags between this page - http://www.iblhoops.net/modules.php?...isplay=ratings (the one that doesn't work) - and this page - http://www.ijbl.net/modules.php?name...isplay=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.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #15
    New Coder
    Join Date
    Aug 2009
    Posts
    95
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •