I am creating a product catalogue which will be browsed through database queries with the results displayed in a div, without page refresh-via ajax.
Category examples would be:
Home
Health
Entertainment
There are also subcategories for each category, i.e:
HOME:
Garden
Furniture
Plumbing
Etc.
I want to make a little directory thing that shows exactly where they are, something like:
Home >>> Garden >>> Lawn care
With each of those as a clickable link to take the person back to that specific query level.
My code is 1 .php document, involving a query and code to output the query. If output is clicked on, it triggers an ajax script which points back to and reruns the same query/output, but with different results.
This being said, i dont know how i would create a way to store and display the directory path. That i mentioned above.
I was thinking of some way that takes the category that was clicked on and passes it through the url so that the php has the value when it reloads. And then i work that variable into a clickable directory link. But the problem is I’m not sure how to do that for multiple layers.
i.e.
If someone just clicked on “garden” i could pass the garden variable through and use that in the nav, but then if someone clicked on “lawn care” i wouldnt be sure how to keep the “garden” variable because the variable i brought over via the url would now read “lawn care.”
I feel like it has something to do with dynamically adding and storing the cumulative values in an array, but I’m really out of ideas…
From what you’ve described, it sounds like you want to implement bread crumbs/categories rather than directories.
If this is the case, you’d basically need to create a Categories table in your database like so:
This would equate to a hierarchy like the following:
So if I want to have a product show, for instance, in Home > Garden > Lawn Care, I’ll need to link the product to Category #5 (Lawn Care). Then I need to develop a function to do a little while loop that figures out the parent structure from there. It will need to loop until it doesn’t find a parent (or until parent_id = 0). In other words, it would go:
There are a number of ways to implement this, which is why I left specifics out.
Alternatively, you could just do this calculation on save of the product or category so it can map out the hierarchy the one time instead of every time (saving on calculations), but this would be a very simple solution that could work for a large number of products.
The benefit to doing it this way is that you can also implement product lists based on the category you’re in, and from another perspective, you can create product counts per category.