views:

83

answers:

1

Hi there,

I am looking at writing an ecommerce platform just for working practice and a small project of mine. I am at the stage where i want to start writing the URL's and SEO friednly URL's.

My problem is i have wrote the system to have unlimited categories / sub categories and making my navigation is causing me some problems.

my ideal url would be:

http://mydomain.com/products/first-category/second-category/productname.aspx

and in order to achieve this i am thinking about looping back to the database where the parentid=. but i feel this is not going to be best practice and doing this for every category and sub category and product for each page seems wrong.

can anyone help how to achieve this ?

I have knowledge of using www.urlrewriting.net and writing the website in asp.net 3.5 but its the technical answer of looping gettings the names etc i need help with.

If you require anymore information i am happy to answer any questions and hope someone can help me achieve this.

Thanks

A: 

Just cache the entire table of categories to memory if you're worried about going back to database.

struct Category
{
  UInt32 CategoryId {get;set;}
  UInt32 ParentId {get;set;}
  string Name {get; set;}
  string Url {get; set;}
}

SELECT CategoryId, ParentId, Name, Url FROM Categories Order By CategoryId;

MyCategories = new Dictionary<UInt32, Category>();

while (rdr.Read)
{
  MyCategories.Add((UInt32)rdr["category_id"], new Category(........));
}

Now getting the complete path is just looping in memory

string path = "";
currentCategory = MyCategories[category_id];

while (currentCategory != nil)
{
  // StringBuilder here 
  path += "/" + currentCategory.Name;
  currentCategory = MyCategories[Category.ParentId];
}
Jorge Córdoba
ok thats great! once i have my product id (end of the url) what is the best approach to work backwards. i.e on my product display page (list of products) would you recommend just concatinating on the end of the current category URL (+= /nameofproduct.aspx" or do the complete loop to find what category / subcategory the product belongs to then redirect. hope that makes sense.
You mean like SELECT category_id from ProductCategories WHERE product_id = id ??
Jorge Córdoba
yes but for example the category id from the productcategories table will be the sub category ID e.g.top level categorysub categoryanother sub category - product << the category id will be of a lower level.so then what is the best way to then get all the higherarchy categories before that (top level) ID to make the rediret goto: http://mydomain.com/products/first-category/second-category/product.aspx. Thanks again for your help and your patience.
I'm sorry, I don't follow where's your problem, the last part of the code shows how to loop to the higher levels of your categories given the root level, that is, you estar with the "another subcategory id" and loop up through the parent_id up to the root.
Jorge Córdoba
ok thanks, i will give it a go. thanks again
ok been trying this out: MyCategories.Add(new Category(........)); <-- does this line represent a problem due to the dictinary expects an int.
Index it by CategoryId
Jorge Córdoba