Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/04/2012, 04:53
franjgg
 
Fecha de Ingreso: marzo-2007
Mensajes: 751
Antigüedad: 17 años, 6 meses
Puntos: 4
Marcador php y ajax actualizarlo cuando otros usuarios den su voto

Hola amigos como estan

Vereis tengo un marcador de votos el cual mira en la base de datos numero de votos y suma uno cuando el usuario lo pulsa.

La cosa es que si dos usuarios tiene abierto el archivo y uno de ellos da su voto al otro no se le actualiza el marcador, no se si abria alguna posible solucion para esto.

A ver si alguien tiene una idea, mil gracias de antemano.

Seria este el codigo:

HTML:
Código HTML:
<html>
<head>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<!-- 
tu codigo aqui
-->
<?php 
//AQUI DEBES BUSCAR LA FORMA DE COLOCAR EL NUMERO
//DEL ARTICULO DONDE SE HARA LA VOTACION 
$articulo = 7588;

include_once('conexion.php');

$noticia = $articulo;

$sql3 = mysql_query("select id_noticia, vistas from Tecnologia_noticias where id_noticia = '$noticia'",$conexion);
$array3 = mysql_fetch_array($sql3);

$votos3 = $array3['vistas'];
?>
<div id="votar">
    <?php echo $votos3.'Han votado por este articulo';?>
</div>
<div id="votar">
    <input type="submit" style="border:0; " name="vota_articulo" id="vota_articulo" value="Votar" onClick="ajax_votar(<?php echo $articulo; ?>)">		
</div>
</body>
</html> 

codigo ajax:

function objetoAjax(){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}

function ajax_votar(articulo){


divResultado1 = document.getElementById('votar');

//instanciamos el objetoAjax
ajax=objetoAjax();

ajax.open("GET", "votar.php?articulo="+articulo);
ajax.onreadystatechange=function() {


if (ajax.readyState==4) {
//mostrar resultados en esta capa
divResultado1.innerHTML = ajax.responseText;

divResultado1.style.display="block";

}
}
//como hacemos uso del metodo GET
//colocamos null
ajax.send(null);
}

y el PHP:

Código PHP:
<?php 
//recibimos la informacion desde ajax.js
$noticia $_GET['articulo'];
//$noticia = 7588;

//aqui debemos hacer una conexion a una base de datos ej:
include_once('conexion.php');
//$link=conectarse();



//revisamos si tiene datos anteriores el articulo
$sql mysql_query("select id_noticia from Tecnologia_noticias where id_noticia = '$noticia'"$conexion);
$filas mysql_fetch_row($sql);

//si se encuentra algun registro de este articulo entonces buscamos cuantos votos tiene
if ($filas 0) {
$sql2 mysql_query("select id_noticia, vistas from Tecnologia_noticias where id_noticia = '$noticia'"$conexion);
$array mysql_fetch_array($sql2);

$votos $array['vistas'];
$votos_total $votos 1;

//aqui actualizamos la tabla con el nuevo resultado
mysql_query("update Tecnologia_noticias set vistas = '$votos_total' where id_noticia = '$noticia' "$conexion);

} else {
//insertamos el voto ejemplo tabla votacion
mysql_query("insert into Tecnologia_noticias (id_noticia, vistas) values ('$noticia', 1)"$conexion);
}

//hacemos una ultima consulta para ver el total de votos
$sql3 mysql_query("select id_noticia, vistas from Tecnologia_noticias where id_noticia = '$noticia'",$conexion);
$array3 mysql_fetch_array($sql3);

$votos3 $array3['vistas'];

//aqui mostramos la informacion final en el div
echo $votos3;
echo 
"Han votado por este articulo";

?>