Código PHP :
<?
include("conexion.php");
//parámetro de entrada tipobusqueda:
//1: ordenado por popularidad
//2: ordenado por fecha alta
//3: al azar
/*$post_tipo = 2;
$post_idusuario = 0;
*/
$tipo = $post_tipo;
$idusuario = $post_idusuario;
switch ($tipo) {
case 1:
$sql = "select distinct(f.id) as idFoto, f.numVotos, r.nombre, r.apellido1, r.apellido2, f.fechaAlta, f.foto, coalesce(f.ganadorMes,0) as ganador from tabla_fotos f, tabla_registrados r where f.idUsuario = r.id and f.estado=1 and f.idUsuario not in (select f2.idUsuario from tabla_fotos f2 where f2.ganadorMes<>0) group by f.id order by f.numVotos desc";
break;
case 2:
//$sql = "select distinct(f.id) as idFoto, f.numVotos, r.nombre, r.apellido1, r.apellido2, f.fechaAlta, f.foto from tabla_fotos f, tabla_registrados r where f.idUsuario = r.id and f.estado=1 group by f.id order by f.fechaAlta desc ";
//LOS MAS VOTADOS DEL MES ACTUAL.
//$sql = "select distinct(f.id) as idFoto, f.numVotos, r.nombre, r.apellido1, r.apellido2, f.fechaAlta, f.foto from tabla_fotos f, tabla_registrados r, tabla_votos as v where f.idUsuario = r.id and f.estado=1 and v.idFoto=idFoto and month(v.fecha)=month(now()) and year(v.fecha)=year(now()) group by f.id order by f.numVotos desc";
//las fotos ordenadas por subida del mes actual
$sql = "select distinct(f.id) as idFoto, f.numVotos, r.nombre, r.apellido1, r.apellido2, f.fechaAlta, f.foto, coalesce(f.ganadorMes,0) as ganador from tabla_fotos f, tabla_registrados r, tabla_votos as v where f.idUsuario = r.id and f.estado=1 and v.idFoto=idFoto and month(v.fecha)=month(now()) and year(v.fecha)=year(now()) and f.idUsuario not in (select f2.idUsuario from tabla_fotos f2 where f2.ganadorMes<>0) group by f.id order by f.fechaAlta desc";
break;
case 3:
$ind = rand(1,6);
$sql = "select distinct(f.id) as idFoto, f.numVotos, r.nombre, r.apellido1, r.apellido2, f.fechaAlta, f.foto, coalesce(f.ganadorMes,0) as ganador from tabla_fotos f, tabla_registrados r where f.idUsuario = r.id and f.estado=1 and f.idUsuario not in (select f2.idUsuario from tabla_fotos f2 where f2.ganadorMes<>0) group by f.id order by ".$ind." desc";
break;
case 4:
$sql = "select v.idFoto, count(*) as numVotos, r.nombre, r.apellido1, r.apellido2, f.foto, coalesce(f.ganadorMes,0) as ganador from tabla_votos v, tabla_fotos f, tabla_registrados r where month(v.fecha)= month(curdate()) and year(v.fecha)=year(now()) and f.id = v.idFoto and f.idUsuario = r.id and f.estado='1' and f.idUsuario not in (select f2.idUsuario from tabla_fotos f2 where f2.ganadorMes<>0) group by v.idFoto order by numVotos desc";
break;
}
$result = mysql_query($sql, $DB);
echo ('<?xml version="1.0" encoding="utf-8"?>');
echo ('<raiz>');
$i = 1;
while ($row = mysql_fetch_assoc($result)){
//si la fecha actual es mayor que la anterior, hay que parar porque ya tenemos los ganadores de cada mes. A partir de este punto, empezarÃa con los segundos de cada mes,...
if ($i != 1){
if ($fechaAnt < $row['fecha']){
exit();
}
}
if ($idusuario > 0 ){
$sql = "select * from tabla_votos where idUsuario = ".$idusuario." and idFoto = ".$row['idFoto'];
$result2 = mysql_query($sql, $DB);
if (mysql_num_rows($result2) > 0){
$ha_votado = 1;
}
else {
$ha_votado = 0;
}
}
else {
$ha_votado = -1;
}
$fechaAnt = $row['fecha'];
$nombre = $row['nombre'];
$apellido1 = $row['apellido1'];
$apellido2 = $row['apellido2'];
$numvotos = $row['numVotos'];
$nombreFoto = $row['foto'];
$idFoto = $row['idFoto'];
$ganador = $row['ganador'];
if ($ganador > 0){ //NO PUEDE VOTAR PORQUE YA HA RESULTADO GANADOR EN OTRO MES
$ha_votado = -2;
}
echo('<foto_'.$i.'>');
echo('<usuario>'.$nombre.' '.$apellido1.' '.$apellido2.'</usuario>');
echo('<numVotos>'.$numvotos.'</numVotos>');
echo('<haVotado>'.$ha_votado.'</haVotado>');
echo('<nombreFoto>'.$nombreFoto.'</nombreFoto>');
echo('<idfoto>'.$idFoto.'</idfoto>');
echo('</foto_'.$i.'>');
$i++;
}
echo ('</raiz>');
?> 