codeboxx php dislike like
<?php
// (A) USER SESSION - FIXED USER ID TO 1 FOR THIS DEMO
session_start();
$_SESSION['user'] = 1;
// (B) DUMMY POSTS
$posts = [
"900" => "Foo Bar",
"901" => "Boo Bar",
"902" => "Goo Bar",
"903" => "Koo Bar"
];
$pid = [];
foreach ($posts as $id=>$txt) { $pid[] = $id; }
// (C) GET REACTIONS
require "2a-reactions.php";
$react = $REACT->get($pid);
$ureact = $REACT->getUser($pid, $_SESSION['user']);
// (D) OUTPUT HTML ?>
<!-- (D1) CSS + JS -->
<!-- https://cdnjs.com/libraries/font-awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"/>
<link rel="stylesheet" href="3b-posts.css"/>
<script src="3c-posts.js"></script>
<!-- (D2) POSTS LIST -->
<div id="demo"><?php
foreach ($posts as $id=>$txt) {
$likes = isset($react[$id][1]) ? $react[$id][1] : 0 ;
$dislikes = isset($react[$id][0]) ? $react[$id][0] : 0 ;
$reuser = isset($ureact[$id]) ? $ureact[$id] : "" ; ?>
<div class="prow" data-react="<?=$reuser?>" id="prow<?=$id?>">
<div class="ptxt"><?=$txt?></div>
<div class="plike" onclick="react(<?=$id?>, 1)">
<i class="fa fa-thumbs-up"></i>
<span class="countlike"><?=$likes?></span>
</div>
<div class="pdislike" onclick="react(<?=$id?>, 0)">
<i class="fa fa-thumbs-down"></i>
<span class="countdislike"><?=$dislikes?></span>
</div>
</div>
<?php } ?>
</div>
5
3
#demo {
max-width: 600px;
margin: 0 auto;
}
.prow {
display: flex;
background: #fafafa;
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
}
.ptxt { width: 100%; }
.plike, .pdislike {
width: 80px;
cursor: pointer;
color: #bbb;
}
.prow[data-react="0"] .pdislike { color: #f12727; }
.prow[data-react="1"] .plike { color: #0cb30c; }
html, body { font-family: arial, sans-serif; }
Thank you!
3
0
4.75
4
<?php
// (A) INIT
session_start();
$_SESSION['user'] = 1; // For this demo only, fixed to 1
require "2a-reactions.php";
$results = [];
// (B) COMMON FUNCTION - GET REACTIONS
function get () {
global $REACT;
global $results;
$results['react'] = $REACT->get([$_POST['id']]);
$results['user'] = $REACT->getUser([$_POST['id']], $_SESSION['user']);
}
// (C) HANDLE REQUEST
switch ($_POST['req']) {
// (C1) SAVE REACTION
case "save":
$results['status'] = $REACT->save($_POST['id'], $_SESSION['user'], $_POST['react']) ? 1 : 0 ;
if ($results['status']) { get(); }
else { $results['error'] = $REACT->error; }
break;
// (C2) DELETE REACTION
case "del":
$results['status'] = $REACT->del($_POST['id'], $_SESSION['user']) ? 1 : 0 ;
if ($results['status']) { get(); }
else { $results['error'] = $REACT->error; }
break;
}
// (D) RESPOND
/* $results = [
* "react" => REACTIONS FOR POST/VIDEO/PRODUCT
* "user" => USER REACTIONS
* "status" => 1 OR 0 (FOR SAVE + DELETE)
* "error" => ERROR MESSAGE, IF ANY
* ] */
echo json_encode($results);
Thank you!
4
0
Are there any code examples left?
New code examples in category PHP
-
PHP 2022-03-27 22:40:03 index:of curiosa 2019 mp4
-
PHP 2022-03-27 22:35:13 Dijkstra’s algorithm php
-
PHP 2022-03-27 22:35:12 get_declared_classes
-
PHP 2022-03-27 22:30:23 php move index of a value to first position in array
-
PHP 2022-03-27 22:25:01 wordpress get_date
-
PHP 2022-03-27 21:30:38 php shorten string with dots
-
PHP 2022-03-27 21:10:46 php header 500
-
PHP 2022-03-27 21:05:06 laravel schedule run
-
PHP 2022-03-27 20:25:12 Get page title, excerpt or content by id
-
PHP 2022-03-27 20:00:27 php convert date from dd/mm/yyyy to yyyy-mm-dd