Skip to content

Instantly share code, notes, and snippets.

@theoomoregbee
Last active October 13, 2017 08:04
Show Gist options
  • Select an option

  • Save theoomoregbee/eda13c8f5b7a37898f33363cc6bf5ef0 to your computer and use it in GitHub Desktop.

Select an option

Save theoomoregbee/eda13c8f5b7a37898f33363cc6bf5ef0 to your computer and use it in GitHub Desktop.
This is our regular traditional way of updating counter
<?php
class Trad
{
public $id;
public $count;
}
/**
* Created by PhpStorm.
* User: theophy
* Date: 13/10/2017
* Time: 5:38 AM
*/
class Traditional1
{
private static $dbname = "playground";
private static $username = "root";
private static $password = "root";
private $db;
function __construct($id, $request)
{
$this->db = $this->connect();
if ($request == "add")
$this->like($id);
else
echo $this->getPost($id);
echo "Done";
}
private function connect(){...}
/**
* @return PDO
*/
private function getDb(){ .... return $this->db; }
private function like($id)
{
$post = $this->getPost($id);
$this->savePost($id, $post->count + 1);
}
/**
* this is used to get a post
* @param $id
* @return Trad
*/
private function getPost($id)
{
try {
$stmt = $this->getDb()->prepare("select * from traditional1 where id='$id'");
$stmt->setFetchMode(PDO::FETCH_CLASS, "Trad");
$stmt->execute();
return $stmt->fetch();
} catch (PDOException $ex) {
print $ex->getMessage();
}
}
/**
* this is used to perform a simple save of our post
* @param $id
* @param $total
*/
private function savePost($id, $total)
{
//form sql
$sql = "update traditional1 set count='$total' where id='$id'";
$stmt = $this->getDb()->prepare($sql);
$stmt->execute();
}
}
new Traditional1($_GET['id'], $_GET['request']);
const request = require('request');
for (let i = 0; i < 5; i++) {
request.get(`http://localhost/playground/shard/Traditional1.php?id=1&request=add`, (error) => {
if (error) return console.log(error);
console.log(`process ${i} is added 1 to count`);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment