HELP!!
Ok I'm new to PHP but I'm loving every minute of it so please be patient. As the title says, I'm having issues sorting by ASC/DESC on click. Below is my code that does not work. I've chopped a bunch out so if all of the IF/ELSE don't make sense keep that in mind. When I click on the link the $sort variable doesn't have a value in the link.
<?php
//*************************************************
$default_sort = 'ASC';
$allowed_sort = array ('DESC','ASC');
if (!isset ($_GET['sort']) ||
!in_array ($_GET['sort'], $allowed_sort))
{
$sort = $default_sort;
}
else
{
$sort = $_GET['sort'];
$sort=($sort=="DESC" ? "ASC" : "DESC");
}
//*****************************************************
include("dbconnect.php");
$query1 = "SELECT DISTINCT * FROM stats ORDER BY SCORE $sort LIMIT 1 ";
$result1 = mysql_query($query1);
if(!$result = mysql_query($query1))
{
print ("Query could not be executed.\n");
}
$r = printTable($result);
function printTable($result)
{
if(!$row = mysql_fetch_assoc($result))
{
return false;
}
print("<p align='center'> <span class='storytitle style2'><a href='test.php?sort=$sort'>Air to Air</a></span>");
do
{
print("<td class='row1ld' style='text-align: right;' width='76'> {$row['rank']}");
if("{$row['rank']}" <= "{$row['oldrank']}")
{
print("<img src='arrowup.jpg' hspace='3' title='Prev rank: {$row['oldrank']}'></img>");
}
else
{
print("<img src='arrowdown.jpg' hspace='3' title='Prev rank: {$row['oldrank']}'></img>");
}
}
while ($row = mysql_fetch_assoc($result));
return true;
}
?>
But if I remove the functions and the IF statements it works.
<?php
//*************************************************
$default_sort = 'ASC';
$allowed_sort = array ('DESC','ASC');
if (!isset ($_GET['sort']) ||
!in_array ($_GET['sort'], $allowed_sort))
{
$sort = $default_sort;
}
else
{
$sort = $_GET['sort'];
$sort=($sort=="DESC" ? "ASC" : "DESC");
}
//*****************************************************
include("dbconnect.php");
$query1 = "SELECT DISTINCT * FROM stats ORDER BY SCORE $sort LIMIT 1 ";
$result1 = mysql_query($query1);
if(!$result = mysql_query($query1))
{
print ("Query could not be executed.\n");
}
print("<p align='center'> <span class='storytitle style2'><a href='test.php?sort=$sort'>Air to Air</a></span>");
?>
What am I doing wrong? Thanks for the help.