Ver Mensaje Individual
  #7 (permalink)  
Antiguo 03/08/2013, 14:29
Avatar de xSkArx
xSkArx
 
Fecha de Ingreso: marzo-2008
Ubicación: Chile
Mensajes: 945
Antigüedad: 16 años, 1 mes
Puntos: 96
Respuesta: duda con sort de bd

Hola, he probado y funciona la primera parte, intente agregar al script lo de ASC y DESC pero no funciona,

Código PHP:
Ver original
  1. <html>
  2. <head>
  3. <?php
  4. $ord = array('ASC', 'DESC');
  5. $campos = array('ID', 'TITULO', 'DESCRIPCION', 'FOTO', 'VIDEO', 'HORA', 'FECHA');
  6. // Defines el orden:
  7. $order = (empty($_GET['sort'])) ? 'ID' : strtoupper($_GET['sort']);
  8. $sorter= (empty($_GET['sorter'])) ? 'ASC' : strtoupper($_GET['sorter']);
  9. // Haces una validación
  10. if(!in_array($order, $campos)) {
  11.        $order = 'ID';
  12. }
  13. if(!in_array($sorter, $ord)) {
  14. $sorter= 'ASC';
  15. }
  16. include('config.php');
  17. ?>
  18. </head>
  19. <body>
  20. <table border="2">
  21. <?php
  22. $q="SELECT * FROM noticias ORDER BY '$sort' '$sorter'";
  23. $r=mysql_query($q,$conexion);
  24. $i=0;
  25. while($row=mysql_fetch_assoc($r)){
  26. $id=$row['ID'];
  27. $title=$row['TITULO'];
  28. $des= $row['DESCRIPCION'];
  29. echo "<tr>";
  30. echo "<td>".$id."</td><td>".$title."</td><td>".substr($des,0, 10)."</td>";
  31. echo "</tr>";
  32. $i++;
  33. }
  34. ?>
  35. </table>
  36. </body>
  37. </html>
Si no pongo ningun parametro en la url lo ordena por ID y ASC. Y si pongo parametros sigue igual
Pienso que el error esta en la consulta
Código PHP:
Ver original
  1. $q="SELECT * FROM noticias ORDER BY '$order' '$sorter'";

he puesto las. Variables con comilla simple y con doble comilla perk no funciona