• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Errors

#5
[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:
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
&lt;link href="&lt;?=base_url();?&gt;css/default.css" rel="stylesheet" type="text/css" /&gt;
[removed]
//&lt;![CDATA[
base_url='&lt;?=base_url();?&gt;';
//]]>
[removed]
&lt;/head&gt;

&lt;body&gt;
    <div id="wrapper">
        <div id="header">
            &lt;?php $this->load->view('header');?&gt;
        </div>
        
        <div id="nav">
            &lt;?php $this->load->view('navigation');?&gt;
        </div>
        
        
        
        
        
        <div id="main">
            &lt;?php $this->load->view($main);?&gt;
        </div>
    
            <div id="footer">
                &lt;?php $this->load->view('footer');?&gt;
            </div>    
    
    
    </div>
&lt;/body&gt;
&lt;/html&gt;

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.


Messages In This Thread
Errors - by El Forum - 09-19-2008, 08:41 AM
Errors - by El Forum - 09-19-2008, 08:49 AM
Errors - by El Forum - 09-19-2008, 08:52 AM
Errors - by El Forum - 09-19-2008, 09:47 AM
Errors - by El Forum - 09-19-2008, 10:11 AM
Errors - by El Forum - 09-19-2008, 10:12 AM
Errors - by El Forum - 09-19-2008, 01:15 PM
Errors - by El Forum - 09-19-2008, 01:48 PM
Errors - by El Forum - 09-19-2008, 03:55 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.