我是新来的,但我喜欢这个网站。我检查了其他类似的问题,但是没有找到想要的东西。
我是一名音乐家,并且一段时间以来我一直在做“一天之歌”的事情,每天我都会写一首小歌。我想将歌曲发布为<div>
内的<li>
。在div中,我只想要一个简单的mp3播放器和一个“喜欢”或“喜欢”按钮。用户可以投票,并且歌曲将根据投票数在<li>
上下移动。
我想通过数学保持这一简单性,即从<li>
数组中的不喜欢中减去不喜欢的东西,并按从高到低的顺序对其进行排序。
最好有一个简单的Cookie系统,以至少使某人坐下来不参加很多表决,但是我对此不太担心。
我一直在寻找有关此的简单PHP或Javascript教程。谁能指出我正确的方向?
谢谢!
最佳答案
您将需要一个中心位置来存储此歌曲信息(主要是选票),但是您也可能需要在其中存储其他歌曲信息(例如标题,音乐文件的路径等)。我建议一个简单的MySQL表如下
CREATE TABLE daily_song (
daily_song_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
vote INT NOT NULL DEFAULT 0,
title VARCHAR(255) NOT NULL DEFAULT "" COMMENT "Name of the song",
path VARCHAR(255) NOT NULL DEFAULT "" COMMENT "Path to the song on the server",
ctime DATETIME NOT NULL COMMENT "Datetime the song was added",
PRIMARY KEY(daily_song_id)
);
我使用以下HTML结构:
<ul id="daily-songs">
<li id="song-id-1">
<h3>Song 1</h3>
<div class="voting-controls">
<a href="#" class="vote-up">Up</a>
<div class="votes">8</div>
<a href="#" class="vote-down">Down</a>
</div>
<div class="player"></div>
</li>
<li id="song-id-2">
<h3>Song 2</h3>
<div class="player"></div>
<div class="voting-controls">
<a href="#" class="vote-up">Up</a>
<div class="votes">5</div>
<a href="#" class="vote-down">Down</a>
</div>
</li>
<li id="song-id-3">
<h3>Song 3</h3>
<div class="player"></div>
<div class="voting-controls">
<a href="#" class="vote-up">Up</a>
<div class="votes">4</div>
<a href="#" class="vote-down">Down</a>
</div>
</li>
</ul>
还有一点CSS
#daily-songs li { clear: both; list-style: none; }
#daily-songs h3 { margin: 0 10px 0 0; float: left; }
#daily-songs .voting-controls { height: 1em; }
#daily-songs .voting-controls * { float: left; }
#daily-songs .voting-controls .votes { padding: 0 10px; }
这是使用jQuery的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,
votes: song.vote.text() // temporary
},
function(data) {
if ( isNaN(data) ) { return; }
song.vote.text(data);
// Re-order the song list
$.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);
});
});
还有一些 stub PHP的
vote.php
:<?php
$songId = !empty($_POST['id']) ? (int)$_POST['id'] : 0;
$delta = !empty($_POST['delta']) ? (int)$_POST['delta'] : 0;
if ( !$songId || !$delta ) {
die("Invalid parameters");
}
// Make sure the voting value is within the valid rang
$delta = $delta > 0 ? 1 : -1;
// Check to see if user has already voted for this song,
// If they haven't voted yet, connect to the database
// If the database connection is succesful, update song entry
// UPDATE daily_song SET votes=votes+$delta WHERE daily_song_id=$songId
// If the UPDATE is successful, SELECT the new vote value
// SELECT vote FROM daily_song WHERE daily_song_id=$songId
// Output the new vote value
// But for now, just accept the current vote and apply the delta
echo $_POST['votes'] + $delta;
我将把PHP留给您填写。如果您有任何疑问,请告诉我。
关于php - 根据用户投票移动div,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1987811/