Welcome Guest, Not a member yet? Register   Sign In
Category and Subcategory URL best practice

[eluser]Nigey B[/eluser]

I've just started using CodeIgniter so bare with me if the below is obvious.

I have a controller called 'Products' which has a function called 'Category'.

On the front end of the site I have the following URL's


So I have the following Route set up: $route['products/(:any)'] = "products/category";

I am using the Helper > URL to break the URL down into segments in my Controller so that I can make a call to the model which has the below:


function getProductDetail($categorySlug = NULL, $subCategorySlug = NULL, $productTitle = NULL) {

$query = "SELECT * FROM tblproducts WHERE productTitleSlug = '".mysql_real_escape_string($productTitle)."'";

if ($subCategorySlug != '') {
$query .= " AND productSubCategorySlug = '".mysql_real_escape_string($subCategorySlug)."'";
if ($categorySlug != '') {
$query .= " AND productCategorySlug = '".mysql_real_escape_string($categorySlug)."'";

$sql = $this->db->query($query);

if ($sql->num_rows() > 0) {
$data = $sql->result_array();
return $data;




It all works OK until a product doesn't have a subcategory (eg /products/category/product) as the product moves from being Segment 4 to segment 3

I could put loads of code into my Controller to work out whether segment 3 is the product or sub category but I can't help thinking that must be a better way of doing this.....


Theme © iAndrew 2016 - Forum software by © MyBB