Jump to content

php mysql switching between asc and desc


tekrscom

Recommended Posts

Hi, I'm trying to figure out a simple way to switch between ASC and DESC in my query when a user clicks on the same ORDER BY hyperlink...

Here's my code...

        	<table width="1300">
            	<tr bgcolor="#C0C0FF">
                	<td><a href="?Sort=UserID">UserID</a></td>
                	<td><a href="?Sort=Username">Username</a></td>
                	<td><a href="?Sort=Email">Email</a></td>
                	<td><a href="?Sort=EmailValidated">Email Validated</a></td>
                	<td><a href="?Sort=MailingList">Mailing List</a></td>
                	<td><a href="?Sort=PrivacySetting">Privacy Setting</a></td>
                	<td><a href="?Sort=AccountType">Account Type</a></td>
                	<td><a href="?Sort=StartDate">Start Date</a></td>
                	<td><a href="?Sort=LogOnDate">Last Log On Date</a></td>
                	<td><a href="?Sort=Gender">Gender</a></td>
                </tr>
<?
if ($ASCDESC = 'ASC'){$ASCDESC = 'DESC';}
if ($ASCDESC = 'DESC'){$ASCDESC = 'ASC';}
$query = "SELECT * FROM Users WHERE 1 ORDER BY $_GET[sort] $ASCDESC";
$results = mysql_query($query);
$counter = 0;
while($row  =  mysql_fetch_array($results)) {
$counter += 1;
    if($counter % 2) {
        echo '<tr bgcolor="#C0C0C0">';
    }
    else {
        echo '<tr bgcolor="#FFFFFF">';
    }
    echo '
                <td>' . $row['UserID'] . '</td>
                <td>' . $row['Username'] . '</td>
                <td>' . $row['Email'] . '</td>
                <td>' . $row['EmailValidated'] . '</td>
                <td>'; if ($row['MailingList'] == 1){echo 'Yes';}else{echo 'No';} echo '</td>
                <td>' . $row['PrivacySetting'] . '</td>
                <td>' . $row['AccountType'] . '</td>
                <td>' . $row['StartDate'] . '</td>
                <td>' . $row['LogOnDate'] . '</td>
                <td>' . $row['Gender'] . '</td>
            </tr>
    ';
}
?>
</table>

I would have thought for sure this would have worked... but it didn't...

if ($_SESSION['Sort'] == $_GET['Sort'] and $_SESSION['ASCDESC'] == 'DESC')	{
$_SESSION['ASCDESC'] = 'ASC';
}
if ($_SESSION['Sort'] == $_GET['Sort'] and $_SESSION['ASCDESC'] == 'ASC')	{
$_SESSION['ASCDESC'] = 'DESC';
}
if (!isset($_SESSION['ASCDESC']))	{
$_SESSION['ASCDESC'] = 'DESC';
}
if (!isset($_GET['Sort']))	{
$_SESSION['Sort'] = 'UserID';
}
if (isset($_GET['Sort']))	{
$_SESSION['Sort'] = $_GET['Sort'];
}
$query = "SELECT * FROM Users WHERE 1 ORDER BY $_SESSION[sort] $_SESSION[ASCDESC]";

if ($ASCDESC = 'ASC'){$ASCDESC = 'DESC';}
if ($ASCDESC = 'DESC'){$ASCDESC = 'ASC';}

 

is incorrect. In your "if" statement, use double equal "==" signs, not single equal.

 

This is correct:

if ($ASCDESC == 'ASC'){$ASCDESC = 'DESC';}
if ($ASCDESC == 'DESC'){$ASCDESC = 'ASC';}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.