jwwceo Posted January 3, 2008 Share Posted January 3, 2008 I have a site where I am building a thumbs up/thumbs down voting system. I want users to only be allowed to vote once. So I am requiring that users be logged in to vote. I will then store their votes in the DB and retrieve this data when they return. If they have voted for an item, they will not be allowed to vote for it ever again. I am just wondering what the best way to approach this is. I will have their votes stored in the DB. How would I pull all of their votes out, (maybe in an array??) and then compare all the items with this list of "already voted on" items to see if they can vote, or to display their last votes. I guess I am uncertain which PHP functions to use...both to turn their votes into an array and also to see if a particular item ID is in that array or not. James Quote Link to comment https://forums.phpfreaks.com/topic/84335-voting-system-logic/ Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 Well, if you store their vote in the DB, pull it in an array and then compare it to your list of polls using in_array(needle, haystack) That's one way. Or use SQL to only pull things they HAVEN'T voted on. Don't know if you want to show the user what they have or haven't voted on. Quote Link to comment https://forums.phpfreaks.com/topic/84335-voting-system-logic/#findComment-429528 Share on other sites More sharing options...
jwwceo Posted January 3, 2008 Author Share Posted January 3, 2008 Which function would I use to create the array?? The fields in the Vote table will be vote_id, user_id, product_id, and vote ( either 1 or 0) for yes or no???? This is where I am a little confused...as I need basically a big list of all the items they have already voted on to then use the in-array function on. Quote Link to comment https://forums.phpfreaks.com/topic/84335-voting-system-logic/#findComment-429534 Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 <?php if(!user_has_voted($subject)) { $vote_list['not_voted'][] = $product_id; } else { $vote_list['voted'][] = $product_id; } ?> Leave it up to you to figure out the logic for the user_has_voted() function (it should either return TRUE or FALSE) - if VOTE has a value (0 or 1) then the user has voted, return TRUE else FALSE. This will create a multidimensional array like this: <?php $vote_list = array( 'voted' => array( '1', '5', '6' ), //end $voted['voted'] 'not_voted' => array( '2', '3', '4' ) //end $voted['not_voted'] ); //end $voted array ?> Quote Link to comment https://forums.phpfreaks.com/topic/84335-voting-system-logic/#findComment-429574 Share on other sites More sharing options...
redarrow Posted January 3, 2008 Share Posted January 3, 2008 little exaple..... written off top of the head......... database whatever table name votes item_id int(5) not null auto-increment primary_id users_id int(5) not null vote_score int(5) not null defualt(0) <?php session_start(); //database connection echo"<center>PLEASE VOTE ON THIS ITEM 1-10 <p><p/><form method=\"POST\" action=\" \"> <select name=\"vote_score\"> <option value=\"1\">1</option> <option value=\"2\">2</option> <option value=\"3\">3</option> <option value=\"4\">4</option> <option value=\"5\">5</option> <option value=\"6\">6</option> <option value=\"7\">7</option> <option value=\"8\">8</option> <option value=\"9\">9</option> <option value=\"10\">10</option> </select> <input type=\"submit\" name=\"submit\" value=\"VOTE\"> </form></center>"; if($_POST['submit']){ mysql_real_escape_string($_POST($_SESSION['userid'])); $user_id=mysql_real_escape_string($_POST['userid']); $vote_score=mysql_real_escape_string($_POST['vote_score']); $itemid=mysql_real_esape_string($_POST['itemid']); $q="SELECT * FROM votes WHERE userid='".$_SESSION['userid']." AND itemid=' ".$_GET['itemid']."' "; $qr=mysql_query($q)or die(mysql_error()); if(mysql_num_rows($qr)==1)){ echo"<center>SORRY YOU ALREADY VOTED</center>"; exit;// use a header redirect }elseif (INT($vote_score)){ $i="UPDATE votes set vote_score=vote_score+1 WHERE userid='".$_SESSION['userid']." and itemid='".$_GET['itemid]."' "; $ir=mysql_query($i)or die(mysql_error()); echo"vote added thank you"; }else}{}; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84335-voting-system-logic/#findComment-429584 Share on other sites More sharing options...
jwwceo Posted January 3, 2008 Author Share Posted January 3, 2008 hmm....I'm not sure I'm 100% clear....my apologies. The votes are just thumbs up (1) or thumbs down(0) and there is a table which stores every vote, with a user id, product_id and the vote. So I can use something like mysql_fetch_array(SELECT * FROM votes WHERE user_id ='123'); which will give me a big array of data of all that users votes.... but then how do I check each shirt against that...to determine whether to allow his to vote again...that is, how do I use a in_array on a multi-dimension array?? James Quote Link to comment https://forums.phpfreaks.com/topic/84335-voting-system-logic/#findComment-429594 Share on other sites More sharing options...
awpti Posted January 3, 2008 Share Posted January 3, 2008 You'd only be checking $voted['voted'] against your list of items the user has voted on. <?php if(in_array($vote_id, $vote_list['voted'])) { // user has voted } else { // user has not voted, accept the vote. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84335-voting-system-logic/#findComment-429606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.