[eluser]Zac G.[/eluser]
Sure!
I am working through the Wrox CI book, and this is my model:
The file name is mproducts.php
Code:
<?php
class MProducts extends Model
{
function MProducts()
{
parent::Model();
}
function getProduct($id)
{
$data = array();
$options = array(
'id' => id_clean($id)
);
$Q = $this->db->getwhere(
'products',$options,1
);
if ($Q->num_rows() > 0)
{
$data = $Q->row_array();
}
$Q->free_result();
return $data;
}
function getAllProducts()
{
$data = array();
$Q = $this->db->get('products');
if ($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
function getProductsByCategory($category_id)
{
$data = array();
$this->db->select('id,name,shortdesc,thumbnail');
$this->db->where('category_id', id_clean($catid));
$this->db->where('status', 'active');
$this->db->orderby('name','asc');
$Q = $this->db->get('products');
if ($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
function getMainFeature()
{
$data = array();
$this->db->select("id,name,shortdesc,image");
$this->db->where('featured','true');
$this->db->where('status', 'active');
$this->db->orderby("rand()");
$this->db->limit(1);
$Q = $this->db->get('products');
if ($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data = array(
"id" => $row['id'],
"name" => $row['name'],
"shortdesc" => $row['shortdesc'],
"image" => $row['image']
);
}
}
$Q->free_result();
return $data;
}
function getRandomProducts($limit, $skip)
{
$data = array();
$temp = array();
if ($limit == 0)
{
$limit=3;
}
$this->db->select("id,name,thumbnail,category_id");
$this->db->where('id !=', id_clean($skip));
$this->db->orderby("category_id","asc");
$this->db->limit(100);
$Q = $this->db->get('products');
if ($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row){
$temp[$row['category_id']] = array(
"id" => $row['id'],
"name" => $row['name'],
"thumbnail" => $row['thumbnail']
);
}
}
shuffle($temp);
if (count($temp))
{
for ($i=1;$i<=$limit; $i++)
{
$data[] = array_shift($temp);
}
}
$Q->free_result();
return $data;
}
}
?>