PHP Sort-able Tables

Hi There,

I got some code from another site regarding sort-able tables and I am trying to adapt it to for my use.

Here is the code I got from the site.

[php]<?php

require_once ( ‘mysql_connect.php’ );

?>

Untitled Document <?php

if ( isset ( $_GET[‘order_var’] ) )
{
$order_var = $_GET[‘order_var’];

    switch ( $order_var )
    {
            case 'fn_asc':
                    $order_var = "FullName ASC";
                    break;
            case 'fn_dsc':
                    $order_var = "FullName DESC";
                    break;
            case 'ln_asc':
                    $order_var = "GP ASC";
                    break;
            case 'ln_dsc':
                    $order_var = "GP DESC";
                    break;
            case 'id_asc':
                    $order_var = "Goals ASC";
                    break;
            case 'id_dsc':
                    $order_var = "Goals DESC";
                    break;
    }

}
else
{
$order_var = “FullName ASC”;
}

$query = “SELECT * FROM SeasonStats ORDER BY $order_var”;
print $query;
$result = mysql_query ( $query );

$id_var = “FN_asc”;
$fn_var = “GP_asc”;
$ln_var = “Goals_asc”;

if ( $order_var == “FullName ASC” )
{
$id_var = “FN_dsc”;
}
else if ( $order_var == “FullName DESC” )
{
$id_var = “FN_asc”;
}

if ( $order_var == “GP ASC” )
{
$fn_var = “GP_dsc”;
}
else if ( $order_var == “FullName DESC” )
{
$fn_var = “FN_asc”;
}

if ( $order_var == “Goals ASC” )
{
$ln_var = “Goals_dsc”;
}
else if ( $order_var == “Goals DESC” )
{
$ln_var = “Goals_asc”;
}

print ’

'; while ( $row = mysql_fetch_assoc ( $result ) ) { print ' '; }

print ‘

Player Name GP Goals
' . $row['FullName'] . ' ' . $row['GP'] . ' ' . $row['Goals'] . '
’;

?>

© Asib12 2009

[/php]

I have been able to figure out how to change the variables and what not but on my site it calls for a Where statement with two variables and I cannot figure out where to put them to get the table to work.

Here is the code I want to transform from my site.

[php]<?php
// Connects to your Database
mysql_connect("", “cnghldb”, “”) or die(mysql_error());
mysql_select_db("") or die(mysql_error());

//Get TeamID from URL
$iTeamID = $_GET[“TeamID”];
$iPlayerID= $_GET[“PlayerID”];
$iSea=$_GET[“Sea”];
Print “<img src=“http://www.cnghl.org/cnghldb/cnghlimages/".$iTeamID.".png”>”;
$oteaminfo = mysql_query("
SELECT Players.FullName, Seasonteam.TeamID, SeasonStats.Sea, SeasonStats.GP, SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.Pim, SeasonStats.PlusMinus, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Players.PlayerID
FROM Seasonteam
LEFT JOIN (Players
LEFT JOIN SeasonStats ON Players.PlayerID = SeasonStats.PlayerID)
ON Seasonteam.TeamID = SeasonStats.TeamID
WHERE Seasonteam.TeamID=$iTeamID AND SeasonStats.Sea=$iSea
ORDER BY SeasonStats.Points DESC;

") or die(mysql_error());

Print “

”;
Print “”;
Print “”;
Print “”;
Print “”;
Print “”;
Print “”;
Print “”;
Print “”;
Print “”;
Print “”;
Print “”;
Print “”;

while($row = mysql_fetch_array($oteaminfo))
{
Print “

”;
Print ‘’;
Print “ “;
Print “ “;
Print “ “;
Print “ “;
Print “ “;
Print “ “;
Print “ “;
Print “ “;
Print “ “;
Print “ “;
Print “ ";
Print “”;
}
Print “
Player NameGPGAPTSPIM+/-PPSHGWGTS
’ . $row[‘FullName’] . ‘”.$row[‘GP’]."”.$row[‘Goals’].””.$row[‘Assists’].””.$row[‘Points’].””.$row[‘Pim’].””.$row[‘PlusMinus’].””.$row[‘PP’].””.$row[‘SH’].””.$row[‘GW’].””.$row[‘GT’].””.$row[‘S’].”
”;[/php]

Both your mysql and HTML code are deprecated.

JavaScript would work much better in this case, rather than making repeated calls to the database to sort your data. I’m sure some of the other posters on here will give you some examples. Basically you make one query to the database to get your data and then let JavaScript sort it clientside each time you change the sort without calling the server again.

You need to learn how to use Pdo or mysqli with prepared statements . There is a download to a PDO bump start database in my signature to help get you going.

Thanks for your help, I will take a look at the link.

Well, I do not know if I agree with that. JavaScript is CLIENT-SIDE and so there is little security in it.
Also, all of your data would have to be sent to the browser. The reason you want to do this in SQL is
to limit what you are sending out to the browser. Especially if you have a large database. Servers are
very fast at sorting out data and pulling just what you want with WHERE clauses. That is what they were
designed for. Doing this in a browser opens up a lot of issues.

Sorry Kevin, but, my opinion.

So, CNGHL, what are you attempting to place into the WHERE clause and why can you not do it?

Hi ErnieAlex,

I have tried adding the where clause on line 49, but It doesn’t pull the information i need, so I am not sure how to work it to get the results that I want.

[php]<?php

require_once ( ‘mysql_connect.php’ );

?>

Untitled Document <?php

if ( isset ( $_GET[‘order_var’] ) )
{
$order_var = $_GET[‘order_var’];

      switch ( $order_var )
      {
              case 'fn_asc':
                      $order_var = "FullName ASC";
                      break;
              case 'fn_dsc':
                      $order_var = "FullName DESC";
                      break;
              case 'ln_asc':
                      $order_var = "GP ASC";
                      break;
              case 'ln_dsc':
                      $order_var = "GP DESC";
                      break;
              case 'id_asc':
                      $order_var = "Goals ASC";
                      break;
              case 'id_dsc':
                      $order_var = "Goals DESC";
                      break;
      }

}
else
{
$order_var = “FullName ASC”;
}

$query = “SELECT * FROM SeasonStats ORDER BY $order_var”;
print $query;
$result = mysql_query ( $query );

$id_var = “FN_asc”;
$fn_var = “GP_asc”;
$ln_var = “Goals_asc”;

if ( $order_var == “FullName ASC” )
{
$id_var = “FN_dsc”;
}
else if ( $order_var == “FullName DESC” )
{
$id_var = “FN_asc”;
}

if ( $order_var == “GP ASC” )
{
$fn_var = “GP_dsc”;
}
else if ( $order_var == “FullName DESC” )
{
$fn_var = “FN_asc”;
}

if ( $order_var == “Goals ASC” )
{
$ln_var = “Goals_dsc”;
}
else if ( $order_var == “Goals DESC” )
{
$ln_var = “Goals_asc”;
}

print ’

'; while ( $row = mysql_fetch_assoc ( $result ) ) { print ' '; }

print ‘

Player Name GP Goals
' . $row['FullName'] . ' ' . $row['GP'] . ' ' . $row['Goals'] . '
’;

?>

© Asib12 2009

[/php]

I need to add basically all this to the code
[php]
SELECT Players.FullName, Seasonteam.TeamID, SeasonStats.Sea, SeasonStats.GP, SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.Pim, SeasonStats.PlusMinus, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Players.PlayerID
FROM Seasonteam
LEFT JOIN Players
LEFT JOIN SeasonStats ON Players.PlayerID = SeasonStats.PlayerID)
ON Seasonteam.TeamID = SeasonStats.TeamID
WHERE Seasonteam.TeamID=$iTeamID AND SeasonStats.Sea=$iSea
[/php]

Sorry, CNGHL, you are not clear on what you are looking for. If you mean that you want to combine the two item, just do so.

So, this line:
$query = “SELECT * FROM SeasonStats ORDER BY $order_var”;
combined with these lines:
SELECT Players.FullName, Seasonteam.TeamID, SeasonStats.Sea, SeasonStats.GP, SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.Pim, SeasonStats.PlusMinus, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Players.PlayerID
FROM Seasonteam
LEFT JOIN Players
LEFT JOIN SeasonStats ON Players.PlayerID = SeasonStats.PlayerID)
ON Seasonteam.TeamID = SeasonStats.TeamID
WHERE Seasonteam.TeamID=$iTeamID AND SeasonStats.Sea=$iSea
Would just be:
[php]
$query = “SELECT Players.FullName, Seasonteam.TeamID, SeasonStats.Sea, SeasonStats.GP, SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.Pim, SeasonStats.PlusMinus, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Players.PlayerID
FROM Seasonteam
LEFT JOIN Players
LEFT JOIN SeasonStats ON Players.PlayerID = SeasonStats.PlayerID)
ON Seasonteam.TeamID = SeasonStats.TeamID
WHERE Seasonteam.TeamID=$iTeamID AND SeasonStats.Sea=$iSea ORDER BY $order_var”;
[/php]
Is that what you are asking? But, you have not told us what arguments you want in the WHERE part.
It appears that you do not need that complicated of a query. Not going by the simpler code displaying
the data. But, without knowing more about your table layouts, I am not sure what you are asking.

So, if this helps, great. If not, what are you trying to select from in the WHERE clause…

Hi ErnieAlex, The arguments I want are as follows

http://www.cnghl.org/cnghldb/test/index.php?TeamID=8&Sea=1

I have tried using the code, but I do not get any results. I have it pulling my database from the code.

I just realized that I am missing

[php]$iTeamID = $_GET[“TeamID”];
$iGoalieID= $_GET[“GoalieID”];
$iSea=$_GET[“Sea”];
$oteaminfo = mysql_query(" [/php]

How would I fit that into the code without getting syntax errors?

Well, if you have $variable=$_GET[“a-variable”] Then you are pulling this information from a HREF.
You are calling that code with something like <a href="blahblah.php?a-variable=‘3’> or other such
link.

In your sample link you posted, you have three links. (Or ANCHORS) which are HREF’s that have just
one argument named “order_var” . (With various values for it.) SO, if you wish to have more links
with other arugments, you would have to add the links in place with the new arugments.

I am guessing that our language is slowing us both up. My guess is that you want more options on your
HTML part of the page where the other three links current are. Just add more links. If you mean you
want more ARGUMENTS in those three current links, you can add more of them this way:

<a href=“index.php?order_var=’” . $ln_var . “&another_var=’” . $another_var . "’>Goals
So, you can add second, third or more variables using the ampersand.

Well, still not sure what you issue is. Hope that helps…

With proper pagination code you are already limited in the results that you get so it doesn’t matter how large the database is. You are still only getting a set amount of results returned.

To say that you have to get all the data sent to the browser is incorrect. If you are doing it that way you are doing it wrong and not using pagination to limit the results.

Then to sort those limited results using javascript is not an issue unless someone disables JavaScript, then the only thing that happens is they lose the ability to sort the limited results. If you can cite any security issue doing that I would love to see it.

Well, thank you for your comments, but, it stills does not help him. Telling him to learn PDO and study prepared statements is a nice thing in general, but, it does not help him solve his problem. There are lots of security issues using JS and JQuery if you do not know what you are doing. At the point his code is at, he can hardly handle query limits and pagination, PDO and the rest. Just my opinion.

Sponsor our Newsletter | Privacy Policy | Terms of Service