I am trying to use $_GET to take a parameter so that if a table heading it selected it will go through my if else statements and sort accordingly.
However, I'm not having much success. I have it set up to sort by ID in ascending order first and I'm just testing by clicking for ID in descending order. Any suggestions would be useful.
if(!isset($_SESSION['userid']))
{
header("Location: login.php");
exit();
}
try
{
$sql = 'SELECT * FROM members ORDER by ID';
$result = $pdo->query($sql);
if($x == 2)
{
$sql = 'SELECT * FROM members ORDER by ID DESC';
$result = $pdo->query($sql);
}
}
catch (PDOException $e)
{
echo 'Error fetching results: ' . $e->getMessage();
exit();
}
while ($row = $result->fetch())
{
$registrations[] = array(
'ID' => $row['ID'],
'email' => $row['email'],
'fname' => $row['fname'],
'mi' => $row['mi'],
'institution' => $row['institution'],
'lname' => $row['lname'],
'uname' => $row['uname']);
}
echo '<table>';
echo '<thead><tr><th>Options</th><th>ID<a href="userlist.php?x=1">
▴</a> <a href="userlist.php?x=2">▾</a></th><th>Name
<ahref="userlist.php?x=3">▴</a><a href="userlist.php?x=4"> ▾
</a></th><th>Institution <a href="userlist.php?x=5">▴</a>
<a href="userlist.php?x=6">▾</a></th><th>Username
<a href="userlist.php?x=7">▴</a>
<a href="userlist.php?x=8">▾</a></th><th>Email
<a href="userlist.php?x=9">▴</a>
<a href="userlist.php?x=10">▾</a></th></tr></thead>';
if($_SESSION['status'] == 1)
{
foreach ($registrations as $user)
{
echo '<tbody><tr><td><a href="userdetails.php?x=' .$user['ID']
.'">VIEW</a> </td>
<td>'.$user['ID'].'</td>
<td>'.$user['lname'].", ". $user['fname']." ". $user['mi'].".".'</td>
<td>'.$user['institution'].'</td>
<td>'.$user['uname'].'</td>
<td>'.$user['email'].'</td> </tr> </tbody>';
}
}
else
{
echo '><tr><td><a href="userdetails.php?x='
.$_SESSION['userid'].'">VIEW</a></td> <td>'. $_SESSION['userid'].' </td>
<td>'.$_SESSION['lname'].", ". $_SESSION['fname']." ".
$_SESSION['mi'].".".'</td><td>'.$_SESSION['institution'].'</td>
<td>'.$_SESSION['uname'].'</td><td>'.$_SESSION['email'].'</td></tr>';
}
echo '</table> ';
$x = $_GET['x'];
?>
userlist.php?x=1? I also noticed you are creating the$xvariable at the bottom of your script, which means any references to$xwont be read at the top.hrefrequest?