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
Awgiedawgie 440215 points

                                    #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=&quot;0&quot;] .pdislike { color: #f12727; }
.prow[data-react=&quot;1&quot;] .plike { color: #0cb30c; }
html, body { font-family: arial, sans-serif; }

5 (3 Votes)
0
4.75
4
Phoenix Logan 186120 points

                                    &lt;?php
// (A) INIT
session_start();
$_SESSION['user'] = 1; // For this demo only, fixed to 1
require &quot;2a-reactions.php&quot;;
$results = [];

// (B) COMMON FUNCTION - GET REACTIONS 
function get () {
  global $REACT;
  global $results;
  $results['react'] = $REACT-&gt;get([$_POST['id']]);
  $results['user'] = $REACT-&gt;getUser([$_POST['id']], $_SESSION['user']);
}

// (C) HANDLE REQUEST
switch ($_POST['req']) {
  // (C1) SAVE REACTION
  case &quot;save&quot;:
    $results['status'] = $REACT-&gt;save($_POST['id'], $_SESSION['user'], $_POST['react']) ? 1 : 0 ;
    if ($results['status']) { get(); }
    else { $results['error'] = $REACT-&gt;error; }
    break;

  // (C2) DELETE REACTION
  case &quot;del&quot;:
    $results['status'] = $REACT-&gt;del($_POST['id'], $_SESSION['user']) ? 1 : 0 ;
    if ($results['status']) { get(); }
    else { $results['error'] = $REACT-&gt;error; }
    break;
}

// (D) RESPOND
/* $results = [
 *   &quot;react&quot; =&gt; REACTIONS FOR POST/VIDEO/PRODUCT
 *   &quot;user&quot; =&gt; USER REACTIONS
 *   &quot;status&quot; =&gt; 1 OR 0 (FOR SAVE + DELETE)
 *   &quot;error&quot; =&gt; ERROR MESSAGE, IF ANY
 * ] */
echo json_encode($results);

4.75 (4 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source