PHP - Get Categories And Subcategories From Website
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 Similar TutorialsHello,
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! I have two tables subcat and category structure subcat id_subcat|id_of_cat|subcat_name| and for main categories ID_cat|cat_name| now what I want to display them as Main category subcategory1 subcategory2 Main category2 subcategoryX etc... Here's the code I'm using but doesn't work,it doesn't display anything. <?php $query = " SELECT ID_cat, id_subcat, subcat_name, cat_name FROM subcat, category WHERE ID_cat=id_subcat"; $result = mysql_query($sql); $lastCat = null; while($row = mysql_fetch_assoc($result)) { # Check if you need to print the category_name if($lastCat != $row['ID_cat']) { # Not the same category as the last one! # Print it and save the ID $lastCat = $row['ID_cat']; echo "<br />". $row['ID_cat']; } # Print the sub category echo "<br />". $row['subcat_name']; } ?> Hey 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. This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=327455.0 Anyone can help me? <?php $tbl_name="menu"; $kategorije = mysql_query("SELECT * FROM $tbl_name WHERE Left(menu_name, 1) BETWEEN 'A' AND 'M' ORDER BY menu_name ASC"); if (!$kategorije) { die("Database query failed: " . mysql_error()); } while ($row=mysql_fetch_array( $kategorije )) { echo "<div id=lista-header><h4>{$row["menu_name"]}</h4></div>"; $id_sub=$row['id_menu']; $podkategorije = mysql_query("SELECT * FROM submenu WHERE id_menu=$id_sub ORDER BY sub_name ASC", $connection); if (!$podkategorije) { die("Database query failed: " . mysql_error()); } echo "<ul class=\"pod\">"; while ($pod=mysql_fetch_array( $podkategorije )) { echo "<li><a href=index.php?=podkate?".$pod["id_sub"]." class=black>{$pod["sub_name"]}</a><hr size=1 align=left width=100px color=#cccccc></li>"; } echo "</ul>"; } ?> In this way, I get list with categories and hes subcategories. How to count how many subcategories have each categories, and count how many articles have each categories? Example (I wanna get this kind of look): Categories name (3) subcategoriesname (2) subcategoriesname (4) subcategoriesname (7) Categories name (5) subcategoriesname (1) subcategoriesname (14) subcategoriesname (9) subcategoriesname (2) subcategoriesname ( Categories name (2) subcategoriesname (28) subcategoriesname (17) Where the numbers represent how many categories and sub-items have articles This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=307157.0 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. Hello to you all, Firstly, I have to admit - I know nothing about PHP coding. That said, I tried to make something work, kinda failed, partly done it, but it's not the way I need it. Let me explain. I'm working on some basic stuff on a woocommerce website. I changed the category tree of the products and now there are categories nested quite deep. Like - Parent category -> Subcategory -> Nested category -> Further nested category. That last piece wasn't showing on the sidebar, so I digged and found the PHP code responsible for showing categories. Upon finding the "Nested category" code, I simply copy-pasted it in a right place and then added CSS "display:block" rule. Now, the "Further nested category" is showing, when "Nested category" is active. But, here are the problems: 1. If there are multiple categories of that last depth for various "Nested categories", they are all showing; 2. When I click on a link of any "Further nested category", the page redirects correctly and shows all the required products, but the category tree is shrinking to "Parent categories" only. The store url is: https://skorpion.info.pl/sklep/. The problem #1 can be seen he https://skorpion.info.pl/kategoria/naczynia-i-przybory/naczynia-kuchenne-naczynia-i-przybory/ - as you can see, both "Garnki" and "Patelnie" nested categories show all of their consecutive further nested subcategories. The problem number two can be seen he https://skorpion.info.pl/kategoria/naczynia-i-przybory/naczynia-kuchenne-naczynia-i-przybory/patelnie-naczynia-kuchenne-naczynia-i-przybory/patelnie-aluminiowe/. The categories should remain visible. Let me post the code below, I'll mark with a comment the block I copy-pasted. I really hope someone can help me here, I would be massively thankful. Cheers! <aside class="sidebarWrapper"> <?php $types = array( 'hendi' => 'Hendi', 'stalgast' => 'Stalgast', 'bartscher' => 'Bartscher', 'kromet' => 'Kromet', 'hiendkitchen' => 'Hiendkitchen', ); ?> <ul class="parent-list"> <?php foreach ($types as $type => $value) : ?> <li> <a href="<?php echo home_url(); ?>/hurtownie/<?php echo $type; ?>"><?php echo $value; ?></a> </li> <?php endforeach; ?> </ul> <h2 class="title"> Kategorie <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 492.004 492.004" style="enable-background:new 0 0 492.004 492.004;" xml:space="preserve" width="512px" height="512px"> <g> <g> <g> <path d="M382.678,226.804L163.73,7.86C158.666,2.792,151.906,0,144.698,0s-13.968,2.792-19.032,7.86l-16.124,16.12 c-10.492,10.504-10.492,27.576,0,38.064L293.398,245.9l-184.06,184.06c-5.064,5.068-7.86,11.824-7.86,19.028 c0,7.212,2.796,13.968,7.86,19.04l16.124,16.116c5.068,5.068,11.824,7.86,19.032,7.86s13.968-2.792,19.032-7.86L382.678,265 c5.076-5.084,7.864-11.872,7.848-19.088C390.542,238.668,387.754,231.884,382.678,226.804z" data-original="#000000" class="active-path" data-old_color="#000000" fill="#F1B63E" /> </g> </g> </g> </svg> </h2> <div class="slideDown-wrapper"> <?php $cate = get_queried_object(); $cateID = $cate->term_id; $cateParentID = $cate->parent; $mainCateParentObject = get_term($cateParentID); $mainCateParentID = $mainCateParentObject->parent; $args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'title', 'order' => 'asc' ); $product_cat = get_terms($args); foreach ($product_cat as $parent_product_cat) : ?> <ul class="parent-list"> <li class="<?php if ($parent_product_cat->term_id == $cateID || $parent_product_cat->term_id == $cateParentID || $parent_product_cat->term_id == $mainCateParentID) echo 'active'; ?>"> <a href="<?php echo get_term_link($parent_product_cat->term_id); ?>"> <?php echo $parent_product_cat->name; ?> </a> <?php if ($parent_product_cat->term_id == $cateID || $parent_product_cat->term_id == $cateParentID || $parent_product_cat->term_id == $mainCateParentID) : ?> <ul class="child-list"> <?php $child_args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => $parent_product_cat->term_id ); $child_product_cats = get_terms($child_args); foreach ($child_product_cats as $child_product_cat) : ?> <li class="<?php if ($child_product_cat->term_id == $cateID || $child_product_cat->term_id == $cateParentID) echo 'active'; ?>"> <a href="<?php echo get_term_link($child_product_cat->term_id); ?>"> <?php if ($child_product_cat->term_id == $cateID) echo '<strong>'; ?> <?php echo $child_product_cat->name; ?> <?php if ($child_product_cat->term_id == $cateID) echo '</strong>'; ?> </a> <ul class="child-list"> <?php $child_args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => $child_product_cat->term_id ); $child_product_cats = get_terms($child_args); foreach ($child_product_cats as $child_product_cat) : ?> <li> <a href="<?php echo get_term_link($child_product_cat->term_id); ?>"> <?php if ($child_product_cat->term_id == $cateID) echo '<strong>'; ?> <?php echo $child_product_cat->name; ?> <?php if ($child_product_cat->term_id == $cateID) echo '</strong>'; ?> </a> <!-- COPY-PASTED CODE FROM ABOVE--> <!-- --> <!-- --> <ul class="child-list" id="child-4-nest"> <?php $child_args = array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => $child_product_cat->term_id ); $child_product_cats = get_terms($child_args); foreach ($child_product_cats as $child_product_cat) : ?> <li> <a href="<?php echo get_term_link($child_product_cat->term_id); ?>"> <?php if ($child_product_cat->term_id == $cateID) echo '<strong>'; ?> <?php echo $child_product_cat->name; ?> <?php if ($child_product_cat->term_id == $cateID) echo '</strong>'; ?> </a> </li> <?php endforeach; ?> </ul> <!-- END OF COPY-PASTED CODE--> <!-- --> <!-- --> </li> <?php endforeach; ?> </ul> </li> <?php endforeach; ?> </ul> <?php endif; ?> </li> </ul> <?php endforeach; ?> </div> </aside> <aside class="saleProductsWrapper container p-0"> <h2 class="title">Promocje</h2> <div class="row"> <?php $cate = get_queried_object(); $cateID = $cate->term_id; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $loop = new WP_Query(array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => '1', 'meta_query' => array( array( 'key' => '_sale_price', 'value' => 0, 'compare' => '>', 'type' => 'numeric' ) ), 'orderby' => 'rand' )); while ($loop->have_posts()) : $loop->the_post(); ?> <div class="col-lg-12"> <?php get_template_part('template-parts/product'); ?> </div> <?php endwhile; wp_reset_query(); ?> </div> </aside>
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); ?> 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 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? 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 } ?> 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 there, this is a really noob question, but I've no idea on how to make it work without making everything messy and crappy. I have one SQL table called categories, and another called subcategories. Inside the subcategories table there's a field named 'parent' that will link it to the correspondent category, so it should look something like this in html: Code: [Select] <ul> <li>Category <ul> <li>Subcategory</li> </ul> </li> </ul> Would you please give this poor guy a hand? =) 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?? ) 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. 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 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 |