views:

94

answers:

1

Hi folks, This is a continuation of this question: http://stackoverflow.com/questions/1987811/move-div-based-on-user-voting

I first posted the question as an unregistered user, and then later registered, so I couldn't edit or add to my original question...

Justin Johnson really helped me out with the code, but I'm still having a problem trying to figure out how to integrate the vote.php page.

Instead of redoing the question here, I'm hoping someone will see this, take a look at the above question (I added more in an "answer further down" and 1) help me resolve it and 2) bump up Justin's work-because I can't do it anymore because of the registration thing...

Thanks! Joel

+1  A: 

MySQL:

CREATE TABLE song (
    song_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    vote INT NOT NULL DEFAULT 0,
    PRIMARY KEY(song_id)
);

JavaScript:

$(function() {
    var listContainer = $("#daily-songs"),
        songs         = [];
    var songSort = function(a, b) {
        return +b.vote.text() - +a.vote.text();
    };

    var submitVote = function(song, delta) {
        $.post("vote.php", 
            {
                id:    song.node.attr("id").match(/\d+$/)[0],
                delta: delta,
            }, 
            function(data) {
                if ( data != 'success' ) { alert(data); }
            }
        );

        $.each(songs.sort(songSort), function() {
          listContainer.append(this.node);
        });
    };

    listContainer.find("li").each(function() {
        var $this = $(this); 
        var song  = {
            node: $this,
            vote: $this.find(".votes")
        };
        $this.find(".vote-up").click(function() {
            submitVote(song, 1);
        });
        $this.find(".vote-down").click(function() {
            submitVote(song, -1);
        });

        songs.push(song);
    });
});

PHP:

<?php

$song_id = !empty($_POST['id'])    ? (int)$_POST['id']    : 0;
$delta  = !empty($_POST['delta']) ? (int)$_POST['delta'] : 0;

if (!$song_id || !$delta || !is_int($song_id) || !is_int($delta)) {
  die("Invalid parameters");
}

// Make sure the voting value is within the valid range.
if ($delta != -1 && $delta != 1) {
  exit("Invalid delta");
}


// Check to see if user has already voted for this song
session_start();
if (isset($_SESSION['voted'])) {
  exit("You already voted!");
}

// If they haven't voted yet, connect to the database
// YOU NEED TO CHANGE THIS INFOMATION TO WHATEVER APPLYS TO YOU.
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
  $dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e) {
  exit('Connection failed: ' . $e->getMessage());
}


// If the database connection is succesful, update song entry
// UPDATE daily_song SET votes=votes+$delta WHERE daily_song_id=$songId
$sth = $dbh->prepare('UPDATE song SET votes = votes + :delta WHERE song_id = :song_id');
$sth->bindParam(':delta', $delta);
$sth->bindParam(':song_id', $song_id);
if (!$sth->execute()) {
  exit("Unable to update votes");
}

exit("success");
dmitrig01
Thanks Dmitri. Can you clarify for me:I have already created the database proposed in the first question...can we use that one in place of yours? Which values would I need to change? Thank you!
Joel
Basically, I just removed and renamed a bunch of columns. You can totally keep them if you want - if so, just change the $dbh->prepare statement to this:$sth = $dbh->prepare('UPDATE daily_song SET votes = votes + :delta WHERE daily_song_id = :song_id');and then it should work with no changes to the database
dmitrig01
Thank you Dmitri. I'm still getting some error, but I know it is my lack of experience here. I managed to get my accounts merged together, so I am going to close this question so we can keep everything on one page.
Joel
What's the error?
dmitrig01