您的位置:首页 > 数据库 > Redis

redis 哨兵 票数_内容组织上的票数

2020-08-05 13:00 441 查看

redis 哨兵 票数

Content organization on the number of votes Quite often, we have the task to display any content (from the database) on the screen, many may argue that this is quite simple and trite. This is true, but when we have additional social components, for example on how to sort records by the number of comments or by rating – many novice webmasters can fall into confusion. Today we will consider the question of how to display the records by the number of gained votes. To do this, we will prepare a simple voting system.

内容组织在票数上经常,我们有任务在屏幕上显示任何内容(来自数据库),很多人可能会认为这很简单且陈腐。 的确如此,但是当我们还有其他社交功能时,例如关于如何按评论数或评分对记录进行排序的方法,许多新手网站管理员可能会感到困惑。 今天,我们将考虑如何通过获得的票数显示记录的问题。 为此,我们将准备一个简单的投票系统。

现场演示

First of all, I recommend that you check our demo to understand how it works. Now, we are ready to start

首先,我建议您检查我们的演示以了解其工作原理。 现在,我们准备开始

步骤1. SQL (Step 1. SQL)

Firstly, let’s prepare a new table in the database with some dummy records. If you are not familiar with the procedure of execution SQL requests, you can search in google about how to do it. But, basically, you just need to open phpMyAdmin, select your database, then click the ‘SQL’ tab, and put here the whole SQL query, and then – execute the query.

首先,让我们在数据库中准备一个包含一些伪记录的新表。 如果您不熟悉执行SQL请求的过程,则可以在Google中搜索如何执行。 但是,基本上,您只需要打开phpMyAdmin,选择数据库,然后单击“ SQL”选项卡,然后将整个SQL查询放在此处,然后–执行查询。

CREATE TABLE IF NOT EXISTS `s395_posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(255) default '',
`description` text NOT NULL,
`when` int(11) NOT NULL default '0',
`votecnt` int(11) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `s395_posts` (`title`, `description`, `when`, `votecnt`) VALUES
('First post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP(), 0),
('Second post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP() + 1, 0),
('Third post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP() + 2, 0),
('Fourth post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP() + 3, 0),
('Fifth post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP() + 4, 0);
CREATE TABLE IF NOT EXISTS `s395_posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(255) default '',
`description` text NOT NULL,
`when` int(11) NOT NULL default '0',
`votecnt` int(11) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `s395_posts` (`title`, `description`, `when`, `votecnt`) VALUES
('First post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP(), 0),
('Second post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP() + 1, 0),
('Third post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP() + 2, 0),
('Fourth post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP() + 3, 0),
('Fifth post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac neque tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum id convallis velit. Sed aliquet nibh vitae lorem tincidunt, quis consequat lectus blandit. Nullam augue mauris, placerat in ante non, fermentum ornare quam. Cras et fermentum purus. Ut elit mauris, blandit sit amet condimentum eu, ornare vel ligula.', UNIX_TIMESTAMP() + 4, 0);
[/code]

The table with records (s395_posts) contains the following fields:

包含记录的表(s395_posts)包含以下字段:

  • id – unique post id

    id –唯一的帖子ID
  • title – post title

    标题–帖子标题
  • description – post description (text)

    说明-帖子说明(文本)
  • when – post date

    什么时候–发布日期
  • votecnt – votes count

    投票数–票数

步骤2. PHP (Step 2. PHP)

In order to work with the database, we will use an existing class that we developed in the past (classes/CMySQL.php). I want to remind that the database credentials are found in this file, so don’t forget to put details of your database into this file:

为了使用数据库,我们将使用过去开发的现有类(classes / CMySQL.php)。 我想提醒一下,在此文件中找到了数据库凭据,所以不要忘记将数据库的详细信息放入此文件中:

类/CMySQL.php (classes/CMySQL.php)

// constructor
function CMySQL() {
$this->sDbName = '_YOUR_DB_NAME_';
$this->sDbUser = '_YOUR_DB_USERNAME_';
$this->sDbPass = '_YOUR_DB_USERPASS_';
// constructor
function CMySQL() {
$this->sDbName = '_YOUR_DB_NAME_';
$this->sDbUser = '_YOUR_DB_USERNAME_';
$this->sDbPass = '_YOUR_DB_USERPASS_';
[/code]

Now we will begin the creation of a new class where we arrange all the necessary functions for our demonstration:

现在,我们将开始创建一个新的类,在其中安排演示所需的所有必要功能:

  • getRecentPosts – this function returns the recent posts (with order by id)

    getRecentPosts –此函数返回最近的帖子(按ID排序)
  • getTopPosts – this function returns the recent posts (with order by count of votes)

    getTopPosts –此函数返回最近的帖子(按票数排序)
  • resultToHtml – this function converts an array of data into html output code

    resultToHtml –此函数将数据数组转换为html输出代码
  • addVote – this function accepts new votes

    addVote –此功能接受新的投票
  • getVisitorIP – this function returns a visitor IP address

    getVisitorIP –此函数返回访客IP地址

classes / CPosts.php (classes/CPosts.php)

<?php
require_once('CMySQL.php'); // include the database class
class CPosts {
// constructor
function CPosts() {
}
// get recent (last) posts
function getRecentPosts() {
$a = $GLOBALS['MySQL']->getAll("SELECT * FROM `s395_posts` ORDER BY `id` DESC");
return $this->resultToHtml($a);
}
// get top posts
function getTopPosts() {
$a = $GLOBALS['MySQL']->getAll("SELECT * FROM `s395_posts` ORDER BY `votecnt` DESC");
return $this->resultToHtml($a);
}
// turn array of results into html output
function resultToHtml($a) {
$posts = '';
foreach ($a as $i => $post) {
$posts .= <<<EOF
<article id="{$post['id']}">
<h2>{$post['title']}</h2>
<i>{$post['description']}</i>
<b>( {$post['votecnt']} votes )</b>
<button>vote up</button>
</article>
EOF;
}
return $posts;
}
// add vote
function addVote($i) {
if ($this->getVisitorIP()) {
$GLOBALS['MySQL']->res("UPDATE `s395_posts` SET `votecnt` = `votecnt` + 1 WHERE `id` = '{$i}'");
}
}
// get visitor IP address
function getVisitorIP() {
$ip = "0.0.0.0";
if( ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) && ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif( ( isset( $_SERVER['HTTP_CLIENT_IP'])) && (!empty($_SERVER['HTTP_CLIENT_IP'] ) ) ) {
$ip = explode(".",$_SERVER['HTTP_CLIENT_IP']);
$ip = $ip[3].".".$ip[2].".".$ip[1].".".$ip[0];
} elseif((!isset( $_SERVER['HTTP_X_FORWARDED_FOR'])) || (empty($_SERVER['HTTP_X_FORWARDED_FOR']))) {
if ((!isset( $_SERVER['HTTP_CLIENT_IP'])) && (empty($_SERVER['HTTP_CLIENT_IP']))) {
$ip = $_SERVER['REMOTE_ADDR'];
}
}
return $ip;
}
}
$GLOBALS['CPosts'] = new CPosts();
<?php
require_once('CMySQL.php'); // include the database class
class CPosts {
// constructor
function CPosts() {
}
// get recent (last) posts
function getRecentPosts() {
$a = $GLOBALS['MySQL']->getAll("SELECT * FROM `s395_posts` ORDER BY `id` DESC");
return $this->resultToHtml($a);
}
// get top posts
function getTopPosts() {
$a = $GLOBALS['MySQL']->getAll("SELECT * FROM `s395_posts` ORDER BY `votecnt` DESC");
return $this->resultToHtml($a);
}
// turn array of results into html output
function resultToHtml($a) {
$posts = '';
foreach ($a as $i => $post) {
$posts .= <<<EOF
<article id="{$post['id']}">
<h2>{$post['title']}</h2>
<i>{$post['description']}</i>
<b>( {$post['votecnt']} votes )</b>
<button>vote up</button>
</article>
EOF;
}
return $posts;
}
// add vote
function addVote($i) {
if ($this->getVisitorIP()) {
$GLOBALS['MySQL']->res("UPDATE `s395_posts` SET `votecnt` = `votecnt` + 1 WHERE `id` = '{$i}'");
}
}
// get visitor IP address
function getVisitorIP() {
$ip = "0.0.0.0";
if( ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) && ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif( ( isset( $_SERVER['HTTP_CLIENT_IP'])) && (!empty($_SERVER['HTTP_CLIENT_IP'] ) ) ) {
$ip = explode(".",$_SERVER['HTTP_CLIENT_IP']);
$ip = $ip[3].".".$ip[2].".".$ip[1].".".$ip[0];
} elseif((!isset( $_SERVER['HTTP_X_FORWARDED_FOR'])) || (empty($_SERVER['HTTP_X_FORWARDED_FOR']))) {
if ((!isset( $_SERVER['HTTP_CLIENT_IP'])) && (empty($_SERVER['HTTP_CLIENT_IP']))) {
$ip = $_SERVER['REMOTE_ADDR'];
}
}
return $ip;
}
}
$GLOBALS['CPosts'] = new CPosts();
[/code]

Now, let’s create a basic index.php file which we’ll run

现在,让我们创建一个基本的index.php文件,它将运行

index.php (index.php)

<?php
require_once('classes/CPosts.php'); // include the main class
// accept a vote
if ($_POST && $_POST['action'] == 'vote' && (int)$_POST['id']) {
$GLOBALS['CPosts']->addVote((int)$_POST['id']);
echo 1; exit;
}
$recent_posts = $GLOBALS['CPosts']->getRecentPosts();
$top_posts = $GLOBALS['CPosts']->getTopPosts();
?>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>Content organization on the number of votes | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
</head>
<body>
<header>
<h2>Back to '<a href="https://www.script-tutorials.com/xxxxxxxxxxxxx/">Content organization on the number of votes</a>' tutorial</h2>
</header>
<div class="container">
<div class="section">
<h1>Top posts:</h1>
<?= $top_posts ?>
</div>
<div class="section">
<h1>Recent posts:</h1>
<?= $recent_posts ?>
</div>
</div>
<script>
$(window).load(function() {
$('article button').click(function(e) {
var id = $(this).parent().attr('id');
if (id) {
$.post('index.php', { action: 'vote', id: id }, function(data) {
if (data) {
location.reload();
}
});
}
});
});
</script>
</body>
</html>
<?php
require_once('classes/CPosts.php'); // include the main class
// accept a vote
if ($_POST && $_POST['action'] == 'vote' && (int)$_POST['id']) {
$GLOBALS['CPosts']->addVote((int)$_POST['id']);
echo 1; exit;
}
$recent_posts = $GLOBALS['CPosts']->getRecentPosts();
$top_posts = $GLOBALS['CPosts']->getTopPosts();
?>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>Content organization on the number of votes | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
</head>
<body>
<header>
<h2>Back to '<a href="https://www.script-tutorials.com/xxxxxxxxxxxxx/">Content organization on the number of votes</a>' tutorial</h2>
</header>
<div class="container">
<div class="section">
<h1>Top posts:</h1>
<?= $top_posts ?>
</div>
<div class="section">
<h1>Recent posts:</h1>
<?= $recent_posts ?>
</div>
</div>
<script>
$(window).load(function() {
$('article button').click(function(e) {
var id = $(this).parent().attr('id');
if (id) {
$.post('index.php', { action: 'vote', id: id }, function(data) {
if (data) {
location.reload();
}
});
}
});
});
</script>
</body>
</html>
[/code]

This is quite basic file which generates our page, it also accepts the POST action (upvote) that is sent by clicking on buttons that are located inside of each post.

这是生成我们页面的非常基本的文件,它还接受通过单击每个帖子内部的按钮发送的POST操作(upvote)。

步骤3. CSS (Step 3. CSS)

After the creation of the main functionality – we can move on to the design styles of all the used elements, that’s what we’ve got:

创建主要功能后,我们可以继续使用所有已使用元素的设计样式,这就是我们得到的:

css / main.css (css/main.css)

*{margin:0;padding:0}
body {
background-repeat:no-repeat;
background-color:#bababa;
background-image: -webkit-radial-gradient(600px 200px, circle, #eee, #bababa 40%);
background-image: -moz-radial-gradient(600px 200px, circle, #eee, #bababa 40%);
background-image: -o-radial-gradient(600px 200px, circle, #eee, #bababa 40%);
background-image: radial-gradient(600px 200px, circle, #eee, #bababa 40%);
color:#fff;
font:14px/1.3 Arial,sans-serif;
min-height:600px;
}
.container {
border:1px #111 solid;
color:#000;
margin:20px auto;
overflow: hidden;
padding:10px;
position:relative;
width:90%;
border-radius:2px;
-moz-border-radius:2px;
-webkit-border-radius:2px;
}
.section {
float: left;
margin: 0 1%;
width: 48%;
}
article {
margin:1em 0;
}
button {
background: #e3e3e3;
border: 1px solid #bbb;
border-radius: 3px;
-webkit-box-shadow: inset 0 0 1px 1px #f6f6f6;
box-shadow: inset 0 0 1px 1px #f6f6f6;
color: #333;
display: block;
font-weight: bold;
padding: 8px 0 9px;
text-align: center;
text-shadow: 0 1px 0 #fff;
width: 150px;
}
button:hover {
background: #d9d9d9;
-webkit-box-shadow: inset 0 0 1px 1px #eaeaea;
box-shadow: inset 0 0 1px 1px #eaeaea;
color: #222;
cursor: pointer;
}
button:active {
background: #d0d0d0;
-webkit-box-shadow: inset 0 0 1px 1px #e3e3e3;
box-shadow: inset 0 0 1px 1px #e3e3e3;
color: #000;
}
*{margin:0;padding:0}
body {
background-repeat:no-repeat;
background-color:#bababa;
background-image: -webkit-radial-gradient(600px 200px, circle, #eee, #bababa 40%);
background-image: -moz-radial-gradient(600px 200px, circle, #eee, #bababa 40%);
background-image: -o-radial-gradient(600px 200px, circle, #eee, #bababa 40%);
background-image: radial-gradient(600px 200px, circle, #eee, #bababa 40%);
color:#fff;
font:14px/1.3 Arial,sans-serif;
min-height:600px;
}
.container {
border:1px #111 solid;
color:#000;
margin:20px auto;
overflow: hidden;
padding:10px;
position:relative;
width:90%;
border-radius:2px;
-moz-border-radius:2px;
-webkit-border-radius:2px;
}
.section {
float: left;
margin: 0 1%;
width: 48%;
}
article {
margin:1em 0;
}
button {
background: #e3e3e3;
border: 1px solid #bbb;
border-radius: 3px;
-webkit-box-shadow: inset 0 0 1px 1px #f6f6f6;
box-shadow: inset 0 0 1px 1px #f6f6f6;
color: #333;
display: block;
font-weight: bold;
padding: 8px 0 9px;
text-align: center;
text-shadow: 0 1px 0 #fff;
width: 150px;
}
button:hover {
background: #d9d9d9;
-webkit-box-shadow: inset 0 0 1px 1px #eaeaea;
box-shadow: inset 0 0 1px 1px #eaeaea;
color: #222;
cursor: pointer;
}
button:active {
background: #d0d0d0;
-webkit-box-shadow: inset 0 0 1px 1px #e3e3e3;
box-shadow: inset 0 0 1px 1px #e3e3e3;
color: #000;
}
[/code]
现场演示

[sociallocker]

[社交储物柜]

存档下载

[/sociallocker]

[/ sociallocker]

结论 (Conclusion)

That’s it for today. Our rating system with the output of records which are sorted by amount of votes is ready. Welcome back to our new articles.

今天就这样。 我们的评分系统已准备就绪,其中包含按票数排序的记录输出。 欢迎回到我们的新文章。

翻译自: https://www.script-tutorials.com/content-organization-on-the-number-of-votes/

redis 哨兵 票数

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: