Home >Backend Development >PHP Tutorial >Online voting system with diverse voting methods implemented in PHP
Online voting system with diversified voting methods implemented in PHP
With the advent of the Internet era, people's demand for participation and expression of opinions has become stronger and stronger. Online voting systems have become a convenient and fast way to collect group opinions and achieve democratic decision-making.
In this article, we will introduce an online voting system based on diversified voting methods implemented in PHP language and provide relevant code examples.
CREATE TABLE vote
(
id
int(11) NOT NULL AUTO_INCREMENT,
title
varchar(255) NOT NULL,
start_time
datetime NOT NULL,
end_time
datetime NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE option
(
id
int(11) NOT NULL AUTO_INCREMENT,
vote_id
int(11) NOT NULL,
content
text NOT NULL,
PRIMARY KEY (id
),
KEY vote_id
(vote_id
),
CONSTRAINT fk_vote_id
FOREIGN KEY (vote_id
) REFERENCES vote
( id
) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
// Voting theme display
echo "c1a436a314ed609750bd7c7d319db4da" . $vote['title'] . "2e9b454fa8428549ca2e64dfac4625cd";
// Option selection
foreach ($options as $option) {
echo "<input type='".$vote['type']."' name='option' value='".$option['id']."'>".$option['content']."</input><br/>";
}
// Submit button
echo "7eb4686203a0017423bce47ab5458009";
// Submit vote
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$selectedOption = $_POST['option']; // 根据选项类型进行不同的处理 if ($vote['type'] == 'radio') { // 单选投票 $voteCount = 1; } elseif ($vote['type'] == 'checkbox') { // 多选投票 $voteCount = count($selectedOption); } elseif ($vote['type'] == 'rating') { // 打分投票 $totalRating = $_POST['total_rating']; $voteCount = 1; } // 更新选项表中的投票统计 foreach ($selectedOption as $optionId) { // 执行更新操作,计数+1 // UPDATE option SET count = count + 1 WHERE id = $optionId }
}
// Query voting results
$options = $db->query("SELECT * FROM option WHERE vote_id = ".$vote['id'])->fetchAll();
foreach ( $options as $option) {
$votePercentage = ($option['count'] / $voteCount) * 100; echo $option['content'] . ": " . $votePercentage . "%<br/>";
}
Through the above code examples, we can implement an online voting system with diverse voting methods. Users can make choices based on voting topics and submit voting results. Administrators can create voting topics and view voting results in real time.
To sum up, the online voting system with diversified voting methods implemented in PHP can effectively meet the needs of groups to express their opinions and achieve democratic decision-making. Developers can customize the system according to actual scenarios and provide more voting methods and functions. I believe that the use of online voting systems will bring more convenience and participation to our lives.
The above is the detailed content of Online voting system with diverse voting methods implemented in PHP. For more information, please follow other related articles on the PHP Chinese website!