tekrscom Posted August 24, 2009 Share Posted August 24, 2009 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> Link to comment https://forums.phpfreaks.com/topic/171573-php-mysql-switching-between-asc-and-desc/ Share on other sites More sharing options...
tekrscom Posted August 24, 2009 Author Share Posted August 24, 2009 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]"; Link to comment https://forums.phpfreaks.com/topic/171573-php-mysql-switching-between-asc-and-desc/#findComment-904786 Share on other sites More sharing options...
obay Posted August 24, 2009 Share Posted August 24, 2009 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';} Link to comment https://forums.phpfreaks.com/topic/171573-php-mysql-switching-between-asc-and-desc/#findComment-904818 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.