xcislav Posted April 8, 2014 Share Posted April 8, 2014 (edited) plus one ("+1") "like button"-voting system I just want to have non-authorization (non-voting "system" at all - just for like button). I want to click "Like"-button key and want to have other users which visit this webpage to see this. Like-button with no additional things (no dislike) just: 1. Press - HTML/Javascript 2. Send PHP (PDO!) 3. Receive Textfile (may be.. mysql) *it has to be as simple as it could be* 4. Show +1, which would be visible to other visitors (ajax/js - optional). That example to be working. I have mysql and apache and php PDO (php5.5.6)! This is with no PDO and I don't think It could work (php5.5.6) and in any case.... //connect to database $conn = mysql_connect('localhost','root',''); mysql_select_db('tutorial'); //define the ID of the item you want to favorite $id = 15; //get the users id from the session id $user_id = $_SESSION['id']; //see if item has already been "liked" by the user $sql = "Select * FROM favorites WHERE user_id = '$user_id' AND item_id = '$item_id'"; $res = mysql_query($sql, $conn); $num_rows = mysql_num_rows($res); if ($num_rows == 0) { echo "<button href='#' class='like-button' onclick='favorite(".$id.")'>Like</button>"; } elseif ($num_rows == 1) { echo "<button href='#' class='unlike-button' onclick='favorite(".$id.")'>Unlike</button>"; } <script type="text/javascript" src='js/favorite_item.js'></script> $(document).on("click",".like-button",function(){var obj = $(this);if( obj.data('liked') ){obj.data('liked', false);obj.html('Like');}else{obj.data('liked', true);obj.html('Unlike');} }); $(document).on("click",".unlike-button",function(){ var obj = $(this);if( obj.data('unlike') ){obj.data('unlike', false);obj.html('Unlike');}else{obj.data('unlike', true);obj.html('Like');} }); //create 'favorite' function function favorite(str) { xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; } //enter the location of your AJAX/PHP file below var url= "ajax/favorite.php"; //adds the id onto your url so the you can "GET" it url=url+"?id="+str; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("GET",url ,true); xmlHttp.send(null); } function GetXmlHttpObject() { var xmlHttp=null; try { xmlHttp=new XMLHttpRequest(); } //catch the error catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } //get id of item selected $item_id = $_GET["id"]; //get the user id from the session $user_id = $_SESSION['id']; //connect to your MySQL database $conn = mysql_connect('localhost' ,'root' ,''); mysql_select_db('favorites'); //check to see if item is already liked $sql = "Select * FROM favorites WHERE user_id = '$user_id' AND item_id = '$item_id'"; $res = mysql_query($sql, $conn); $num_rows = mysql_num_rows($res); if($num_rows == 0) { $sql= "INSERT INTO favorites (item_id, store, user_id)values('$item_id','$item_store','$user_id')"; $save_favorite = mysql_query($sql, $conn); } elseif($num_rows == 1){ $sql = "DELETE FROM favorites WHERE user_id = '$user_id' AND item_id = '$item_id'"; $delete_favorite = mysql_query($sql, $conn); } The upperscript is not very much comprehensible though, because the only thing I done in PDO was: $db=new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'hyuiuik');foreach($db->query('SELECT * FROM TABBL') as $value) {echo $value['nn'];} Considering the upper borrowed example ,I have some speculations: I don't need Ajax for my first steps (at all) - because it's long and lots of unknowns and after all it's increasing complexity. Might it be not a bad idea to refuse to use mysql. I had had a two-week (or so) searching(examination/looking) before js tryings when searching (in English) how to do PDO. (I should say it's a great effort to fight for your little desirable piece of code over an inet). Reminder for myself: 1. Launch mysql: /etc/init.d/mysql start just for likes: 1a) Make a db, make users, grant rights OR 1b) After mysql launch with the help of PHP/PDO: grant mysql rights admin, make a db, make db users & grant them rights to db, make tables and INSERT data INTO tables (may be a single like-table). OR even 1c) Make a textfile - and forget this brain pain and append to it (like I do it in bash >>). 2. Make HTML 2a) <form><input type=button id="likebutton" onClick="databaseinsertevent()"> 2b) ......onClick=Database insert without any functions - just because it is very many text 3. Make JavaScript 3a) something+=something+1 >>like 3b) show like on page 4. Make PHP/PDO or PHP/txt-file 4a) read last line of txt txt 1 2 3 4b) a database (much more complex variant) $db=new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'hyuiuik'); foreach($db->query('SELECT like FROM TABBL') as $value) { echo $value['nn']; foreach($db->query('INSERT like TO TABBL') as $something <-- #comment//* Repeat the 1st output 5. Link all modules 5a) HTML + Javascript 5b) html/js + PHP 5c) PHP/PDO + mysql (or a textfile) 5d) run /etc/init.d/apache2 start *5d1 Link apache2+mysql+php(+pdo) (emerge -av php/pdo etc...,config..) if needed ------------------------------------------------- I am in my first steps of a difficult winding climb towards ⬘ Edited April 8, 2014 by xcislav Link to comment Share on other sites More sharing options...
thescientist Posted April 9, 2014 Share Posted April 9, 2014 I'm not sure what this is Link to comment Share on other sites More sharing options...
xcislav Posted June 13, 2014 Author Share Posted June 13, 2014 I'm writing for those who will search and find mine for their use and those who have motivation for their goal. But I'm not writing for those who won't help or will advice something HOW OTHERS SHOULD BE or flame or blame or offtop. <?php $db=new PDO('mysql:host=localhost;dbname=test;charset=utf8','root','hyuiuik');$db->exec("UPDATE LIK SET likes=likes+1");$rs=$db->query("SELECT likes FROM LIK");print_r($rs->fetchColumn());?> Works! Every F5. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now