我可以获得有关此 php/javascript 投票代码的一些帮助吗?

发布于 2024-08-16 19:29:27 字数 362 浏览 6 评论 0 原文

这是这个问题的延续: 根据用户投票移动 div

我首先以未注册用户的身份发布了这个问题,然后后来注册了,所以我无法编辑或添加到我原来的问题...

Justin Johnson 确实帮助我解决了代码问题,但我在尝试弄清楚如何集成 vote.php 页面时仍然遇到问题。

我不想在这里重做这个问题,而是希望有人会看到这个,看看上面的问题(我在“进一步回答”中添加了更多内容,1)帮助我解决它,2)提高贾斯汀的工作 - 因为由于注册的事情我不能再这样做了...

谢谢! 乔尔

This is a continuation of this question:
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-08-23 19:29:27

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");

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文