PHP - Moved: Displaying Categories And Subcategories In Grid (with Mysql)
This topic has been moved to HTML Help.
http://www.phpfreaks.com/forums/index.php?topic=327455.0 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']; } ?> 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 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. Hi guys. I'm trying to build a games site for a friend. Currently, the front-end (HTML/CSS) of the site is done. Now, I want to make a way for him to easily add games to the site. Ideally, I'd like to make a database with the following columns: ID, Name, Category, Link, Thumbnail_Link. So, those would be the ID, name of the game, the category, a link to the game, and a link to the 50x50 thumbnail image respectively. Then, using PHP, I'd like to call the first x number (not sure what it will be yet, let's say 50) and make format it as a grid in the following way: There's the thumbnail image followed by the game name, and they're all a clickable link to the game URL. Is this possible? How would I go about doing this? I've already set up a database for a login module to the site, so each page has already opened a connection to the MySQL database. However, I've only ever done basic PHP for mail forms and am otherwise extremely new to it, and am especially new to MySQL. Could anyone walk me through how to do this or even give me a quick example script to work off of? Thanks, any of your time is greatly appreciated! 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 I am a new guy here and don,t have much knowledge of PHP. My problem is complex but let me explain it in simple lines. I have two different codes , 1) This code takes form entries 'website','caption' and 'name' and stores it into the Table named as 'user_info' into mysql database. This page is saved as index.php 2) This code can add add new category as a root or a child of existing category with update(edit & delete) option.This page is saved as categories.php Both codes are working(except upload function),but what i want to do is to combine the both forms (Items and category) into one page so that a user can add : webite :Its any URL caption: Description of website name: name of the person Add category:choosing existing category Add new category:If not exist add new category Tell me how can i combine both codes so that my item entries display under their categories???? 1) code for items -stores in Table 'user_info' <html> <head> <style type="text/css"> body { margin-left: 10.5%; margin-right: 10%; margin-top: 1%;} </style> <style type="text/css"> .italic { font-style:italic; color: #336666; font-family: Calibri; padding:20px; } .bold { font-weight:bold; } </style> <title>submit Bookmarks</title> </head> <body background="wrapper.png"> <form action="index.php" method="post"> <font face="Calibri">Add Your Links Here: </font> <input name="website" maxlength="100" size="58" value="http://www."> <p><font face="Calibri">Description: </font> <input name="caption" maxlength="100" size="58"> </p> <p><font face="Calibri">Your Name: </font> <input name="name" maxlength="100" size="28"> <input type="submit" name="Submit" value="Submit"> <font size="4"> </font></p> <p><font size="4"> OR </font><font color="#008080" size="2"><b><a href="soon.html"> <span style="text-decoration: none"><font color="#008080" face="Calibri">Upload</font></span></a></b></font>|<i><font size="2" face="Calibri">upload your Bookmarks from your computer. e.g., , .Csv,.txt or .mdb files.</font></i></p> <form action='upload.php' method='post'> <input type="hidden" name="sel_file" /> <font face="Calibri">Choose a file: </font> <input name="uploaded_file" type="file" /> <input type="submit" value="Upload" /> </form> </p> </form> </body></html> <?php $hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost $db_user = "username"; // change to your database password $db_password = "Password"; // change to your database password $database = "database-name"; // provide your database name $db_table = "user_info"; // # STOP HERE #################################################################### # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db); ?> <html> <head> <title>Add Your Bookmarks Here</title> </head> <body> <?php if (isset($_REQUEST['Submit'])) { # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE $sql = "INSERT INTO $db_table(name,website,caption) values ('".mysql_real_escape_string(stripslashes($_REQUEST['name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['website']))."','".mysql_real_escape_string(stripslashes($_REQUEST['caption']))."')"; $query_auto = "INSERT INTO user_info (entry_date, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )"; if($result = mysql_query($sql ,$db)) { echo 'Thank you, your information has been entered into our database<br><br>'; } else { echo "ERROR: ".mysql_error(); } } else { ?> <?php } ?> <?php if(isset($_POST['SUBMIT'])) { $fname = $_FILES['sel_file']['name']; $chk_ext = explode(".",$fname); if(strtolower($chk_ext[1]) == "csv") { $filename = $_FILES['sel_file']['tmp_name']; $handle = fopen($filename, "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $sql = "INSERT into user_info(name,website,caption) values('$data[0]','$data[1]','$data[2]')"; mysql_query($sql) or die(mysql_error()); } fclose($handle); echo "Successfully Imported"; } else { echo "Invalid File"; } } ?> <?php $result = mysql_query("select * from user_info ORDER BY CUR_TIMESTAMP desc"); // Database Query result ?> <?php while ($row = mysql_fetch_array ($result)) { $i++; if ($i & 1) { echo " <div class='color1'> <br/> <span class='bold'>Website:</span> <span class='normalfont' ><a href='" . $row['website'] . "'>" . $row['website'] . "</a></span> <br/> <span class='bold'>Description:</span> <span class='normalfont'>" . $row['caption'] . " </span><span class='italic'>Added by: " . $row['name'] . "</span> <br/> </div>"; } else { echo " <div class='color2'> <br/> <br/> <span class='bold'>Website:</span> <span class='normalfont'><a href='" . $row['website'] . "'>" . $row['website'] . "</a></span> <br/> <span class='bold'>Description:</span> <span class='normalfont'>" . $row['caption'] . "</span><span class='italic'>Added by: " . $row['name'] . "</span> <br/> <br/> </div>"; } } ?> </body> 2) Code for Adding Categories <?php error_reporting(E_ALL); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Categories Class</title> <style> .code_div{ border:dashed #006600 1px; background:#E8FFEB; font-family:Georgia, "Times New Roman", Times, serif; font-size:11px; width:90%; padding:5px; } </style> </head> <body style="font-family:Tahoma"> <p> <?php // connect to database $hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost $db_user = "username"; // change to your database password $db_password = "password"; // change to your database password $database = "batabase-name"; // provide your database name $db_table = "categories"; // leave this as is # STOP HERE #################################################################### # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db); require_once("categories.class.php"); // Simple Usage if(!isset($_GET["id"])) $_GET["id"] =0; $ctg_id = $_GET["id"]; $categories = new categories; $categories->name_prefix = " "; ?> </p> <p>Here is it a Html Menu:</p> <br> <div class="code_div"> <code><font color="#000000"><font color="#0000bb">$categories </font><font color="#007700">= new </font><font color="#0000bb">categories</font><font color="#007700">; </font></font><br><font color="#000000"><font color="#ff8000"> </font><font color="#0000bb">$output </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">html_output</font><font color="#007700">(</font><font color="#0000bb">$ctg_id</font><font color="#007700">); <br> echo </font><font color="#0000bb">$output</font><font color="#007700">;</font></font></code> </div> <p> <? // lets display the categories $output = $categories->html_output($ctg_id); echo $output; ?> </p> <hr> <p>now I'll modify the $HtmlTree value a bit.</p> <div class="code_div"> <code><font color="#000000"><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">HtmlTree </font><font color="#007700">= array( <br> </font><font color="#dd0000">"header" </font><font color="#007700">=> </font><font color="#dd0000">"<table width=200px border=0 cellpadding=2 cellspacing=2>"</font><font color="#007700">, <br> </font><font color="#dd0000">"BodyUnselected" </font><font color="#007700">=> </font><font color="#dd0000">'<tr><td bgcolor=#C4D9FD >[prefix]&raquo;<a href="?id=[id]"><font color=#53507A>[name]</font></a></td></tr>'</font><font color="#007700">, <br> </font><font color="#dd0000">"BodySelected" </font><font color="#007700">=> </font><font color="#dd0000">'<tr><td bgcolor="#E4DB2C">[prefix]&bull;<a href="?id=[id]"><strong><font color="#000000">[name]</font></strong></a></td></tr>'</font><font color="#007700">, <br> </font><font color="#dd0000">"footer" </font><font color="#007700">=> </font><font color="#dd0000">'</table>'</font><font color="#007700">, <br> ); <br> <br> </font><font color="#0000bb">$output </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">html_output</font><font color="#007700">(</font><font color="#0000bb">$ctg_id</font><font color="#007700">); <br> echo </font><font color="#0000bb">$output</font><font color="#007700">; </font></font></code> </div> <?php $categories->HtmlTree = array( "header" => "<table width=200px border=0 cellpadding=2 cellspacing=2>", "BodyUnselected" => '<tr><td bgcolor=#C4D9FD >[prefix]»<a href="?id=[id]"><font color=#53507A>[name]</font></a></td></tr>', "BodySelected" => '<tr><td bgcolor="#E4DB2C">[prefix]•<a href="?id=[id]"><strong><font color="#000000">[name]</font></strong></a></td></tr>', "footer" => '</table>', ); $output = $categories->html_output($ctg_id); echo $output; ?> <hr> <? // lets get an array of the categories for our works $categories = new categories; $categories_list = $categories->build_list(); // lets do some actions if(!isset($_REQUEST['act'])) $_REQUEST['act'] = ""; $act = $_REQUEST["act"]; switch($act) { case "add": // lets add new category // $categories->add_new( category parent , category name , description , icon path , group ) $categories->add_new($_POST['parent'] , $_POST["name"] , $_POST["desc"] , $_POST["icon"] ); echo '<script>alert("Category was inserted successfully into database"); location="class_categories_test.php"; </script>'; break; case "delete": $categories->delete($_GET["id"]); echo '<script>alert("Category and Sub-Categories was successfully deleted"); location="class_categories_test.php"; </script>'; break; case "_update": $cat = $categories->fetch($_GET["id"]); ?> <form name="form1" method="post" action=""> <p> the form below will execute the following. <br> </p> <div class="code_div"> <code><font color="#000000"><font color="#007700"> </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">update</font><font color="#007700">(</font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"id"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"parent"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"name"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"desc"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"icon"</font><font color="#007700">] );</font></font></code> </div> <br> <table width="90%" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td colspan="3"><div align="center">Edit Category </div></td> </tr> <tr> <td width="13%">Child Of : </td> <td width="1%">:</td> <td width="86%"> <select name="parent" id="parent"> <option value="0" selected>ROOT</option> <? foreach($categories_list as $c) { if($cat["id"] == $c["id"]) continue; // we don't list the category to be child of itself !! ?> <option value="<?=$c["id"]?>" <? if($c["id"] == $cat["parent"]) echo "selected"; ?> ><?=$c["prefix"]?>»<?=$c["c_name"]?></option> <? } ?> </select> </td> </tr> <tr> <td>Name:</td> <td>:</td> <td><input name="name" type="text" id="name" value="<?=$cat["c_name"]?>" size="20"></td> </tr> <tr> <td>Description:</td> <td>:</td> <td><textarea name="desc" cols="40" rows="3" id="desc"><?=$cat["c_desc"]?> </textarea></td> </tr> <tr> <td>Icon:</td> <td>:</td> <td><input name="icon" type="text" id="icon" value="<?=$cat["c_icon"]?>" size="30"></td> </tr> <tr> <td colspan="3"><div align="right"> <input name="act" type="hidden" value="update"> <input name="id" type="hidden" value="<?=$ctg_id?>"> <input type="submit" name="Submit" value="Save"> </div></td> </tr> </table> </form> <? die(); break; case "update": $categories->update($_POST["id"] , $_POST["parent"] , $_POST["name"] , $_POST["desc"] , $_POST["icon"] ); echo '<script>alert("Category was updated successfully!"); location="class_categories_test.php"; </script>'; break; } ?> <p align="center"> </p> <p align="center"><strong>You may list all your categories expanded like this</strong></p> <div class="code_div"> <code><font color="#000000"><font color="#0000bb">$categories </font><font color="#007700">= new </font><font color="#0000bb">categories</font><font color="#007700">; <br> </font><font color="#0000bb">$categories_list </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">build_list</font><font color="#007700">(0);</font></font></code> <br>// $categories_list is now an array and you should use foreach command to print something like the following list. </div> <br> <table width="90%" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td bgcolor="#4180BE"><font color="#FFFFFF"><strong> Categories List: </strong></font></td> </tr> <? foreach($categories_list as $c) { ?> <tr> <td> <?=$c["prefix"]?>»<?=$c["c_name"]?> - [<a href="?act=_update&id=<?=$c["id"]?>">Edit</a> - <a href="?act=delete&id=<?=$c["id"]?>">Delete</a>] </td> </tr> <? } ?> </table> <p align="center"> </p> <p align="center"><strong>Or you may also use it in a combo box</strong></p> <table width="0" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td width="72"><select name="category_id" id="category_id"> <? foreach($categories_list as $c) { ?> <option value="<?=$c["id"]?>"><?=$c["prefix"]?>»<?=$c["c_name"]?> </option> <? } ?> </select></td> <td width="0"><input type="button" name="Button" value="Edit" onClick="location='class_categories_test.php?act=_update&id='+document.getElementById('category_id').value;"></td> <td width="0"><input type="button" name="Button" value="Delete" onClick="location='class_categories_test.php?act=delete&id='+document.getElementById('category_id').value;"></td> </tr> </table> <p> </p> <p><hr></p> <form name="form1" method="post" action=""> the form below will execute the following. <br> <div class="code_div"> <code><font color="#000000"><font color="#ff8000"> </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">add_new</font><font color="#007700">(</font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">'parent'</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"name"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"desc"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"icon"</font><font color="#007700">] ); </font></font></code></div><br> <table width="400" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td colspan="3" bgcolor="#4180BE"><div align="center"><strong><font color="#FFFFFF">Add New Category </font></strong></div></td> </tr> <tr> <td width="13%">Child Of : </td> <td width="1%">:</td> <td width="86%"><select name="parent" id="parent"> <option value="0">ROOT</option> <? foreach($categories_list as $c) { ?> <option value="<?=$c["id"]?>" > <?=$c["prefix"]?>»<?=$c["c_name"]?> </option> <? } ?> </select></td> </tr> <tr> <td>Name:</td> <td>:</td> <td><input name="name" type="text" id="name" size="20"></td> </tr> <tr> <td>Description:</td> <td>:</td> <td><textarea name="desc" cols="40" rows="3" id="desc"></textarea></td> </tr> <tr> <td>Icon:</td> <td>:</td> <td><input name="icon" type="text" id="icon" value="http://" size="30"></td> </tr> <tr> <td colspan="3"><div align="right"> <input name="act" type="hidden" value="add"> <input type="submit" name="Submit" value="Add"> </div></td> </tr> </table> </form> <p align="center"> </p> </body> </html> Sorry for making this long post,hope u won,t mind :-) and will solve my problem This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308347.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=307157.0 Hey folks! I am looking to build a small adventure game in PHP and the key to this will be a 3x3 (or possibly bigger) grids of squares, with the user situated in the centre. I therefore need to be able to figure out the co-ords of the surrounding squares. Presumably I will need to define the size of the master grid, then using the co-ords of the current square (defined from a database containing the users location), calculate the co-ords for the surrounding 8 grid squares. Has anyone had any experience with this? Where should I start? (Maths isn't my strong point tbh, but I can pick things up quickly) Thanks in advance! EDIT - in case it wasn't too clear, the "world map" would be something like 1000x1000 squares, but the player can only see the 3x3 grid at there location. I don't want to use AI, or a primary key to sort the data, as it could create some problems if the items are visited out of order, but I want to sort it always based upon a single element for speed.
I'm creating a table that will be populated (built), when it's corresponding entry doesn't exist, how do I get it sorted out if someone visits 2, then 7 before they visit 5 to read what's there? It's not a 2d number line, but a 3d number grid.
Edited by Q695, 09 September 2014 - 11:30 PM. I am working on a csv file import to mysql, which will include items in one table (irrelevant here), and a hierarchical table of categories (id | name | parent). I would think the simplest method to get each category name is too explode the categories from the csv into an array, for example: $array = "root/Business/Employment"; $categories = explode("/", $array); My problem though, is figuring out what the ID's are for each of the categories without knowing each name will be unique and not knowing for sure the full depth of each category hierarchy either. So (I think) I need to rely on the category structure to define the lowest level ID. So something like this: Code: [Select] SELECT t1.parent AS p1, t1.name AS lev1, t1.id AS cat1, t2.name AS lev2, t2.id AS cat2 FROM (categories AS t1) LEFT JOIN categories AS t2 ON t2.parent = t1.id WHERE t1.parent = '1' AND t2.name = "Employment"; This gives me the ID of Employment, but it's easily possible that the name will be duplicated somewhere else in another heirarchy (in fact I get 4 results from phpmyadmin). And since I don't know all these will be only 3 levels deep, it won't work all the time. Any ideas help would be greatly appeciated! 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 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=344127.0 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. 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 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>
This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=309496.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=352551.0 |