Answers for "codeboxx php dislike like"

PHP
0

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>
Posted by: Guest on March-31-2021
0

codeboxx php dislike like

#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; }
Posted by: Guest on March-31-2021
0

codeboxx php dislike like

<?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);
Posted by: Guest on March-31-2021

Browse Popular Code Answers by Language