[eluser]NateL[/eluser]
I'm confused too...still learning this OOP stuff :-S
Anyway, perhaps it will be easier if I post all of the code I've typed up from the book so far...
Controllers - welcome.php
Code:
class Welcome extends Controller {
function Welcome(){
parent::Controller();
}
function index(){
$data['title'] = "Welcome to Claudia's Kids";
$data['navlist'] = $this->MCats->getCategoriesNav();
$data['mainf'] = $this->MProducts->getMainFeature();
$skip = isset($data['mainf']['id']) ? $data['mainf']['id'] : '0';
$data['sidef'] = $this->MProducts->getRandomProducts(3,$skip);
$data['main'] = 'home';
$this->load->vars($data);
$this->load->view('template');
}
}
Model - mproducts.php
Code:
class MProducts extends Model{
function MProducts(){
parent::Model();
}
function getProduct($id){
$data = array();
$options = array('id' => $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 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;
}// end getMainFeature
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 !=', $skip);
$this->db->where('status', 'active');
$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;
}// end getRandomProducts
} // end class
Model - mcats.php
Code:
class MCats extends Model {
function MCats(){
parent::Model();
}
function getCategory($id){
$data = array();
$options = array('id' => $id);
$Q = $this->db->getwhere('categories', $options, 1);
if ($Q->num_rows() > 0){
$data = $Q->row_array();
}
$Q->free_result();
return $data;
}
function getAllCategories(){
$data = array();
$Q = $this->db->get('categories');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
function getCategoriesNav(){
$data = array();
$Q = $this->db->get('categories');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
}
}
Views - template.php
Code:
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $title; ?></title>
<link href="<?=base_url();?>css/default.css" rel="stylesheet" type="text/css" />
[removed]
//<![CDATA[
base_url='<?=base_url();?>';
//]]>
[removed]
</head>
<body>
<div id="wrapper">
<div id="header">
<?php $this->load->view('header');?>
</div>
<div id="nav">
<?php $this->load->view('navigation');?>
</div>
<div id="main">
<?php $this->load->view($main);?>
</div>
<div id="footer">
<?php $this->load->view('footer');?>
</div>
</div>
</body>
</html>
View - navigation.php
Code:
if (count($navlist)){
echo "<ul>";
foreach ($navlist as $id => $name){
echo "<li>";
echo anchor("welcome/cat/$id", $name);
echo "</li>";
}
echo "</ul>";
}
then there's the CSS, header and footer which only contain dummy code right now - nothing relevant to this issue.