I know the title may not be exactly what this is about but bear with me.
I don't know how another title for this.
Well look this is my situation.
I'm building a little cms system (for myself and to learn from it). I want the pages inside the CMS to be listed and ordered by categories.
It will look something like this:
Webpages
- Home
-- homepage(this is the web page itself)
- News
-- Latest news
-- Archive
this system would mean I will have sub-categories.
In the database I have made a table:
| ID | Parent_ID | Name | Lable | Order|
1 1 Webpages webpages 1
2 1 Home home 1
3 1 News news 2
As you can see here the main category is the Webpages category and Home and News are sub-categories of it.
And those 2 categories are ordered so the Home category is first then the News second.
The problem I'm facing is this:
If i want to get all the sub-categories means i need to start with the main category Webpages and with that ID i can get the sub-categories of the main category.
I think you can see how deep this can go en that would mean (I think) that there will eventually be many query's that will be run for each sub-category.
So my question is:
Is there a way to get all the sub-categories at once in the correct order in one ore 2 query's.
if you have an answer, please let me know.
thnks
When writing the query you will need to use the ORDER BY clause. For this specific example you will need to ORDER BY Parent_ID, Order. This will return sorted by Parent_ID and then sub sorted by Order.
You can then use mysql_fetch_assoc() for parse the result set into an associative array.
Since you are going to have multiple levels of hierarchy you will have to loop through the result set once store it in a associative array consisting of the hierarchy structure.
This array can then be used to display the appropriate navigation hierarchy on the page.
Related
I have been trying to figure this out, but am getting lost in the possible option. I have a set of categories in my Wordpress, up to 3 levels deep. I would like to generate a list of links to each category, keeping the hierarchy in order. The format I'm after would be something like this:
Parent
-Child Level 1
-Child level 1
--Child level 2
-Child level 1
Parent
-Child level 1
--Child level 2
I have figured out how to alter the query to always return empty categories etc, but I just can't get the format right...
Without any code, I can not help you much, but wp_list_categories() function should do exactly what you want by default.
Note that, to indent the lists correctly, you will need to add some CSS, instead.
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:
Categories
id | parent_id | name
1 | 0 | Home
2 | 1 | Garden
3 | 1 | Furniture
4 | 1 | Plumbing
5 | 2 | Lawn Care
This would equate to a hierarchy like the following:
Home
Garden
Lawn Care
Furniture
Plumbing
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:
I'm in Lawn Care. Does Lawn Care have a parent?
Yes -> Garden. Does Garden have a parent?
Yes -> Home. Does Home have a parent?
No -> End.
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.
You can use URLs like \Home\Garden\LawnCare and rewrite them using .htaccess and mod_rewrite.. And when user clicks the Garden or the Home, you can easily go back to the requested page...This is such a simple solution to the problem...
I have a category table like this:
I can list categories as list and subcategories as dropdown list but My designer send me a design that categories have subcategories with the same name:
How can I manage this type of subcategories?
Thanks in advance
Sounds to me a "design" issue rather than a database issue.
You can add a dash symbol before the subcategories names in the dropdown, so the options values would be:
Category 1
-Subcategory 1
-Subcategory 2
Category 2
-Subcategory 1
-Subcategory 2
My appending " -" before the subcategory's title, you are creating levels in the dropdown, which the user can visually see, and recognize where each subcategory belong to.
It's not a perfect solution, but it's sure easy to implement. If you're willing to spend time on it, try research jquery plugins that have multi level select boxes support, such as http://php4bd.wordpress.com/2007/07/15/javascript-controlled-dependent-or-cascading-select-list/
You have id to uniquely identify your tuples. Duplicated names aren't a problem.
Also read: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
I'm looking for a solution to list and browse categories and subcategories and their records (classified ads), when you store category levels in separate tables. In the past I have worked with the adjacency model but I have to stick to this database setup now, and it is new to me. I'm using php and mysql.
The site is a classified ad site structured the common way: it has the main category list on its homepage, when you click one of the category links then only its subcategories are listed and the ads that belong to this category, and so on, at every level.
I'm a bit confused in the following areas:
How do you construct
the category links when browsing
categories in order for the script to know which table it should select categories from if I consider the below
mysql structure? Do I need separate
parameters at every category level I access
like e.g: "mysite.com/?cat2=4" when
accessing category "4" in the cat2
table and "mysite.com/?cat3=9" when
accessing category "9" in cat3 table
in order to identify category
levels? If separate parameter not
needed, then how can php and mysql
tell what table you have to select
the categories from?
And most
importantly in this case, what is
the best way to construct SEO
friendly links? And how will mysql know
which table to select categories
from? I would like to use the most
simplest solution that is possible
like:
mysite.com/electronics/television/sony.
As far as I know, I have to include
at least the cat_id
somewhere in the link... where do I put it? and do I have to include the number of level as well? To
complicate it more the category
names are in foreign language with
accented characters (though I
created a function that changes
accented characters into latin ones
on the fly when generating category
links) so I think it is best to
select them by their ids.
How is a sample mysql select looks
like that selects the child
categories of a certain category?
How can I construct breadcrumb
navigation?
MYSQL STRUCTURE:
Table "cat1" (main category):
cat1_id | cat1_name
Table "cat2" (subcategory):
cat2_id | cat1_id | cat2_name
Table "cat3" (subsubcategory):
cat3_id | cat2_id | cat3_name
Table "ads":
ad_id | cat1_id | cat2_id | cat3_id | ad_title | ad_description
Thanks and sorry for the long post.
My favourite pattern for category (and tag) URLs:
http://mysite.com/articles/brains+zombies+legs+frogs
The + symbol is nice for tags, and friendly to spiders (and SEO). Using the text of the categories is important for both spiders and humans as it's meaningful.
As for the SQL, I suggest 2 tables for anything with categories or tags:
Categories (id, name, description)
CategoryRelationships (catID, thingID)
For any given thing, you join Categories to Things via CategoryRelationships. For example:
SELECT * FROM Things t
JOIN CategoryRelationships ON thingID = t.ID
JOIN Categories c ON catID = c.CatID
The result will be a list of Things and their categories, where you have only one definition of each category, and a bunch of links to the categories via the Relationship table.
As for breadcrumbs, they're a slightly different problem. Breadcrumbs either:
Provide navigation through your site hierarchy, or
Help the user retrace their steps
Depending on the type of breadcrumb you're aiming at, you take a different approach. For a simple site hierarchy set of breadcrumbs, you can simply parse the URL and foreach over the set of segments:
http://mysite.com/people/zombies/brains/brains
Parsing the URI would result in:
people, zombies, brains, brains
For which you would generate links to each segment.
I have a table that defines the possible categories in my website - fields look something like this:
- id
- name
- parentID
The information is stored something like this:
+-----+------+----------+
| id | name | parentID |
+-----+------+----------+
| 1 | pets | 0 |
+-----+------+----------+
| 2 | cats | 1 |
+-----+------+----------+
| 3 | dogs | 1 |
+-----+------+----------+
A parentID of 0 indicates that the category/page is on the home level. I'm looking for a way to quickly and easily generate the parent categories.
The first method that came to mind was a series of SQL queries, but I quickly realised that this would be insidiously resource intensive the more complicated the site got.
Reading through the mysql manual, I've seen that mysql can use loops and conditional statements, however I'm unsure how I'd put those into practice here.
Ideally, I'd like to have a single query that pulls up all directly related parent elements.
If I were looking at the Pets category, I would only see home because it's on the top level. As soon as I drill down (either into cats, dogs or a page under pets) then I should see pets on the bar - the same goes for subsequent child categories and pages.
What's the most efficient way to generate a list of categories using information stored in this fashion? If this question requires more clarification, please ask, and I will do my best to provide more information.
Clarification: This is part of a CMS - and as such, users are going to need the ability to make changes to categories on the fly. I've looked at several data storage schemes (such as nested sets) and they do not appear to lend themselves well to a simple form for making changes to navigation.
As such, any method needs to be easily a) understood by a user, and b) implemented easily to a user.
The categories are best described as folders on a PC, rather than tags. When you view any given category, you can see the immediate children of that category, as well as immediate child pages.
When you view a category or a page, the parent categories (but not itself are visible).
Example: I have German Shepard which resides under dogs which is under pets
When viewing *pets*: Home
When viewing *dogs*: Home -> Pets
When viewing *German Shepard*: Home -> Pets -> Dogs
Consider using "nested sets" model instead: Managing Hierarchical Data in MySQL.
Update (based on clarification to the question): The nested sets model does not have to be (in fact I have a pretty hard time imagining why would it be) exposed to end users. All directory-style operations (adding a new folder / subfolder; moving folder to a different path, etc...) can be supported in nested sets model, though some are a bit harder to implement then others. The article I've linked to provides examples for both adding and deleting of (sub)folder.
Could you have a stack or ordered set (ordered by how the user applied filters to their browsing) containing your breadcrumb, stored on the session?
I could see it getting grim when you started cross-querying, but sometimes data isn't hierarchical, but more of a soup of tags, and the above starts being your tag-soup clarification breadcrumb.
Most websites don't actually feature good (or any) tag soup drilling down. E.g., how many times have you been look at the sale CDs on a website, and wanted to drill down to just see the Metal CDs (for example), but clicking on the "Rock and Metal" link on the left took out to the top level metal category, instead of acting as a filter on your current browsing state.
So - is your problem actually a tag soup that you're applying a false hierarchy onto? Should you in fact be looking at automatic tag generation libraries that you can pass your items into, and tag lookup mechanisms? Okay, I'm sure your personal website won't be complex enough to ever require tag search, but in general terms, I think it is worth thinking about.