Foros del Web » Programando para Internet » PHP »

Duda con sistema de "likes" (PHP,AJAX,SQL).

Estas en el tema de Duda con sistema de "likes" (PHP,AJAX,SQL). en el foro de PHP en Foros del Web. Tengo unos archivos que he buscado en tutoriales y los he modificado para intentar hacer un sistema de "likes" en una web pero no se ...
  #1 (permalink)  
Antiguo 23/04/2015, 06:34
 
Fecha de Ingreso: febrero-2015
Mensajes: 61
Antigüedad: 9 años, 2 meses
Puntos: 0
Duda con sistema de "likes" (PHP,AJAX,SQL).

Tengo unos archivos que he buscado en tutoriales y los he modificado para intentar hacer un sistema de "likes" en una web pero no se implementarlo bien, ya que con las variables que yo tengo y mis archivos no se implementar bien el codigo, alguien me ayuda?

BASE DE DATOS
Código SQL:
Ver original
  1. #   Nombre  Tipo    Cotejamiento    Atributos   Nulo    Predeterminado  Extra
  2.     1   id  INT(11)         No  Ninguna AUTO_INCREMENT 
  3.     2   post_id INT(11)         No  Ninguna    
  4.     3   autor_id    INT(11)         No  Ninguna        
  5.     4   fecha   datetime            No  Ninguna


CLASE FACEBOOK
Es una clase que tengo varios metodos para enviar mensaje, enviar comentario... dejo la parte donde se inserta una publicación.

Linea 37, el codigo de los likes.

Código PHP:
Ver original
  1. public function insert_entry($message)
  2.     {
  3.         self::set_names();
  4.         $user = $_SESSION['username'];
  5.         $userid = $_SESSION['id'];
  6.         $ema = $_SESSION['email'];
  7.         $fecha = date('Y-m-d H:i:s');
  8.         $query = $this->connect->prepare("INSERT INTO messages VALUES ('',?,?,?)");
  9.         $query->bindParam(1,$userid);
  10.         $query->bindParam(2,$message);
  11.         $query->bindParam(3,$fecha);
  12.         $query->execute();
  13.         $id=$this->connect->lastInsertId();
  14.        
  15.  
  16.         //insertamos un mensaje en la table comments
  17.         $firstcomment = $this->connect->prepare("INSERT INTO comments (user_comment, messageid, comment,date_comment) VALUES (?,?,?,?)");
  18.         $firstcomment->bindParam(1,$user);
  19.         $firstcomment->bindParam(2,$id);
  20.         $firstcomment->bindParam(3,$comment);
  21.         $firstcomment->bindParam(4,$fecha);
  22.         $firstcomment->execute();
  23.  
  24.         $comments = $this->connect->prepare("SELECT * FROM comments where messageid = $id order by id desc");
  25.         $comments->execute();
  26.         $result = $comments->fetch();
  27.         ?><br><br>
  28.  
  29.  
  30.  
  31.         <div class="comments" id="lastmessage">
  32.             <img src="<?=$this->get_gravatar($ema)?>/?d=404&f=y" width="40" height="40" /><br><br>
  33.             <?=$user?><span style="float: right;"><?=$fecha?></span><br><br>
  34.             <?=$this->link($message)?>
  35.             <br>
  36.             <br>
  37.             <span id="cantidad">0</span> <a href="#" id="megusta" post-id="<?=$id;?>" autor-id="<?=$user;?>">Me gusta</a>
  38.            
  39.         </div>
  40.  
  41.  
  42.         <div id="form_messages" class="lastmessage" style="width: 380px; margin-left: 250px; margin-top: 40px">
  43.         <form method="post" action="">
  44.             <textarea name="comment" class="comment" cols="44" rows="2" maxlength="250"  id="comment_textarea"></textarea>
  45.             <br>
  46.             <input type="submit"  value="Comment"  id="" class="send_comment"/>
  47.         </form>
  48.         </div>
  49.  
  50.  
  51.        
  52.     <?php
  53.     }






HEADER.PHP

Es la cabecera del archivo "Index.php" que viene después. En la linea 20 incluyo el archivo "megusta.js".

Código PHP:
Ver original
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5.  
  6.     <link rel="stylesheet" type="text/css" href="css/style.css">
  7.     <link rel="stylesheet" type="text/css" href="css/style2.css">
  8.  
  9.  
  10.  
  11.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  12.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
  13.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
  14.     <link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/paper/bootstrap.min.css" rel="stylesheet">
  15.  
  16.     <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
  17.     <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
  18.  
  19.     <script type="text/javascript" src="js/functions.js"></script>
  20.     <script src="js/megusta.js"></script>
  21.  
  22.     <title>Let's Travel</title>
  23. </head>
  24.  
  25. <body>
  26. <div class="container_12" style="background: #fefefe">
  27.    <div class="grid_12" id="cabecera"></div>




INDEX.PHP

La página principal, pongo solo el div del comentario a mostrar, a partir del span es la linea de codigo donde quiero mostrar los likes. En la linea 7 es el codigo de los "likes".

Código PHP:
Ver original
  1. <div class="grid_5 push_3 messages" id="other_comments" style="margin-top: 10px">
  2.         <img src="<?=$gravatar?>" width="40" height="40" /><br><br>
  3.         <?=$message['username']?><span style="float: right;"><?=$message['date_message']?></span><br><br>
  4.         <?=$nuevo->link($message['message'])?>
  5.         <br>
  6.         <br>
  7.         <span id="cantidad">0</span> <a href="#" id="megusta" post-id="<?=$nuevo->link($message['message'])?>" autor-id="<?=$message['username']?>">Me gusta</a>
  8.         </div><br><br><br><br><br><br><br><br>



MEGUSTA.JS
No se si estan bien puestas las variables.

Código Javascript:
Ver original
  1. $(document).ready(function() {
  2.  
  3. $('a#megusta').click(function() {
  4. var post_id = $(this).attr('id');
  5. var autor_id = $(this).attr('user');
  6.  
  7. var cadena = 'user='+autor_id+'&id='+post_id;
  8.  
  9.   $.ajax({
  10.     type: "POST",
  11.     url: "megusta.php",
  12.     data: cadena,
  13.     dataType: "json",
  14.     success: function(respuesta){
  15.     if (respuesta.error) {
  16.     alert(respuesta.msj_error);
  17.     return false;
  18.     }
  19.    
  20.    $("a#megusta").addClass("megusto");
  21.    $("a#megusta span#cantidad").html(respuesta.cantidad);
  22.    return false;
  23.  
  24.    }//FUNCION
  25.  
  26. });//AJAX
  27.  
  28. return false;
  29.  
  30. });//me gusta
  31.  
  32. }



MEGUSTA.PHP
Como en el anterior, no se si estan bien puestas las variables y demas declaraciones para autor_id y post_id

Código PHP:
Ver original
  1.  
  2.  
  3.  
  4.  
  5.     $host = "localhost";
  6.     $user = "root";
  7.     $pass = "";
  8.     $bd = "facebook";
  9.     $dbc;
  10.  
  11.     $conexion = mysqli_connect($host, $user, $pass, $bd);
  12.  
  13.     if($conexion->connect_errno){
  14.       echo "ERROR AL CONECTARSE ($conexion->connect_errno)";
  15.     }
  16.  
  17. $post_id = (int)$_POST["id"];
  18. $autor_id = (int)$_POST["user"];
  19. $proceso = array("error"=>false,"msj_error"=>"","yagusta"=>false,"cantidad"=>0);
  20.  
  21. //verificamos que los parametros esten correctos
  22. if (empty($post_id) || empty($autor_id)) {
  23. $proceso["error"] = true;
  24. $proceso["msj_error"] = "Ocurrio un error, por favor intentalo de nuevo";
  25. echo json_encode($proceso);
  26. exit();
  27. }
  28.  
  29. // ya dio me gusta?, indiquemosle al usuario
  30. $yagusto = mysql_query("SELECT COUNT(*) FROM `wk_megusta` WHERE autor_id=$autor_id AND post_id=$post_id");
  31.  
  32. if ($yagusto > 0) {
  33. $proceso["error"] = true;
  34. $proceso["msj_error"] = "Ya te gusta esta publicacion";
  35. echo json_encode($proceso);
  36. exit();
  37. }
  38.  
  39. //Almacenamos el nuevo ME GUSTA
  40.  $consulta_ajax = mysql_query("INSERT INTO `wk_megusta` (autor_id,post_id,fecha) VALUES($autor_id,$post_id,NOW())");
  41.  
  42. if ($consulta_ajax) {
  43. $proceso["cantidad"] = mysql_query("SELECT COUNT(*) FROM `wk_megusta` WHERE post_id=$post_id");
  44. echo json_encode($proceso);
  45. exit();
  46. }
  47.  
  48. ?>

Última edición por IVICP3; 23/04/2015 a las 06:42

Etiquetas: fecha, html, likes, mysql, select, sistema, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:31.