Skip to content

Instantly share code, notes, and snippets.

@escapeboy
Last active October 2, 2020 17:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save escapeboy/11324949 to your computer and use it in GitHub Desktop.
Save escapeboy/11324949 to your computer and use it in GitHub Desktop.
CodeIgniter Rating Library + Microdata (optional)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Rating Library
* Using jQuery Raty plugin to rate products
* @author Nikola Katsarov
* @website http://katsarov.biz
*/
class Rating {
private $rating_table = 'product_ratings';
public $seo = true;
public function __construct()
{
$this->CI =& get_instance();
if(!$this->CI->db->table_exists($this->rating_table)){
$this->install();
}
}
private function install()
{
$sql = "CREATE TABLE IF NOT EXISTS `".$this->rating_table."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`score` int(11) NOT NULL DEFAULT '1',
`date_registered` datetime NOT NULL,
`user_ip` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `product_id` (`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
";
$this->CI->db->query($sql);
}
public function get($product_id)
{
$rating->avg = $this->get_avarage($product_id);
$rating->votes = $this->get_total_votes($product_id);
if(!$rating->avg){
$rating->avg = 0;
}
return $rating;
}
public function generate_html($product_id, $product_name='', $product_photo='', $seo=true)
{
$this->seo = $seo;
if ($this->seo) {
$html= '<div id="product-rating" data-score="'.$this->get($product_id)->avg.'"></div>';
$html.= '<div itemscope itemtype="http://data-vocabulary.org/Review-aggregate">';
if($product_name){
$html.= '<meta itemprop="itemreviewed" content = "'.$product_name.'"> ';
}
if($product_photo){
$html.= '<meta itemprop="photo" content = "'.$product_photo.'"> ';
}
$html.='<span itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating">';
$html.='<meta itemprop="average" content = "'.$this->get($product_id)->avg.'">';
$html.='<meta itemprop="best" content = "5">';
$html.='<meta itemprop="votes" content = "'.$this->get($product_id)->votes.'">';
$html.='</span>';
$html.='</div>';
}else{
$html = '<div id="product-rating" data-score="'.$this->get($product_id)->avg.'"></div>';
}
return $html;
}
private function get_total_votes($product_id)
{
$this->CI->db->where('product_id', $product_id);
return $this->CI->db->count_all_results($this->rating_table);
}
private function get_avarage($product_id)
{
$this->CI->db->where('product_id', $product_id);
$this->CI->db->select_avg('score');
$q = $this->CI->db->get($this->rating_table);
return round($q->row()->score, 2);
}
public function add($data)
{
$this->CI->db->set('date_registered', 'NOW()', false);
if($this->CI->db->insert($this->rating_table, $data)){
return true;
}
}
public function update($data)
{
$this->CI->db->set('date_registered', 'NOW()', false);
if($this->CI->db->update($this->rating_table)){
return true;
}
}
public function delete($rating_id)
{
$this->CI->db->where('id', $rating_id);
if($this->CI->db->delete($this->rating_table)){
return true;
}
}
}
@tapfumamanhanga
Copy link

Hi can you please put code for the respective controllers, views and models please

@nsoni244
Copy link

Please share other code like view files, controller to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment