PHP - Unlimited / Infinite Levels (depths) Of Categories With Php And Mysql
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. Similar TutorialsHow 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? 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, 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. Hey everyone ... This should be a pretty easy question for someone who is used to using fgetcsv: I have a script that needs to import a csv file into a mysql table ... I keep getting stuck in a loop that only ends when the script hits the 30 second timeout I have set on the server ... It never actually inserts a record, and it never spits out a message indicating and error ... Can someone help figure out why I get caught in this loop? Here's the code: Code: [Select] function import_csv() { if(isset($_POST['submit_csv'])) { $filename=$_POST['filename']; $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import="INSERT INTO `{mydatabase}`.`{my table}` (`name`, `qrl`, `begin`) VALUES ('$data[0]','$data[1]','$data[2]')"; mysql_query($import) or die(mysql_error()); } fclose($handle); print "Import done"; } else { print "<form action='results.php' method='post'><input type='file' name='filename'><br /><input type='submit' value='submit' name='submit_csv' /></form>"; } } I am working on this page concept of pedigree. How can I foreach or create a loop that can get 6 levels of parents, for example name= 1 ; nfather=2; nmohter = 3 and the loop continue to get or consider nfather as a name = 2 and get his parents. this is my table any help plees
CREATE TABLE horsetest01 (
CREATE INDEX horsetest01_index02
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! 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! This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=327455.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 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. ok im trying to create a admin panel but i want to be able to set what level of power they have how would i start i know that i have to use a enum Before anyone flames me, I want to be clear, I have searched and researched this issue for years. Maybe its my keywords or maybe its just my understanding, but nothing I have found seems satisfactory or even addresses my specific need. Anyway, my gratitude in advance for everyone being polite and patient. I have a website that keeps track of pricing data for an online gaming site. When I made it I had no understanding of coding beyond BASIC on a Color Computer II from 1981. So I put together everything bit by bit. Now its three years later and I have a much much greater understanding of things. But I'm stuck with a data base that contains 365 columns just for a year of pricing data on an item. And I want to keep all data for every new day, not just one year. I know there has got to be a better way then name,bla,bla,day1,day2,day3,day4,day5... and adding columns for every new day. Any suggestions? And I know this question is mostly focused on MySQL (actually any database but I use MySql) rather than PHP. But PHP will be playing an integral part of maintaining the database. Plus, the people here are always incredibly knowledgeable, and polite. Again, the specific question is how should I design a database that will keep an on-going history of pricing data? my webhost told me there was a infinite loop that in a matter of 30seconds used up over 12GB can someone help me find this and stop the loop for doing so? or is there a program that could help me with this kind of stuff <?php include "sessionStore.php"; $userfinal = get_username($_SESSION['user_id']); $the_time = time(); mysql_query("UPDATE users SET date='$the_time' WHERE Username='$userfinal'")or die(mysql_error()); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Zhshero Friends</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="default.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1><font color=white>ZhsHero Friends</h1> <h2>Making the world a better place :)</h2> </div> <div id="menu"> <ul> <li><a href="http://zhshero.freehostingcloud.com/home.php">Home</a></li> <li class="active"><a href="http://zhshero.freehostingcloud.com/mymembers.php">Members</a></li> <li><a href="http://zhshero.freehostingcloud.com/update_profile.php">Settings</a></li> <li><a href="http://zhshero.freehostingcloud.com/inbox/">Inbox <? $sql = mysql_query("SELECT count(message_id) FROM messages WHERE to_user='$userfinal' AND message_read='0'"); $total = mysql_fetch_array($sql); $num = $total[0]; echo $num; ?> </a></li> <li><a href="http://zhshero.freehostingcloud.com/logout.php">Logout</a></li> </ul> </div> <div id="content"> <div id="colOne"> <center><h2><font color=white><?php echo 'Welcome ' . get_username ( $_SESSION['user_id'] ) . ' '; ?></h2> <p><img src="<? echo get_main_P ( $_SESSION['user_id'] ) ?>" border=1 width="135" height="150"></p> </center> <h2><font color=white>News box</h2> <p><? $result = mysql_query("SELECT * FROM otherapia"); while($row = mysql_fetch_array($result)) { echo $row['id'] . " " . $row['news']; } ?></p> </div> <div id="colTwo"> <h2><font color=white> ZhsHero Friends Users - <a href="http://zhshero.freehostingcloud.com/who_online.php">View online users only</a></h2> <p> <center><table border=2 width="250" height="125"><tr> <? $Members = mysql_query("SELECT * FROM users") or die(mysql_error()); $numRowsMembers = mysql_num_rows($Members); ?> <?php for($count = 4/n; $count <= $numRowsMembers; $count++) { $name = mysql_fetch_array($Members); ?> <td width="150" height="125"> <a href="view_profile.php?username=<? echo $name['Username']?>"><img src="<? echo $name['main_P']?>" width="100" height="100"/> <? echo $name['Username']?></a> <? if (isset($name['date']) && (time() - $name['date'] > 300)) { echo 'offline'; } else { echo "<font color=green>[Online]</font>"; } $name['date'] = time(); // update last activity time stamp ?> </td> <? } ?> </tr></table></center> </p> </div> <div style="clear: both;"> </div> </div> <div id="footer"> <p>Copyright © 2010 ZhsHero LTD. Designed by <a href="http://www.freecsstemplates.org/"><strong>Free CSS Templates</strong></a></p> </div> <center><a href="http://www.adleaf.com" style="font-size:12px;">Free Advertising</a><br/><script type="text/javascript"> riv_client = 318310; riv_backgroundColor = '000000'; riv_borderColor = '000066'; riv_headlineColor = '669900'; riv_textColor = 'FFFFFF'; riv_linkColor = 'CC3300'; riv_adWidth = 728; riv_adHeight = 90; riv_adType = 4; </script> <script type="text/javascript" src="http://ad1.adleaf.com/js/rivad.js"></script> </body> </html> Happy Monday All! I am writing some code to try to pull in some XML from the TVA on lake levels. This file is updated about every 3 to 4 hours. I have the startings to pull the data in, but running into some problems. Here is a break down of the problems. I am fairly new to PHP, but trying to learn as I go. Any help would be greatly appreciated. The code I have will not pull in the data and display One I am able to display the code, I need to review the upsteam elevation and compare to the previous levels and show the lake rising, falling, or stable. Here is my code: Code: [Select] <?php $html = file_get_contents('http://www.tva.gov/lakes/xml/OHH_R.xml'); $xml = simplexml_load_file('$html'); echo $html->getName() . "<br />"; foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; } ?> I am fairly new to PHP, but trying to learn as I go. Any help would be greatly appreciated. i know how to get my current directory, and the root path, but what i'm needing to do is find the levels of the current dir, for instance, if i'm in: domain.com/folder1 or if i'm in domain.com/folder1/folder2 or if i'm in domain.com/folder1/folder2/folder3 can someone help? thanks much GN I just added a user log on for a site I am building using PHP. I used a simple session based system for this. I can log on just fine. What I am looking for is a way to pass on in ht session what rank a user is so some links/pages will only show for the admin or the super user but not the normal user. I rank the users as Admin 1, superuser 2, and user 3. This is what I have for the session in my header that allows you to be logged in. Code: [Select] <? session_start(); if(!session_is_registered(myusername)){ header("location:login.php"); } ?> Here is what I have that starts the session Code: [Select] //Connection stuff $query="SELECT * FROM user WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($query); $count=mysql_num_rows($result); if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:index.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> I have two while loops that should run infinitely. while loop #1 runs during 9:30am - 4pm, monday through thursday while loop #2 runs when its not 9:30am-4pm, monday through thursday. $hourmin = date("Gi", time(now)); $current = getdate(); while ( $hourmin > 829 && $current[hours] < 15 && $current[wday] > 0 && $current[wday] < 6 ): // while loop #1 while ( $hourmin < 830 || $current[hours] > 14 || $current[wday] = 0 || $current[wday] = 6 ): // while loop #2 as you can see the while condition is the easy part, but I'm not sure how to set this up. I was going to do this using a combination of if statements and the goto function but my version of php does not support goto. I just want it to switch back and forth between these these two while statements infinitely. Is this possible in PHP? Hi all, I am working on a project where I need to monitor accounts and suspend features associated with them if their deposited balance runs out. To do this I'm thinking it might be best to have a script running with an infinite loop that constantly refreshes but I'm not sure what kind of strain this might put on the server. The pseudocode would look something like this: while (1) { For each row in database { If balance <= 0 { mysql query - account_status = suspended } } } Please note (obviously) this is psuedocode and not actual code. I'm just concerned about how I would stop this script timing out, and what kind of impact it would have on server performance? Is there any better way to do this? Happy to discuss, dB |