Welcome Guest, Not a member yet? Register   Sign In
NEW QUESTION -> LAST POST..Categories/URI/Maybe Model/Controller help? Code inside.
#1

[eluser]ColinHoernig[/eluser]
Basically, I'm just starting to use the whole "MVC" framework style and I'm having some issues grasping some things..

Here is my products controller:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Products extends Controller {

    function Products()
    {
        parent::Controller();
        $this->load->model(array('categories_model', 'products_model'), '', TRUE);
        $this->load->scaffolding('categories');
        $this->load->helper('url');
    }

    function Index()
    {
        $data['categories_query'] = $this->categories_model->getAllCategories();
        $data['products_query'] = $this->products_model->getAllProducts();
        
        $data['page_title'] = "Products";
        $data['heading'] = "Products";
        $this->load->view('products', $data);
    }
    
    function Category() {
        $data['categories_query'] = $this->categories_model->getCategoryInfo($this->uri->segment(3));
        $data['products_query'] = $this->products_model->getProductsByCat($this->uri->segment(3));
        $data['page_title'] = "Products";
        $data['heading'] = "Products";
        $this->load->view('products', $data);
    }

}

/* End of file: filename.php */
/* Location: ./system/application/controllers/filename.php */

parts of Products model:
Code:
//get all products in table
    function getAllProducts() {
        $this->db->orderby('product_name', 'asc');
        
        return $this->db->get('products');
    }
    
    //get all the product info from table
    function getProductInfo($product_id, $fields = '*') {
        $this->db->select($fields);
        $this->db->where('product_id', $product_id);
        
        return $this->db->get('products')->row();
    }
    
    function getProductsByCat($category_id) {
        $this->db->where('product_categoryID', $category_id);
        
        return $this->db->get('products');
    }

Parts of categories_model:

Code:
//get all categorys in table
    function getAllCategories() {
        $this->db->orderby('category_name', 'asc');
        
        return $this->db->get('categories');
    }
    
    //get all the category info from table
    function getCategoryInfo($id, $fields = '*') {
        $this->db->select($fields);
        $this->db->where('category_id', $id);
        
        return $this->db->get('categories')->row();
    }

What I'm trying to do is make it so that if they have already selected a category from the products VIEW, then it will change the query and only show them the products in that category.

My view looks like this:

Code:
<div id="categories">
<ul class="categories">
&lt;?php foreach($categories_query->result() as $categoryRow): ?&gt;
<li>&lt;?php echo anchor('products/category/' . $categoryRow->category_id, '&raquo; ' . $categoryRow->category_name); ?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>
</div>

<div id="allProducts">
&lt;?php foreach ($products_query->result() as $productRow): ?&gt;
<div class="productBox">
    <div class="productName">&lt;?=$productRow->product_name;?&gt;</div>
<div class="productImage">
    &lt;?php echo img(base_url() . 'img/products/' . $productRow->product_image_url) . "\n";?&gt;
</div>
<div class="productDescription">&lt;?=$productRow->product_shortDescription;?&gt;</div>
&lt;?php endforeach; ?&gt;
</div>

I don't even know if what I'm doing is even close to how I should do it, and I've never had to do this in PHP before.

So far the view loops through the categories and displays them all with a link like "products/category/1" where 1 is the category_id and it also loops through displaying all products correctly.

If someone could point me in the right direction that would be great. Thank you so much to anybody who helps.


Messages In This Thread
NEW QUESTION -> LAST POST..Categories/URI/Maybe Model/Controller help? Code inside. - by El Forum - 11-18-2008, 06:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB