2 OPCIONES PARA SOLUCIONARLO Mírate esto es lo que arme:
!.- paso de parámetros por método GET dentro de la misma URL esto hará que se recargue la pagina y envié el campo a ordenar:
LA TABLA QUE TENGO PARA ESO ES ESTA:
organice de esta forma:
Para la opción 1 “
paso de parámetros por el URL” quedo así -->
index.php, acciones.php, function.php, BD_SQL.php y la CSS estilos.css
CODIGO:
index.php Código PHP:
<? require_once("acciones.php"); ?>
<? require_once("function.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ordenar por Columnas</title>
<link rel="stylesheet" type="text/css" href="estilos.css" />
</head>
<body>
<h1 align="center">Ordenar registros de una tabla con Ajax y PHP </h1>
<div id="listado">
<table>
<tr class='encabezado'><td><a href="index.php?orden=name">Nombre</a></td><td><a href="index.php?orden=hits">Jugadas</a></td><td><a href="index.php?orden=rating">Puntuacion</a></td><td><a href="index.php?orden=highscores">Ranking</a></td><td><a href="index.php?orden=advert_id">Guia</a></td><td><a href="index.php?orden=category_id">Categoria</a></td></tr>
<?
for($i=0;$i<count($row);$i++)
{
echo "<tr><td>".$row[$i]['name']."</td><td>".$row[$i]['hits']."</td><td>".numEstrellas($row[$i]['rating'])."</td><td>".ranking($row[$i]['highscores'])."</td><td>".guia($row[$i]['advert_id'])."</td><td>".categoria($row[$i]['category_id'])."</td></tr>";
}
?>
</table>
</div>
</body>
</html>
acciones.php
Código PHP:
<?
$baseDatos = 'pruebas';
$servidor = 'localhost';
$usuario = 'root';
$contrasena = '1475369';
require_once('BD_SQL.php');
$conn = new BD_SQL;
$conn->conectar($baseDatos,$servidor,$usuario,$contrasena);
$conn->execute("SET NAMES 'latin1'");
if(isset($_REQUEST['orden']))
{
$sql = "SELECT * FROM ava_games order by ".$_REQUEST['orden'];
}
else{
$sql = "SELECT * FROM ava_games";
}
$conn->execute($sql);
$row = $conn->getRows();
?>
function.php
Código PHP:
function numEstrellas($total)
{
$t = $total;
if($t==0){$s .= '<center>Sin puntuar.</center>';}
else{
for($i=0;$i<5;$i++)
{
if($i < $t){
$s .= "<img src='http://www.mjuegos.net/templates/red/images/star.png'>";
}else{
$s .= "<img src='http://www.mjuegos.net/templates/red/images/empty_star.png'>";
}
}
}
return $s;
}
//
function ranking($num)
{
if ($num){
$result = "<center><img src='http://www.mjuegos.net/images/trophy_smaller.png'></center>";
}
else{
$result = " ";
}
return $result;
}
//
function guia($num)
{
if ($num == 7) {
$result = "<center><img src='http://foro.mjuegos.net/Themes/default/images/post/pagina.gif'></center>";
}
else {
$result = " ";
}
return $result;
}
//
function categoria($num)
{
if ($num == 103) {
$result = "<center><a style='text-decoration:none; color:grey' href='http://www.mjuegos.net/index.php?task=category&id=103&sortby=newest&page=1'>Acción</a></center>";
}
else if ($num == 2) {
$result = "<center><a style='text-decoration:none; color:grey' href='http://www.mjuegos.net/index.php?task=category&id=2&sortby=newest&page=1'>Aventuras</a></center>";
}
else if ($num == 102) {
$result = "<center><a style='text-decoration:none; color:grey' href='http://www.mjuegos.net/index.php?task=category&id=102&sortby=newest&page=1'>De mesa</a></center>";
}
else if ($num == 7) {
$result = "<center><a style='text-decoration:none; color:grey' href='http://www.mjuegos.net/index.php?task=category&id=7&sortby=newest&page=1'>Estrategia</a></center>";
}
else if ($num == 100) {
$result = "<center><a style='text-decoration:none; color:grey' href='http://www.mjuegos.net/index.php?task=category&id=100&sortby=newest&page=1'>Habilidad</a></center>";
}
else if ($num == 9) {
$result = "<center><a style='text-decoration:none; color:grey' href='http://www.mjuegos.net/index.php?task=category&id=9&sortby=newest&page=1'>Para los más peques</a></center>";
}
return $result;
}
BD_SQL.php
Código PHP:
<?
class BD_SQL{
// variables de conexion
var $BaseDatos;
var $Servidor;
var $Usuario;
var $Clave;
// Identificador de conexion y Consultas
var $Conexion_ID = 0;
var $Consulta_ID = 0;
// Numero y Texto de Error
var $Errno = 0;
var $Error = "";
// Constructor de la clase
function BD_SQL($bd = "",$host = "", $user = "", $pass = ""){
$this->BaseDatos = $bd;
$this->Servidor = $host;
$this->Usuario = $user;
$this->Clave = $pass;
}
// Conexion a la Base de Datos
function conectar($bd, $host, $user, $pass){
if($bd != "") $this->BaseDatos = $bd;
if($host != "") $this->Servidor = $host;
if($user != "") $this->Usuario = $user;
if($pass != "") $this->Clave = $pass;
// Enlace con el servidor
$this->Conexion_ID = mysql_connect($this->Servidor,$this->Usuario,$this->Clave);
if(!$this->Conexion_ID){
$this->Error = "Ha fallado la Conexion con el Servidor de Base de Datos";
return 0;
}
// Seleccionamos la Base de Datos
if(!@mysql_select_db($this->BaseDatos, $this->Conexion_ID)){
$this->Error = "Imposible Abrir la Base de Datos : ".$this->BaseDatos;
return 0;
}
mysql_query("SET NAMES 'utf8'");
// si Todo fue un exito obtenemos el identificador de la conexion de lo contrario devuelve 0
return $this->Conexion_ID;
}
//
function execute($sql = ""){
if($sql == ""){
$this->Error = "No ha especificado una consulta SQL";
return 0;
}
$this->Consulta_ID = @mysql_query($sql, $this->Conexion_ID);
if(!$this->Consulta_ID){
return $this->Error = "Error al ejecutar la consulta";
}
return $this->Consulta_ID;
}
//
function getrows(){
while($fila = mysql_fetch_array($this->Consulta_ID))
{
$registros[] = $fila;
}
return $registros;
} // fin de la funcion
} // fin de la clase
?>
estilos.css
Código HTML:
@charset "utf-8";
/* CSS Document */
table{
border: 1px solid #CCCCCC;
margin: 0 auto;
background-color:#FFFFCC;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
color:#000066;
}
table a{
text-decoration:none;
cursor:pointer;
}
td {
padding-top:2px;
padding-bottom:2px;
border-top: 1px solid #FFFF00;
border-right: 1px solid #FFFF00;
padding-left:10px;
padding-right:10px;
color:#666666;
}
.encabezado{
background-color:#FFCC00;
}
.encabezado_selec{
background-color:#FF9900;
}
El mismo ejemplo pero usando
AJAX con JQUERY te recomiendo este
FrameWork ya que es muy sencillo de aplicar y usar
ahora fijate que son los mismo archivos solo con una ligeras modificaciones
en el
index.php solo agregar las siguientes lineas dentro del <head></head>
Código HTML:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
para la libreria JQUERY descargala desde su web o desde este enlace [URL="http://www.macrowebdigital.com/forosdelweb/images/jquery.js"]JQUERY[/URL]
CODIGO:
script.js Código HTML:
$(document).ready(function(){
$('#listado a').click(function(e){
e.preventDefault();
orden = $(this).attr("href");
filtro = obtenerParam('orden',orden);
pagina = 'ordenaTabla.php?orden='+filtro+'&rand='+Math.random()*999999999;
$.ajax({
url : pagina,
success : function(data){
$("#listado").html(data);
}
});
});
});
//
function obtenerParam(name,pagina){
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp(regexS);
var tmpURL = pagina;
var results = regex.exec(tmpURL);
if(results == null)
return "";
else
return results[1];
}
Y por último el archivo que aplica la acciones en la tabla de resultados:
ordenaTabla.php Código PHP:
<? require_once("acciones.php"); ?>
<? require_once("function.php"); ?>
<script type="text/javascript" src="script.js"></script>
<table>
<tr class='encabezado'><td><a href="index.php?orden=name">Nombre</a></td><td><a href="index.php?orden=hits">Jugadas</a></td><td><a href="index.php?orden=rating">Puntuacion</a></td><td><a href="index.php?orden=highscores">Ranking</a></td><td><a href="index.php?orden=advert_id">Guia</a></td><td><a href="index.php?orden=category_id">Categoria</a></td></tr>
<?
for($i=0;$i<count($row);$i++)
{
echo "<tr><td>".$row[$i]['name']."</td><td>".$row[$i]['hits']."</td><td>".numEstrellas($row[$i]['rating'])."</td><td>".ranking($row[$i]['highscores'])."</td><td>".guia($row[$i]['advert_id'])."</td><td>".categoria($row[$i]['category_id'])."</td></tr>";
}
?>
</table>
aqui dejo los archivos para descargar:
ORDENACION POR PASO DE PARAMETROS POR EL URL [URL="http://www.macrowebdigital.com/forosdelweb/images/ordenacion01.zip"]DESCARGAR[/URL] ORDENACION POR AJAX USANDO JQUERY [URL="http://www.macrowebdigital.com/forosdelweb/images/ordenacion02.zip"]DESCARGAR[/URL]