Skip to content

Commit

Permalink
update to 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
itchief committed Nov 9, 2019
1 parent 1d248ad commit c0ed89f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
68 changes: 2 additions & 66 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,12 @@
<title>PHP Star Rating</title>
<style>
body {
margin: 0;
margin: 10px 0 0 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
text-align: center;
margin-top: 10px;
}

.star-rating__container {
display: inline-block;
}

.star-rating__wrapper {
position: relative;
display: flex;
}

.star-rating {
display: inline-block;
position: relative;
user-select: none;
}

.star-rating__bg {
color: #e0e0e0;
display: flex;
}

.star-rating__live {
display: flex;
color: #ffb74d;
overflow: hidden;
position: absolute;
top: 0;
left: 0;
}

.star-rating_active:hover .star-rating__live {
overflow: auto;
width: 100% !important;
color: #e0e0e0;
}

.star-rating__item_active {
color: #fb8c00;
cursor: pointer;
transition: color 0.1s ease-in-out;
}

.star-rating__item {
width: 32px;
height: 32px;
flex: 0 0 32px;
}

.star-rating__avg {
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
text-align: center;
width: 2em;
}

.star-rating__votes {
display: flex;
align-items: center;
justify-content: center;
padding-left: 0.5em;
font-size: 0.875em;
}
</style>
<link rel="stylesheet" href="star_rating.css">
</head>

<body>
Expand Down
74 changes: 38 additions & 36 deletions process_star_rating.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
<?php

const
DB_HOST = 'localhost',
DB_NAME = 'mydb',
DB_CHARSET = 'utf8',
DB_USER = 'root',
DB_PASSWORD = '',
MAX_RATING = 5,
IS_CHECK_IP = false;
DB_HOST = 'localhost',
DB_NAME = 'mydb',
DB_CHARSET = 'utf8',
DB_USER = 'root',
DB_PASSWORD = '',
MAX_RATING = 5,
IS_CHECK_IP = false;

function log_write($message)
{
$log = date('d.m.Y H:i:s') . PHP_EOL . $message . PHP_EOL . '-------------------------' . PHP_EOL;
file_put_contents('error.log', $log, FILE_APPEND);
}

function getIp() {
function getIp()
{
$keys = [
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'REMOTE_ADDR'
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'REMOTE_ADDR'
];
foreach ($keys as $key) {
if (!empty($_SERVER[$key])) {
$ip = trim(end(explode(',', $_SERVER[$key])));
if (filter_var($ip, FILTER_VALIDATE_IP)) {
return $ip;
if (!empty($_SERVER[$key])) {
$ip = trim(end(explode(',', $_SERVER[$key])));
if (filter_var($ip, FILTER_VALIDATE_IP)) {
return $ip;
}
}
}
}
return false;
}


$output['result'] = 'error';

if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
exit(json_encode($output));
}
Expand All @@ -39,18 +43,16 @@ function getIp() {
exit(json_encode($output));
}

$output['result'] = 'error';

$count = 0;
$totalVotes = 1;
if (empty($_POST['id'])) {
log_write('Не передан id!');
log_write('Не передан id!');
exit(json_encode($output));
}
$ratingId = filter_var($_POST['id'], FILTER_SANITIZE_STRING);
if (strlen($ratingId) == 0) {
log_write('Параметр id имеет в качестве значения пустую строку!');
exit(json_encode($output));
exit(json_encode($output));
}

try {
Expand Down Expand Up @@ -81,7 +83,7 @@ function getIp() {
if (IS_CHECK_IP == true) {
$sql = 'SELECT count(*) FROM star_rating_ip WHERE rating_id = :rating_id AND rating_ip = :rating_ip';
$result = $conn->prepare($sql);
$data = ['rating_id' => $row[id], 'rating_ip' => getIp()];
$data = ['rating_id' => $row['id'], 'rating_ip' => getIp()];
$result->execute($data);
$countRows = $result->fetchColumn();
if ($countRows == 0) {
Expand All @@ -95,7 +97,7 @@ function getIp() {
log_write('Ошибка выборки данных: ' . $e->getMessage());
break;
}
$output['result'] = 'success';
$output['result'] = 'success';
break;

case 'set_rating':
Expand Down Expand Up @@ -126,7 +128,7 @@ function getIp() {
log_write('Ошибка выборки данных: ' . $e->getMessage());
break;
}

if ($count == 0) {
try {
$result = $conn->prepare('INSERT INTO star_rating (rating_id, rating_avg, total_votes) VALUES (:rating_id, :rating_avg, :total_votes)');
Expand All @@ -145,26 +147,26 @@ function getIp() {
} catch (PDOException $e) {
log_write('Ошибка добавления новой записи в таблицу star_rating_ip: ' . $e->getMessage());
break;
}
}
}
} catch (PDOException $e) {
log_write('Ошибка выборки данных: ' . $e->getMessage());
break;
}
}
}
} catch (PDOException $e) {
log_write('Ошибка добавления новой записи в базу: ' . $e->getMessage());
break;
}
} else {
$ratingAvg = ($ratingAvg * $totalVotes + $rating) / ($totalVotes + 1);
$totalVotes = $totalVotes + 1;
$totalVotes = $totalVotes + 1;
if (IS_CHECK_IP == true) {
$ip = getIp();
$sql = 'SELECT count(*) FROM star_rating_ip WHERE rating_id = :rating_id AND rating_ip = :rating_ip';
$result = $conn->prepare($sql);
$data = ['rating_id' => $id, 'rating_ip' => $ip];
$result->execute($data);
$result->execute($data);
$countRows = $result->fetchColumn();
if ($countRows > 0) {
break;
Expand All @@ -175,13 +177,13 @@ function getIp() {
} catch (PDOException $e) {
log_write('Ошибка добавления новой записи в таблицу star_rating_ip: ' . $e->getMessage());
break;
}
}
}
$sql = 'UPDATE star_rating SET rating_avg=:rating_avg, total_votes=:total_votes WHERE rating_id=:rating_id';
$data = [
'rating_id' => $ratingId,
'rating_avg' => $ratingAvg,
'total_votes' => $totalVotes
'rating_id' => $ratingId,
'rating_avg' => $ratingAvg,
'total_votes' => $totalVotes
];
try {
$conn->prepare($sql)->execute($data);
Expand All @@ -190,12 +192,12 @@ function getIp() {
break;
}
}

$output['result'] = 'success';

$output['data'] = [
'rating_avg' => $ratingAvg,
'total_votes' => $totalVotes
'rating_avg' => $ratingAvg,
'total_votes' => $totalVotes
];
break;
}
Expand Down
63 changes: 63 additions & 0 deletions star_rating.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
.star-rating__container {
display: inline-block;
}

.star-rating__wrapper {
position: relative;
display: flex;
}

.star-rating {
display: inline-block;
position: relative;
user-select: none;
}

.star-rating__bg {
color: #e0e0e0;
display: flex;
}

.star-rating__live {
display: flex;
color: #ffb74d;
overflow: hidden;
position: absolute;
top: 0;
left: 0;
}

.star-rating_active:hover .star-rating__live {
overflow: auto;
width: 100% !important;
color: #e0e0e0;
}

.star-rating__item_active {
color: #fb8c00;
cursor: pointer;
transition: color 0.1s ease-in-out;
}

.star-rating__item {
width: 32px;
height: 32px;
flex: 0 0 32px;
}

.star-rating__avg {
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
text-align: center;
width: 2em;
}

.star-rating__votes {
display: flex;
align-items: center;
justify-content: center;
padding-left: 0.5em;
font-size: 0.875em;
}

0 comments on commit c0ed89f

Please sign in to comment.