[eluser]Twisted1919[/eluser]
You don't really need any category_id in the url at all.
In a weekend site that i worked, and finished it 5 minutes ago, i had to deal with category and products also, and my final urls are like :
Code:
// For listing products from acertain category
http://www.website.com/categories/category-unique-name.html
// Viewing a product
http://www.website.com/categories/category-unique-name/product-category-unique-name.html
This is great for SEO, as you have the url very descriptive.
In the database, you would add a url field and index it, for category and for product, then you will be sure that when adding categories, the generated url(which is the name transformed with url_title() function) is unique . The same you would do for products, but be sure that the product name is unique for a category so that you can have two products with same name in distinct categories.
Also, here is my code ;
Code:
//Categories Controller
public function index()
{
$route = $this->uri->segment(3,'index');
$category_url = $this->uri->segment(2);
$product_url = $this->uri->segment(3);
if(empty($category_url) || ! $cdata = $this->mdl->get_category_by_url($category_url))
{
redirect('products');
}
$this->load->model('products/products_model','pmdl');
$this->data['cdata'] = $cdata ;
//view the products from a certain category
if($route == 'index')
{
$numrows = $this->pmdl->count($cdata->category_id);
$this->per_page = 12 ;
$this->load->library('pagination');
$config['base_url'] = rtrim(site_url(),'/').'/categories/'.$category_url.'/index/';
$config['total_rows'] = $numrows ;
$config['per_page'] = $this->per_page ;
$config['uri_segment'] = 4;
$config['num_links'] = 2;
$config['cur_tag_open'] = '| <strong>';
$config['cur_tag_close'] = '</strong> ';
$config['prev_tag_open'] = '';
$config['prev_tag_close'] = '';
$config['next_link'] = '»';
$config['prev_link'] = '«';
$config['next_tag_open'] = '';
$config['next_tag_close'] = '';
$config['full_tag_open'] = '';
$config['full_tag_close'] = '';
$config['num_tag_open'] = '|';
$config['num_tag_close'] = '';
$config['last_link'] = '';
$config['last_link'] = '';
$config['first_link'] = '';
$config['first_link'] = '';
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$segment = intval($this->uri->segment(4,0));
$this->data['records'] = $this->pmdl->get_products($segment,$this->per_page,$cdata->category_id);
$images = array();
if($this->data['records'])
{
foreach($this->data['records'] AS $p)
{
$images[$p->product_id] = $this->pmdl->get_product_images($p->product_id,1);
}
}
$this->data['images'] = $images ;
$this->template->load('categories/index',$this->data);
}
else
{
if( ! $data = $this->pmdl->get_product_by_url($product_url,$cdata->category_id))
{
redirect('products');
}
$this->data['images'] = $this->pmdl->get_product_images($data->product_id,3);
$this->data['data'] = $data;
//next/prev products
$this->data['next'] = $this->pmdl->get_next_product($data->product_id,$data->sort_order,$cdata->category_id);
$this->data['prev'] = $this->pmdl->get_prev_product($data->product_id,$data->sort_order,$cdata->category_id);
//3 recommended products from each category .
$categories = $this->mdl->get_all(2);
$recommended = $images = array();
foreach($categories AS $c)
{
if($recommended[$c->url] = $this->pmdl->get_recommended_products($data->product_id,$c->category_id))
{
foreach($recommended[$c->url] AS $p)
{
$images[$p->product_id] = $this->pmdl->get_product_images($p->product_id,1);
}
}
}
$this->data['rec_images'] = $images ;
$this->data['recommended'] = $recommended;
$this->template->load('products/view',$this->data);
}
}
Hope it helps you, if you need any more info, just let me know.