user3796173
user3796173

Reputation: 19

Seperate like button for each while statement returned

I need to get a like button for each post, so that each time the while returns results each of those results has a like button unique to them, the issue I am having right now if the person can vote for one, but that's it, and all the other seperate posts are blocked off because they already voted, any advice? If you need referral to what I am trying to have the like system like I need it to be like this sites. http://fmylife.com/ their structure is the formum like I am looking for

while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    ?>
    <div class="wrapper">
        <div class="submissions">
            <div class="logo-logo"><h2>Quotr.</h2>
                <div class="checkboxes">
                    <?php echo htmlentities($row["formtype"]); ?>
                </div>
            </div>
            <div class="top-submit">
                &#8220;<?php echo htmlentities($row["actual_quote"]); ?>&#8221;
            </div>
            <div class="poster">
                - <?php
                    echo htmlentities($row["poster"]);

                    if(isset($row3["voted"]) && isset($row3["ip"]))
                    {
                        echo "You have already voted for this.";
                    }
                    else
                    {
                        ?>
                        <form action="" method="post">
                            <input type="submit" name="like" value="like" />
                            <input type="submit" name="dislike" value="dislike" />
                        </form>
                        <?php
                    }
                ?>
                <div class="like"></div>
                <div class="dislike"></div>
            </div>
            <!-- use select to get the items to stay on the page-->
        </div>
    </div>
    <?php
}
?>

And here's the code for the like button/dislike:

$likes = (empty($_POST['like'])) ? : $_POST['like'] ;
$dislikes = (empty($_POST['dislike'])) ? : $_POST['dislike'] ;
$ip = $_SERVER['REMOTE_ADDR'];

if(isset($_POST['like'])){
    $likes1 = $likes+1;
    $voted1 = $voted+1;
    $query2 = $db->prepare("INSERT INTO voters (voted, ip) VALUES ( :voted, :ip)");
    $query2->bindParam(':voted', $voted1, PDO::PARAM_STR);
    $query2->bindParam(':ip', $ip, PDO::PARAM_STR);  
    $query2->execute();
    header("Location: like.php?");
    $update1 = $db->prepare("INSERT INTO votes (likes) VALUES ( :likes)");
    $update1->bindParam(':likes', $likes1, PDO::PARAM_STR);
    $update1->execute();
}

if(isset($_POST['dislike'])){
    $dislikes1 = $dislikes+1;
    $voted1 = $voted+1;
    $query2 = $db->prepare("INSERT INTO voters (voted, ip) VALUES ( :voted, :ip)");
    $query2->bindParam(':voted', $voted1, PDO::PARAM_STR);
    $query2->bindParam(':ip', $ip, PDO::PARAM_STR);  
    $query2->execute();
    header("Location: like.php?");
    $update1 = $db->prepare("INSERT INTO votes (dislikes) VALUES ( :dislikes)");
    $update1->bindParam(':dislikes', $dislikes1, PDO::PARAM_STR);
    $update1->execute();
}
$stmt = $db->query("SELECT * FROM voters");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$row3 = $stmt->fetch();

Upvotes: 0

Views: 95

Answers (1)

myinitialsaretk
myinitialsaretk

Reputation: 38

I think you would benefit by taking a step back from the code and thinking about what you are trying to accomplish. When a user "likes" something, what are they liking? Presumably a quote?

So I would think that you need a data model that relates a users, quotes and likes.

There are a few different ways to do this, but I would suggest drawing it out on paper first. Using your database schema, the votes table is only taking an integer.

Just building off of what you already have, try to think about the problem like this:

Votes
-----
voter_id
quote_id
value

Then within your while loop you could create a form something like (this is psuedocode):

<form action="" method="post"> 
  <input type="hidden" name="quote_id" value="<?php echo $row['id']; ?>" />
  <input type="submit" name="Dislike" value="-1" />         
  <input type="submit" name="Like" value="1" />         
</form>

And you could insert a record into your vote table, keeping track of what quote they voted for as well as the id of the voter, after you insert that into the database. (sidenote: authenticating the user or keeping track of user session might be a better method than tracking by IP, but you can worry about that later)

Given that schema, you could now query to see if the voter had voted for that quote_id before. Or sum up how many likes or dislikes votes had been made by a user_id.

Just some food for thought. Hope that helps you down the right path.

Upvotes: 0

Related Questions