Jump to content
boco_bc

VoteUP cu ajax, PHP, mysql

Recommended Posts

Posted (edited)

Vreau sa fac un vote up la posturile mele dar se pare ca nu reusesc sa fac cum vreau eu plus ca nu stiu javascript,ajax,etc, doar "descifrez" ce poate scrie in codul acela. O sa incerc sa scriu cat mai putin "carnat code".

Am asa in table "post"-> id,title,vote,user (le-am sters pe celelalte care nu ne intereseaza)

Si in table "post_vote_security" memoram cine a postat -> post_uniq_id,post_id,post_user

Vreau sa dau click pe "VOTE UP" si fara sa incarce pagina sa se adauge votul,asta cred ca am rezolvat! dar nu stiu cum sa se contorizeze votul cu +1 fara sa dau refresh la pagina si sa scrie in loc de "VOTE UP"-> "VOTED" ca text(neclickabil(ce ciudat suna))! iar dupa ce se da refresh la pagina , postul care s-a votat sa arate "VOTED" tot asa, ca text.

Exista live update din sql cu jquery? sa pot schimba numarul voturile in felul asta?sau cum se face?

La partea cand vreau sa scrie VOTED dupa refresh postului respectiv am incercat sa intru in amandoua tabele deodata (SELECT * from post LEFT JOIN post_vote_security on post.id=post_vote_security.post_id order by vote DESC) si credeam ca am rezolvat( verificam daca sesiunea coincide cu userul care a votat si in functie daca coincidea numele sau nu, aparea mesajul VOTED sau VOTE UP).....DAR daca POSTUL "VACA" il vota X si y......aparea de 2 ori acel post.....si m-a dat peste cap :)

Cum sa procedez sa imi arate VOTED sau VOTE UP la post?

<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(".vote").click(function()
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);

if (name=='up')
{
$(this).fadeIn(200).html('<img src="dot.gif" />');
$.ajax({
type: "POST",
url: "vote.php",
data: dataString,
cache: false,

success: function(html)
{
parent.html(html);
}
});
}
return false;
});
});
</script>
</head>
<body>
<?php
include('/configs/db.php');
/* INCEPE SESIUNEA*/
session_start();
$_SESSION['user']='Crancanel';
$user=$_SESSION['user'];

/* SELECTEZ POSTURILE*/
$sql=mysql_query("SELECT * from post order by vote DESC");
while($show=mysql_fetch_array($sql))
{$title=$show['title'];
$vote=$show['vote'];
$id=$show['id'];

echo 'Voturi: <a href="" id="'.$id.'" class="vote" name="up" >vote up</a>'.$vote.' -- '.$title.'<br><hr> ';

}
?>
</body>
</html>

<?php

if(isset($_POST['id']))
{
/*INCEPE SESIUNEA*/
session_start();
$user=$_SESSION['user'];
include('/configs/db.php');
$id=$_POST['id'];

/*VERIFIC DACA USERUL DIN SESIUNEA A MAI VOTAT LA POSTUL SELECTAT*/
$check=mysql_query("SELECT * from post_vote_security where post_id='$id'");
while($verifica=mysql_fetch_array($check)){$vuser=$verifica['post_user'];}


if($vuser==$user){echo 'Deja ai votat';}else{

/*SELECTEAZA VOTURILE DIN POSTUL SELECTAT */
$sel=mysql_query("select vote from post where id='$id'");
if($rand=mysql_fetch_array($sel))
{$vote=$rand['vote'];}
$vote=$vote+1;

/*ADAUGA +1 LA VOTURI*/
$insert="update post SET vote='$vote' where id='$id'";
if(mysql_query($insert)){echo 'Voted';}

/*ADAUGA USERUL CARE A VOTAT LA POSTUL SELECTAT*/
$insertsecure=mysql_query("INSERT into post_vote_security(post_uniq_id,post_id,post_user)VALUES(0,'$id','$user')");
}
}else{echo 'esec';}



?>

Edited by boco_bc
Posted (edited)

Nu inteleg de ce va intindeti asa de mult, invatarea fara practica nu se poate dar nici practica fara teorie.

Unde e identarea? Eu nu pot citii codu ala...

Edited by eusimplu

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...