PHP - Unlimited Sub-categories
How can i make unlimited sub categories when i have this mysql fields: id, name, parent
Code: [Select] $query = "SELECT * FROM cats"; $result = mysql_query($query); while ($var = mysql_fetch_array($result)) { echo $var['name']; // now i need here some code for sub cats, but sub cat in subcat :( } Can someone help me? Similar TutorialsHey all, If I want to have a category which can have subcategories, which in turn can have its own subcategories or belong to the parent category, and the subcategories can have more than one parent categories, is the most effective way to design this is to create a categories table and categories_join table where the categories table has a foreign key categories_join_id linking it to the categories_join table and the categories_join table has a field called parent_id which associates with the primary key of the categories table, therefore allowing me to have multiple subcategories to one category: categories_join table id parent_id 1 2 1 3 categories table id categories_join_id 1 1 2 2 3 3 So from the example above, the first category has two parents, category 2 and category 3. As an alternative option, in this post: http://stackoverflow.com/questions/5384183/database-design-question-categories-subcategories The second answer down mentions to use recursive programming? But he doesn't give an example of what he means. Is he saying you have a function call itself passing it parameters as to what the parent should be: function getParent($category,$parents = array()){ } Thanks for response. Hi everyone ! I am stuck on a task which I'm sure has been achieved by several others in the past. I need to create a category table with unlimited / infinite levels (depths) of categories, sub-categories, sub-sub-categories, sub-sub-sub-categories and so on. I need to know how to show the category tree through PHP. An example would be great or even a link to another page where someone has laid out a step-by-step tutorial. Please help! Thank you very much! Kind Regards, Xeirus. Hello Guys, Need a little help here. I have a db structure like this cat_id | cat_name | cat_status | parent_id 1 First Test 1 0 2 Just testing 1 0 4 Books 1 2 5 Cars 1 0 6 Ford 1 5 If the parent_id = 0 it is considered to be the parent. If it is a number it is considered a child and referencing the parent I want to be able to query the database and grab the cat and the sub cat and echo it on my page. Its like a classified ads application. Here is my while loop that I currently have which only echo's out the parent category. I would like to echo the child category under the respective parent category. I think I would have to do a loop within a loop, but not sure how to do it.. $query = "SELECT * FROM ad_category WHERE cat_status='1' && Parent_id='0' ORDER BY cat_name"; $result = mysql_query($query); while($row = mysql_fetch_row($result)) { $cat_name = $row[1]; $cupper = UCWords($cat_name); echo $cupper . "<br>" ; } Thanks for your help in advance Hi, Please help I need to make categories with su-categories I made in my database tables like this table = categories cid | cat_id | parent_list | categ_name | type 1 0 1 General category c 2 1 1,2 sub category s 3 1 1,3 sub category s 4 1 1,2 sub category s 5 1 1,5 sub category s 6 5 1,5,6 sub sub category s 7 0 7 General category2 c 8 7 7,8 sub category s I need to view this in dropdown menu using parent_list column I have been going in circles about how to handle the issue that I have come up with and was hoping that someone could provide a different method. Basically I want to be able to create an infinite number of categories and allow them to be assigned to other categories then making them subcategories/sub sub categories if you will. My initial plan was to use a method I have used before by just saving the parent id of the subcategories and recursively going through the data. However, I am not to fond of this because of the inefficiency that it provides. I did some research and found that method is called the "Adjacency List". I also found another way to do it called "Nested Sets" which seems to be much more efficient but its a little harder to understand / maintain. I was hoping that someone here could offer alternatives to both of these methods that will allow me to keep the efficiency of "Nested Sets" but providing the ease of use / maintenance of the "Adjacency List". Thanks in advance for any help. I'm building a form using ajax that will allow users to add additional rows to their submissions. The problem however is getting PHP to recognize those variables. So that if the user submits 5 rows, it reads and creates a variable for all 5. I'm pretty sure that doing this will involve some form of while() processing and a total row count, but I'm unsure how to name the variables on the form end and how to receive those into PHP without the script erroring out. Suggestions? I was thinking something along the lines of naming each variable something like "number_title" or as an example "5_title." Then using a while function to pull the data from $_POST[$number.'_title']; or something similar, but I'm unsure if that method would even work. Quote
#adf #234432 #fwerfw I have this as a result of a form that has been completed. It might be twice that, it might be much much more. But what I want to do is render it on a page inside Divs. So each one is in a kind of shaded box. The CSS side of it is easy, but how do I extract each one of these into a variable useful means to surround each one by <div>$variable</div>? I thought it might be "explode", but you appear to have to state the [3] number of the variable to show. In this case, it's totally random. We won't be using these as Variables to trigger anything else, it's purely "for display" purposes. I have two tables, one called Categories and one called SubCategories. Each subcategory needs to be displayed under its parent category in a HTML table. So I have this code, but I don't know how to list each Sub-Category under each Category in a HTML table. <?php require ('includes/initialize.php'); require 'classes/database.class.php'; $sql = "SELECT * FROM Categories, SubCategories WHERE Categories.ID = SubCategories.CatID"; $Result = $database->Query($sql); $Data = $database->Fetch($Result); ?> Hi guys I need help writing a php script I have a table 'categories' with fields 'id' 'cat_name' 'parent_id' the 'parent_id' is just an the id field.. I want to create a way to have unlimited sub categories so I can have categorie -> sub-categories categories -> sub-categories -> sub-categories categories -> products Hello dear friends, If i've the following ( categories - gifts ) <?PHP $sql ="select * from categories"; $result= mysql_query($sql); while($line= mysql_fetch_array($result)){ $qryrow="select * from gifts where gifts='$line[1]'"; } ?> it means select category and its gifts now my question how to view it all in table for example if i add Code: [Select] <TABLE cellSpacing=1 cellPadding=1 width="100%"> <TBODY> <TR> <TD> <?php echo "". $line[1].""; ?> </TD> </TR> </TBODY> </TABLE> it will show me like this How can i make it like this my problem how to make table like this and where to put the php variable .. thanks Hi there, I have a piece of code that takes all posts with the category of 'model' and puts it on the models page. It then displays the post as a gallery using fancy box. The problem is I want all posts with the category of 'faces' to go on the faces page, but at the moment they are appearing on both pages. Here is the code I have. Any help will guarantee a donation as I am at my wits end trying to figure this out. Code: [Select] <?php global $post; $args = array( 'numberposts' => 8, 'offset' => 0, 'category' => 'models', 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish' ); $posts_array = get_posts( $args ); $x = 1; // track model foreach( $posts_array as $post ) { setup_postdata($post); ?> <div id="model-thumb"> <?php $arg2 = array( 'post_type' => 'attachment', 'numberposts' => -1, 'order' => 'ASC', 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($arg2); if ($attachments) { $i = 1; // track images foreach ( $attachments as $attachment ) { $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'full' ); if ($i == 1) { // Get thumbnail info to display with second image $img1src = $image_attributes[0]; $img1w = $image_attributes[1]; $img1h = $image_attributes[2]; } elseif ($i == 2) { // Display thumbnail with link to second image ?> <a class="allModels" rel="gallery<?php echo $x; ?>" href="<?php echo $image_attributes[0]; ?>" ?><img src="<?php echo $img1src; ?>" width="<?php echo $img1w; ?>" height="<?php echo $img1h; ?>"/></a><br /><p><?php the_content();?></p></div> <?php } else { // Other images ?> <div style="display:none;"><a class="allModels" rel="gallery<?php echo $x; ?>" href="<?php echo $image_attributes[0]; ?>"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"/></a></div> <?php } $i++; // Increase image count } } $x++; // Increase model count } ?> Im struggling with how to filter items that have multiple categorises say if i wanted to search for a shoe that is good for walking and hiking in my database? Shoe_id Shoe name color_id type_id colors color_id color type type_id type My problem is what do i do in a situations like this? say a shoe is good for walking and running? aka type 1 and 2 shoe id 1 shoe name nike color id 1 type id 1 and 2 Hello,
New to php&mysql here and need a little help.
I want to make category list with sub-categories and sub-sub-categories. The goal is when I load website I will see full list with master(root)categories . Then when I click some categories should load sub-category and if I click on sub-category should load sub-sub-category. Something like this:
Computers -> Motherboards -> Model 1 -> Model 2 -> etc. Monitors -> Model 1 -> Model 2As you can see max level of sub-category will be 2+root. Here is the perfect example what I want to do http://3docean.net/category I don't understand(know) also how exactly will look my database table? How to store categories and sub-categories. I don't want someone to write full code etc as I want to learn it but will be great if you guys can show me some good tutorial how to make it mostly database structure. Thank you in advance! p.s. Sorry for my English! Hi, So i'm having a little trouble trying to simplify some code I already have. Code: [Select] $result = mysql_query("SELECT images, cat_id FROM image_cat"); while ($row=mysql_fetch_array($result)) { $cat_id=$row['cat_id']; echo $cat_id; $imgresult = mysql_query("SELECT * FROM images WHERE cat_id='".$cat_id."'"); while ($col=mysql_fetch_array($imgresult)) { echo $col['img_id']; } } Okay, so this works, however i'm sure I can bring it together under one query, though i'm truly stumped as to how? I thought I could do a simple join: Code: [Select] $result = mysql_query("SELECT image_cat.cat_id, images.img_id WHERE image_cat.cat_id=images.image_cat"); This works, but repeats the image category on each loop. I could remove this via php if statements, but there must be a simpler/mysql solution. If it doesn't make any sense I need to get Img cat 1 -img 1 -img 2 img cat 2 -img 3 -img 4 etc etc That join will give img cat 1 -img 1 img cat 1 -img 2 img cat 2 -img 3 Hope that makes sense?? And any help would be amazing! Thanks. Hi, I have been working on an eCommerce platform for the last few months. Its almost complete but I need to do one thing and thats to only display sub categories if products exist in them. This is the site craftgo.me (development site), and this is how the categories are produced for a category page. ie. http://www.craftgo.me/category.php?cat_id=96 Code: [Select] if($_GET['cat_id']) { $parents_id = $_GET['cat_id']; $cat_num_rows_sql = "SELECT category_id, parent_id FROM mccategories WHERE parent_id = '$parents_id'"; $cat_num_rows_result = mysql_query($cat_num_rows_sql); $cat_row_count = mysql_num_rows($cat_num_rows_result); if($cat_row_count == '0'){ $current_cat_parent_sql = "SELECT parent_id FROM mccategories WHERE category_id = '$cat_id'"; $current_cat_parent_result = mysql_query($current_cat_parent_sql); $cat_parent_row = mysql_fetch_array($current_cat_parent_result); $parent_id = $cat_parent_row['parent_id']; } else { $parent_id = $_GET['cat_id']; } } $catsql = "SELECT category_id, category_name FROM mccategories WHERE cat_showing = '0' AND parent_id = '$parent_id' ORDER BY category_name"; $catresult = mysql_query($catsql); while($catrow = mysql_fetch_array($catresult)) { $category_id = $catrow['category_id']; $cat_name = $catrow['category_name']; echo '<li><a href="category.php?cat_id=' . $category_id . '">' . $cat_name . '</a></li>'; } In the mcproducts table the category id for each product is under 'product_category' Im not even sure where to start to make this work, but if anyone could give me a pointer, that would be super. Many thanks Eoin Hi all am haiving abit of a problem am trying to query 6 categories with 6 games each.With the code i done is only showing one cat with 6 games.
How can i fix this ?.
If you go to games-flash.co.uk you will see at the bottom of the home page that only one cat shows.
php code.
<div class="catgames"> If i have a list of categories like: 1. Billboards and advertisement section 1(a)City Clock 1(a)(i)Application fee (has columns for approved price and proposed price) 1(a)(ii)Advertisement Per year (has columns for approved price and proposed price) 1(b)Billboards 1(b)(i)Application fee for construction (has columns for approved price and proposed price) 1(b)(ii)Charge per year without advertisement (has columns for approved price and proposed price) 1(c)Something here..... 1(c)(i) something here...(has columns for approved price and proposed price) 1(c)(ii)something here...(has columns for approved price and proposed price) 1(c)(iii) something here... (has columns for approved price and proposed price) e.t.c THEN SOME CATEGORIES LOOK LIKE:category 2 2. Category name 2(a)something here..(has columns for approved price and proposed price) 2(b)something here.. 2(b)(i)something here..(has columns for approved price and proposed price) 2(b)(ii)something here..(has columns for approved price and proposed price) NOW: i have categories without sub categories and others with sub-sub categories , a main category doesn't does have a column for prices ( or lets just say other columns) , a sub category WITHOUT its sub category will have columns for prices, if a sub category has its sub category, then its subcategory should have columns for prices (this is where my problem is - how do i handle such a situation in php Mysql?? ) I have made a custom post type for products, and am using a custom taxonomy for the product categories. The thing that I am wondering about is that sometimes a product category is a parent, and sometimes a child, but sometimes both. For instance, if you have a top level category named shoes, then you might have a sub category named Nike. However, Nike also has shirts, so you could say that shoes and shirts were both subcategories of Nike. Do you know what I'm saying? It's like there is no real parent to child relationship, but they are both parents and both children of each other. How do you handle this type of situation?
Hello, I am doing (well trying to do, anyway) a script where I need to follow a link, and through its source (file_get_contents) i need to to follow each "category" and into its "subcategory" (and sometimes even SUBsubcategory). Lets say the first in the menu is called "Catfood", the second "Dogfood", you click on "Catfood" and you get a submenu with for example "Whiskas", another one called "Purina Pro", and you click "Whiskas" and you see a list of food called for example "Whiskas Junior Chicken" and "Whiskas Junior Fish". then after i have followed "Whiskas", i need to go back and follow "Purina Pro". then after "Purina Pro" i need to go back to "Dogfood" and do the same to its submenu + subsubmenu + food menu.. so yeah, thats pretty much it basically.. i have already used wget for windows to download the entire website to not put load on it all the time when trying stuff out.. i use RegEx to find categories, their products and price, and i got all that covered, it's just that the website isn't built very friendly for using Regex to tie lets say "Purina Pro" with being in the "Catfood" category, so i have to go through all categories and subcats to save the categories in maybe an array, and bind the subcategories to the main category ("Purina Pro" with "Catfood") i hope this all doesn't sound too errr weird lol, any help is very much appreciated even enough to just get me started on my own! =) ****************** EDIT BELOW: The menu looks a bit like this, though it's orinally not about cat or dogfood, they are just examples Catfood - Whiskas - Purina Pro Dogfood - Royal Canin - Puppy food - Grown - Senior - Bozita Robur Hello! I've got exactly what I've wanted in my previous topic so I have decided to make a new topic for some help. Quote General General Discussion Description here Entertainment Discussion Description here Gaming Console Discussion Description here Handheld Discussion Description here And a database like this; id title description parent 1 General description here 0 2 Gaming description here 0 3 General Discussion description here 1 4 Entertainment Discussion description here 1 5 Console Discussion description here 2 6 Handheld Discussion description here 2 Code: [Select] <?php function generate_forums($parent) { $has_childs = false; global $forums_array; foreach($forums_array as $key => $value) { if ($value['parent'] == $parent) { if ($has_childs === true) { $has_childs = false; } echo '<a href="/category/' . $value['title'] . '/">' . $value['title'] . '</a><br>' . $value['description'] . ''; generate_forums($key); echo '<br>'; } } } generate_forums(0); ?> The code works wonders. But it is hard to customize template wise. $ID#1 $title = General I would start off with <table> I need to have the Categories that Parent = ( 0 ) to have a HTML <tr> I also need the Categories with Parent = ( Value ) to have a HTML <td> Then the tags would be closed off before $ID#2 $title = Gaming's template would start again. I keep trying to scramble the coding around and can't seem to find that probably "simple" touch. Thanks in Advanced. |