pastilhex Posted September 2, 2011 Share Posted September 2, 2011 Hello, Im trying to order and sort 3 columns by pulling data from Mysql db. For now im able to order by headings, but the second click would be to sort the same column and its not working. Could any body take a look to my code and give me some help ? <?php session_start(); if(!session_is_registered(myusername) || $_SESSION['nivel']==2){ header("location:index.php"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="description" content="" /> <meta name="keywords" content="" /> <meta name="author" content="" /> <link rel="stylesheet" type="text/css" href="style.css" media="screen" /> <title>Gestão e Controlo de Funcionários</title> </head> <body> <div id="wrapper"> <div id="header"><h1 align="center"><?php include('includes/header.php'); ?></h1></div><!-- end #header --> <div id="nav"><?php include('includes/nav.php'); ?></div> <div id="logout" align="right"><?php include('includes/nav_logout.php'); ?></div> <div id="content"> <?php if(isset($_GET['orderby'])){$order = $_GET['orderby'];}else{$order = "nome";} if(!isset($_GET['sortby'])){$newsort="ASC";}else{ switch ($newsort){ case "ASC": $newsort="DESC"; break; case "DESC": $newsort="ASC"; break; } } ?> <table width="950" cellpadding="2" cellspacing="1" class="texto"> <tr> <td width="130" height="30" bgcolor="#CBDCED"><a href="<? echo $_SERVER['PHP_SELF']."?orderby=nome&sortby=$newsort";?>">Nome</a></td> <td width="130" height="30" bgcolor="#CBDCED"><a href="<? echo $_SERVER['PHP_SELF']."?orderby=apelido&sortby=$newsort";?>">Apelido</a></td> <td width="130" height="30" bgcolor="#CBDCED"><a href="<? echo $_SERVER['PHP_SELF']."?orderby=num_funcionario&sortby=$newsort";?>">Nº de Funcionário</a></td> <td width="130" height="30" bgcolor="#CBDCED">Inserir Horas Extras</td> <td width="130" height="30" bgcolor="#CBDCED">Consultar Horas Extras</td> <td width="130" height="30" bgcolor="#CBDCED">Editar Perfil</td> <td bgcolor="#CBDCED">Última Visita</td> </tr> <?php require('config.php'); $sql=mysql_query("SELECT * FROM `infopessoal` ORDER BY `$order` $newsort"); while($row = mysql_fetch_array($sql)) { echo "<tr>"; echo "<td>" . $row['nome'] . "</td>"; echo "<td>" . $row['apelido'] . "</td>"; echo "<td>" . $row['num_funcionario'] . "</td>"; echo "<td><a href='insereh.php?id={$row['func_id']}'><img src='images/insert.png' border='0'></a></td>"; echo "<td><a href='detalheh.php?id={$row['func_id']}'><img src='images/lupa.png' border='0'></a></td>"; echo "<td><a href='modificaf.php?id={$row['func_id']}'><img src='images/perfil.png' border='0'></a></td>"; echo "</tr>"; } mysql_close($link); ?> </table> </div><!-- end #content --> <div id="footer"><?php include('includes/footer.php'); ?></div><!-- end #footer --> </div><!-- End #wrapper --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/246306-little-help-sorting-table/ Share on other sites More sharing options...
MasterACE14 Posted September 3, 2011 Share Posted September 3, 2011 the 'switch' is checking a variable that doesn't exist at that point in the script. Change this: switch ($newsort){ case "ASC": $newsort="DESC"; break; case "DESC": $newsort="ASC"; break; } to this: switch ($_GET['sortby']){ case "ASC": $newsort="DESC"; break; case "DESC": $newsort="ASC"; break; } and validate the $_GET's Quote Link to comment https://forums.phpfreaks.com/topic/246306-little-help-sorting-table/#findComment-1265012 Share on other sites More sharing options...
pastilhex Posted September 3, 2011 Author Share Posted September 3, 2011 Thanks MasterACE14, That really solved my problem You're the man !! Quote Link to comment https://forums.phpfreaks.com/topic/246306-little-help-sorting-table/#findComment-1265141 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.