Re: [PHP-DB] category structure
- Original Message - From: olinux [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: 29 October 2001 08:16 Subject: [PHP-DB] category structure anyone have any ideas on this I have a table of directory categories id | parentid | category so... 1 | 0 | blah1 top 2 | 1 | blah2 sub 3 | 1 | blah3 sub2 4 | 3 | blah4 sub2-sub I want to hit the DB only once so i select all and drop into array Now i've created array with these like so: $category[$parentid][$id] But now what? trouble is that $msub[2][4] is a sub category of $msub[1][2] so i can't just run thru and print the array - need to manipulate a little. Any ideas? [or even a beeter way to do this?] Olinux, What you are asking is a fairly common requirement, with well-known solutions. In what format do you require the output printed? Do you know what a tree data structure is, and how to 'walk the tree'? =dn -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] category structure
Yes, I know what a tree data structure is - how to walk the tree I'm not sure what you mean. But I've come up with something quite cool. but still not working. I select all the data from the catagories table [id | pid | category] and drop it into the array = $menu = array(); while ($row = mysql_fetch_array($result)) { $id = $row['cat_id']; $category = $row['category']; $pid = $row['pid']; $menu[$pid][$id] = $category; } foreach($menu as $key1 = $value1) { echo Key: $key1; Value: $value1br\n; foreach ($value1 as $key2 = $value2) { echo Key: $key2; Value: $value2br\n; // delete array that has been printed unset($menu[$key1][$key2]); } } = and out comes: Key: 0; Value: Array Key: 1; Value: Automotive Key: 4; Value: Technology Key: 1; Value: Array Key: 2; Value: Repair Shops Key: 3; Value: Glass Replacement etc.. So now what i need is the ability to go thru all of the menu array with index of $menu[0][*] one at a time and then print out all $menu indicies where [*] matches $menu[THIS][] the unset removes a category once it's been printed Any ideas? I don't want to recursively hit the database a bunch of times. Thanks, olinux --- DL Neil [EMAIL PROTECTED] wrote: - Original Message - From: olinux [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: 29 October 2001 08:16 Subject: [PHP-DB] category structure anyone have any ideas on this I have a table of directory categories id | parentid | category so... 1 | 0 | blah1 top 2 | 1 | blah2 sub 3 | 1 | blah3 sub2 4 | 3 | blah4 sub2-sub I want to hit the DB only once so i select all and drop into array Now i've created array with these like so: $category[$parentid][$id] But now what? trouble is that $msub[2][4] is a sub category of $msub[1][2] so i can't just run thru and print the array - need to manipulate a little. Any ideas? [or even a beeter way to do this?] Olinux, What you are asking is a fairly common requirement, with well-known solutions. In what format do you require the output printed? Do you know what a tree data structure is, and how to 'walk the tree'? =dn __ Do You Yahoo!? Make a great connection at Yahoo! Personals. http://personals.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] category structure
Olinux, Yes, I know what a tree data structure is - how to walk the tree I'm not sure what you mean. =Walk the tree means using the logic of the 'pointers' in each row to work out which sub-categories fit within each category... But I've come up with something quite cool. but still not working. =you haven't shown what the output should look like. =having unloaded the entire database into an array (and assuming this is realistic/possible in the production environment), there should be no need to go back to the database at all. =do you have a particular reason for putting the data into an associative array? If you make the first index/dimension into an enumerative array then the pointers in the data will work as array elements. What I mean is, can you turn this schema: id | parentid | category 1 | 0 | blah1 top 2 | 1 | blah2 sub 3 | 1 | blah3 sub2 4 | 3 | blah4 sub2-sub into a table thus: $menu[1] = 0 | blah1 top $menu[2] = 1 | blah2 sub $menu[3] = 1 | blah3 sub2 $menu[4] = 3 | blah4 sub2-sub Then use a loop to process each row in turn (using, eg index of $i) and when you wish to print/process $menu[3] ($i=3 and thus $menu[$i]) you search down the [parentid] column (loop index, eg $j) looking for ( $menu[ $j ] = $menu[ $i ] ) - NB I'm making an assumption about the id-column data! Ok, regardless, you appear to need an outer 'processing' loop to print each category, and an inner loop to pick up each sub-category (if any exist). =I'm feeling as if I've made a number of assumptions because I lack the data you know. =Hope it helps, =dn I select all the data from the catagories table [id | pid | category] and drop it into the array = $menu = array(); while ($row = mysql_fetch_array($result)) { $id = $row['cat_id']; $category = $row['category']; $pid = $row['pid']; $menu[$pid][$id] = $category; } foreach($menu as $key1 = $value1) { echo Key: $key1; Value: $value1br\n; foreach ($value1 as $key2 = $value2) { echo Key: $key2; Value: $value2br\n; // delete array that has been printed unset($menu[$key1][$key2]); } } = and out comes: Key: 0; Value: Array Key: 1; Value: Automotive Key: 4; Value: Technology Key: 1; Value: Array Key: 2; Value: Repair Shops Key: 3; Value: Glass Replacement etc.. So now what i need is the ability to go thru all of the menu array with index of $menu[0][*] one at a time and then print out all $menu indicies where [*] matches $menu[THIS][] the unset removes a category once it's been printed Any ideas? I don't want to recursively hit the database a bunch of times. Thanks, olinux --- DL Neil [EMAIL PROTECTED] wrote: - Original Message - From: olinux [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: 29 October 2001 08:16 Subject: [PHP-DB] category structure anyone have any ideas on this I have a table of directory categories id | parentid | category so... 1 | 0 | blah1 top 2 | 1 | blah2 sub 3 | 1 | blah3 sub2 4 | 3 | blah4 sub2-sub I want to hit the DB only once so i select all and drop into array Now i've created array with these like so: $category[$parentid][$id] But now what? trouble is that $msub[2][4] is a sub category of $msub[1][2] so i can't just run thru and print the array - need to manipulate a little. Any ideas? [or even a beeter way to do this?] Olinux, What you are asking is a fairly common requirement, with well-known solutions. In what format do you require the output printed? Do you know what a tree data structure is, and how to 'walk the tree'? =dn __ Do You Yahoo!? Make a great connection at Yahoo! Personals. http://personals.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP-DB] category structure
anyone have any ideas on this I have a table of directory categories id | parentid | category so... 1 | 0 | blah1 top 2 | 1 | blah2 sub 3 | 1 | blah3 sub2 4 | 3 | blah4 sub2-sub I want to hit the DB only once so i select all and drop into array Now i've created array with these like so: $category[$parentid][$id] But now what? trouble is that $msub[2][4] is a sub category of $msub[1][2] so i can't just run thru and print the array - need to manipulate a little. Any ideas? [or even a beeter way to do this?] thanks olinux __ Do You Yahoo!? Make a great connection at Yahoo! Personals. http://personals.yahoo.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]