CodeIgniter Forums
Category and Subcategory URL best practice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Category and Subcategory URL best practice (/showthread.php?tid=45884)



Category and Subcategory URL best practice - El Forum - 10-10-2011

[eluser]Nigey B[/eluser]
Hello,

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

/products/category/subcategory/product

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

Anyone?