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.
#2

[eluser]neen[/eluser]
Pretty simple, really...

Something like this in a controller
Code:
function categoryProducts($id)
{
    $this->category_model->getProducts($id);
}

Then in your model (assuming you use AR as you did above):
Code:
function getProducts($id)
{
    $products = $this->db->get_where('products', array('category_id' => $id))->result_array();
    return $products
}
#3

[eluser]ColinHoernig[/eluser]
EDIT: Actually, this works fine! The problem was in my autoload, it was trying to load a model that had a ton of errors and it just quit loading the page.



Well I had it partially working yesterday..fooled with it today without testing on the server and now it's all broken..

Heres my new code:

products view (products.php)
Code:
&lt;?php
    $this->load->view('header');
    if ($category_query->num_rows() > 0):
?&gt;
    <div id="categories">
        <ul class="categories">
            &lt;?php foreach($category_query->result() as $categoryRow): ?&gt;
                <li class="catMenu">
                    &lt;?php echo anchor('products/category/' . $categoryRow->category_id, '&raquo; ' . $categoryRow->category_name); ?&gt;
                </li>
            &lt;?php endforeach; ?&gt;
        </ul>
    </div>
&lt;?php else: ?&gt;
    <div class="errorBox">
        No more categories
    </div>
&lt;?php endif; ?&gt;
<div id="allProducts">
&lt;?php if ($product_query->num_rows() > 0):?&gt;
    &lt;?php foreach ($product_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;
            {$productRow->product_shortDescription}
        </div>
    &lt;?php endforeach; ?&gt;
&lt;?php else: ?&gt;
    <div class="errorBox">
        I'm sorry, this category does not contain any products.
    </div>
&lt;?php endif; ?&gt;
</div>
&lt;?php
    $this->load->view('footer');
?&gt;

products controller (products.php)
Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Products extends Controller {

    function __construct() {
        parent::Controller();
        $this->load->model(array('category_model', 'product_model'), '', TRUE);
        $this->load->scaffolding('products');
    }

    function Index() {
        $data['category_query'] = $this->category_model->getAllCategories();
        $data['product_query'] = $this->product_model->getAllProducts();
        
        $data['page_title'] = "Products - All";
        $data['heading'] = "Products - All";
        $this->load->view('products', $data);
    }
    
    function Category() {
        $data['page_title'] = "Products";
        $data['heading'] = "Products";
        
        if ($this->uri->segment(3) === FALSE) {
            redirect('products/');
        } else {
            $data['category_query'] = $this->db->get_where('categories', array('category_parentID' => $this->uri->segment(3)));
            $data['product_query']     = $this->db->get_where('products', array('product_categoryID' => $this->uri->segment(3)));
            $this->load->view('products', $data);
        }
    }
    
}

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

Same models as first post. When I load (in MAMP) http://localhost:8888/sitename/index.php/products/ I don't get an error, so it's getting to the controller, but I get just a blank page with absolutely no client side source code. Can someone please help? I don't know what I'm doing wrong Sad.
#4

[eluser]ColinHoernig[/eluser]
One more question, how would I go about selecting all the products in all of the subcategories listed?

For instance, if I click the product category "Apparel" which contains the subcategories "Shirts", "Pants", "Jackets", etc, I want the page to display all of the products that are located in all of the subcategories (because the subcategories are just being used to limit the selection).

If anybody could help me out that would be great!
#5

[eluser]MEM[/eluser]
Have you found your solution? Smile

I'm following Professional Codeginiter Book as well, and I have doubts about, how to deal with n categories instead of two.

Regards,
Márcio




Theme © iAndrew 2016 - Forum software by © MyBB