Mark Alan
Mark Alan

Reputation: 455

displaying products with categories

I am having trouble with uri segments in codeigniter I am looking to develop products and product details so I created a controller for it and passed a parameter of product name to show product name in the url so my url will become like this

localhost/project_name/products/product_name

and this part is working fine for me but I am confused about what If I would like to display as products category and product name like I am trying to make my url like this

localhost/project_name/products/category_name /product_name

so how I will do this now when I pass in category name it shows me 404 not found The page you requested was not found. as some times direct product will be shown and sometimes if users go through the product details via category so the category name will passed in the url and also how this segements work ? I am having big confusion about this

Here is my controller for products

public function products($product_name) {
        if($this->uri->segment(3)) {
            $category = ucwords(str_replace('-', " ", $product_name));
            $product  = ucwords(str_replace('-', " ", $this->uri->segment(3)));
        } else {
            $product  = ucwords(str_replace('-', " ", $product_name));
        }

        $page_data = $this->get_data->get_Data($product, 'name', 'products');
        $data['title'] = 'Products - ' . $page_data->row()->name;
        $data['body_class'] = 'poductsdetails';

        $this->load->view('includes/header.php', $data);
        $this->load->view('templates/navigation.php');

        $data['content'] = array(
                'name'        => $page_data->row()->name,
                'price'       => $page_data->row()->price,
                'description' => $page_data->row()->description,
                'picture'     => $page_data->row()->picture,
                'category'    => $page_data->row()->category,
            );

        $this->load->view('products/details', $data);
        $this->load->view('templates/footer-form.php');
        $this->load->view('includes/footer.php');
    }

And here is my routes I have created

$route['default_controller'] = 'home';
$route['admin']              = 'admin/index';
$route['admin/([a-z])']      = 'admin/$1';
$route['products/(:any)']    = 'home/products/$1';
$route['products/(:any)']    = 'home/products/$1/$2';
$route['products/(:any)']    = 'home/products/$1/$2/$3';
$route['(:any)']             = 'home/page/$1';
$route['(:any)']             = 'home/page/$1/$2';

Upvotes: 2

Views: 212

Answers (2)

Owais Aslam
Owais Aslam

Reputation: 1589

You have just passed one parameter in route that's why it can't find

change your route like this

$route['products/(:any)']    = 'home/products/$1';
$route['products/(:any)/(:any)']    = 'home/products/$1/$2';
$route['products/(:any)/(:any)/(:any)']    = 'home/products/$1/$2/$3';

It will work.

Upvotes: 6

Harshita
Harshita

Reputation: 442

if category_name is function defined in this controller then try this

$route['products/category_name/(:any)'] = 'products/category_name/$1';

Upvotes: 0

Related Questions