PHP PROJECT

README.md

Comments php class with replies and like buttons

It can display forms and buttons using the Bootstrap framework to let the user enter comments, replies to comments and click to like or dislike a comment.

Files


NameTypeSizeLast Modified
config.php phpfile icon 617 08 December 2015 10:24:30
badwordslist.txt textfile icon 2213 08 December 2015 10:24:30
comment.php phpfile icon 3693 08 December 2015 10:24:30
like-dislike.php phpfile icon 1751 08 December 2015 10:24:30
comment-template.php phpfile icon 1705 08 December 2015 10:24:30
comment.sql sqlfile icon 468 01 February 2017 04:13:02
bootstrapcomment.php phpfile icon 7061 08 December 2015 10:24:30

Source

  1. <?
  2. /**
  3.  * PHP Comment Class
  4.  * @author vishv23@yahoo.com - http://v23.in
  5.  * @version 1.0.0
  6.  * @date November 17, 2015
  7. */
  8. class bootstrapcomment{
  9. var $link;
  10. var $debug=true;
  11. var $table;
  12. var $fieldRequire;
  13. var $primeryKey;
  14. var $perPageDisplay;
  15. var $badword;
  16. var $iid;
  17. var $rr;
  18. var $url;
  19. var $arr;
  20.     public function __construct($host$user$pass$db
  21.     {
  22.         $this->link mysqli_connect($host$user$pass$db);
  23.             if (!$this->link && $debug) {
  24.             echo "Error: Unable to connect to MySQL." PHP_EOL;
  25.             echo "Debugging errno: " mysqli_connect_errno() . PHP_EOL;
  26.             echo "Debugging error: " mysqli_connect_error() . PHP_EOL;
  27.             exit;
  28.             }    
  29.         //init    
  30.         $this->arr=array();
  31.         $this->url=$_SERVER['REQUEST_URI'];
  32.         $this->table "comment";
  33.         $this->fieldRequire = array("name"=>"","comment"=>"");
  34.         $this->primeryKey "id";
  35.         $this->perPageDisplay 12;
  36.         $this->badword file("badwordslist.txt");
  37.     }
  38.   
  39.     public function __destruct()
  40.     {
  41.     mysqli_close($this->link);
  42.     }
  43.     
  44.       public function close()
  45.     {
  46.     mysqli_close($this->link);
  47.     }
  48.     
  49.     public function hit($durl)
  50.     {
  51.     
  52.     $this->link->mysqli_query("UPDATE ".$this->table."  SET hit = hit + 1 WHERE `url`='".$durl."' LIMIT 1");
  53.     }
  54.     
  55.     public function BadWordFilter($text$replace$bads)
  56.     {    
  57.     $text preg_replace('/\s\s+/'' '$text);
  58.     $text preg_replace(array('/[^a-zA-Z0-9 -]/''/[ -]+/''/^-|-$/'), ' '$text);
  59.         for($i=0;$i<sizeof($bads);$i++) {            
  60.         $bads[$i]="/".trim($bads[$i])."/i";
  61.         }
  62.     $text preg_replace($bads,'***'$text);
  63.     return $text;
  64.     }
  65.     
  66.     public function save($comment="",$name="",$uri=''$tm="",$ra="",$reply=false)
  67.     {
  68.     $na=array();
  69.     $this->url=$uri;
  70.         $commentprev strlen($comment);
  71.         if($this->badword)
  72.         $comment $this->BadWordFilter($comment,1,$this->badword); 
  73.         $commentnext strlen($comment);
  74.         
  75.             $na['name']=$this->link->real_escape_string($name);
  76.             $na['comment']=$this->link->real_escape_string($comment);
  77.             $na['url']=$uri;
  78.             $na['tm']=$tm;
  79.             $na['ip']=$ra;
  80.             $na['status']=1;
  81.             if($reply)
  82.             $na['pid']=$reply;
  83.                 foreach ($na as $key=>$value)
  84.                 {
  85.                 isset($vals) ? $vals .= ',' $vals '';
  86.                 isset($cols) ? $cols .= ',' $cols '';
  87.                 $vals .= '\''.$value.'\'';
  88.                 $cols .= '`'.$key.'`';
  89.                 }
  90.         //--------- if time limit------------
  91.         /*
  92.             $ar = $this->link->query("SELECT * from ".$this->table." WHERE `tm` > '".$tm."'-1000 AND `tm` < '".$tm."' AND  `comment` = '".$comment."' AND  `url` = '".$this->url."' LIMIT 1");
  93.             */
  94.         
  95.         $ar $this->link->query("SELECT * from ".$this->table." WHERE  `comment` = '".$comment."' AND  `url` = '".$this->url."' LIMIT 1");
  96.         
  97.         if( $ar->num_rows && $commentprev == $commentnext){
  98.         $this->link->query('INSERT INTO '.$this->table.' ('.$cols.') VALUES ('.$vals.')');
  99.         $this->iid mysqli_insert_id($this->link);
  100.         
  101.         }
  102.         else{
  103.         
  104.         if ($this->debug){ 
  105.         echo '<div class="alert alert-success"><strong>Error!</strong>All ready Exist.</div>';
  106.         }
  107.         }
  108.     }
  109.     
  110.     public function like($id)
  111.     {
  112.     $this->link->query("UPDATE ".$this->table."  SET `like` = `like` + 1 WHERE `id`=".$id." LIMIT 1");
  113.     }
  114.     
  115.     public function dislike($id)
  116.     {
  117.     $this->link->query("UPDATE ".$this->table."  SET `dislike` = `dislike` + 1 WHERE `id`=".$id." LIMIT 1");
  118.     }
  119.     
  120.     
  121.     public function totalRows($uri='',$condition='')
  122.     {
  123.     $sql "SELECT * FROM ".$this->table." WHERE `url` = '".$uri."' ".$condition;
  124.     $query $this->link->query($sql);
  125.         if ($query->num_rows 0)
  126.         return $query->num_rows;
  127.         else
  128.         return 0;
  129.     }
  130.     public function findDisLike($id)
  131.     {
  132.     $sql "SELECT * FROM ".$this->table." WHERE  `id` = $id LIMIT 1";
  133.     $query $this->link->query($sql);
  134.         if ($query->num_rows 0)
  135.         return mysqli_fetch_object($query);
  136.         else
  137.         return 0;
  138.     }
  139.   
  140.     function fetchCategoryTree($parent 0$uri=''$level=0,$limit='') {
  141.         
  142.         
  143.         
  144.         
  145.     $sql "SELECT * FROM ".$this->table." WHERE `url` = '".$uri."' AND `pid` = $parent ORDER BY id DESC $limit";
  146.     
  147.     $query $this->link->query($sql);
  148.         if ($query->num_rows 0) {
  149.         
  150.             while ($row mysqli_fetch_object($query)) {
  151.             
  152.             $this->arr[] = '<ul><div class="media">';
  153.             $this->arr[] =  '<div class="media-body"><div class="panel panel-info"><div class="panel-heading"><h4 class="media-heading text-primary">'$row->name.'<small class="text-muted">   commented '$this->timeAgo($row->date).'</small></h4></div><div class="panel-body">'$row->comment .'<p class="pull-right"><span class="glyphicon glyphicon-pencil rreply" aria-hidden="true" aria-value="'$row->id .'" id="REPLY'$row->id .'"> Reply </span><span class="glyphicon glyphicon-thumbs-up" aria-hidden="true" aria-value="'$row->id .'" id="UP'$row->id .'"> '$row->like .' </span><span class="glyphicon glyphicon-thumbs-down" aria-hidden="true" aria-value="'$row->id .'" id="DOWN'$row->id .'"> '$row->dislike .' </span></p></div></div>';
  154.             $this->arr $this->fetchCategoryTree($row->id$uri,$level+1,"LIMIT 1");
  155.             $this->arr[] = '</div></div></ul>';
  156.             }
  157.         
  158.         }
  159.     return $this->arr;
  160.     }
  161.     public function findChild($uri,$cid)
  162.     {
  163.     $ar $this->link->query("SELECT * FROM ".$this->table." WHERE  `id` = $cid AND  `url` = '".$uri."'  LIMIT 1");
  164.         if( $ar->num_rows 0){
  165.          while ($row $ar->fetch_assoc()) {
  166.           if($row['pid'] != 0){
  167.          $this->findChild($uri,$row['pid']);
  168.          }
  169.         
  170.         }
  171. }
  172.     }
  173.     
  174.     
  175.    
  176.     
  177.     public function render_temp($file$data = array()) {
  178.         if (file_exists($file)) {
  179.             extract($data);
  180.             ob_start();
  181.             require($file);
  182.             $out ob_get_contents();
  183.             ob_end_clean();
  184.             return $out;
  185.         } else {
  186.         return false;
  187.         }
  188.     }
  189.     
  190.     public function timeAgo($time_ago){
  191.         $cur_time time();
  192.         $time_elapsed $cur_time strtotime($time_ago);
  193.         $seconds $time_elapsed ;
  194.         $minutes round($time_elapsed 60 );
  195.         $hours round($time_elapsed 3600);
  196.         $days round($time_elapsed 86400 );
  197.         $weeks round($time_elapsed 604800);
  198.         $months round($time_elapsed 2600640 );
  199.         $years round($time_elapsed 31207680 );
  200.             if($seconds <= 60){
  201.              return  "$seconds seconds ago";
  202.             }
  203.             else if($minutes <=60){
  204.                  if($minutes==1){
  205.                  return  "one minute ago";
  206.                  }
  207.                  else{
  208.                  return  "$minutes minutes ago";
  209.                  }
  210.             }
  211.             else if($hours <=24){
  212.                  if($hours==1){
  213.                  return  "an hour ago";
  214.                  }else{
  215.                  return  "$hours hours ago";
  216.                  }
  217.             }
  218.             else if($days <= 7){
  219.                  if($days==1){
  220.                  return  "yesterday";
  221.                  }else{
  222.                  return  "$days days ago";
  223.                  }
  224.             }
  225.             else if($weeks <= 4.3){
  226.                  if($weeks==1){
  227.                  return  "a week ago";
  228.                  }else{
  229.                  return  "$weeks weeks ago";
  230.                  }
  231.             }
  232.             else if($months <=12){
  233.                  if($months==1){
  234.                  return  "a month ago";
  235.                  }else{
  236.                  return  "$months months ago";
  237.                  }
  238.             }
  239.             else{
  240.                  if($years==1){
  241.                  return  "one year ago";
  242.                  }else{
  243.                  return  "$years years ago";
  244.                  }
  245.             }
  246.     }
  247.     
  248. }
  249. ?>

Demo

You can find a demo available here: bootstrap comment

Requirment

  • mySql
  • PHP >= 5.4
  • Apache Server

Contribute

  • Vishv Sahdev

Example

>>

Download