SOLVED (Was: Hierarch transversal problem with MySQL)
- From: Lars Eighner <usenet@xxxxxxxxxxxxxxx>
- Date: Mon, 8 Dec 2008 09:51:11 +0000 (UTC)
In our last episode, <slrngjp32o.2b0g.usenet@xxxxxxxxxxxxxxxxxxxxxxxxx>, the
lovely and talented Lars Eighner broadcast on comp.lang.php:
I have this MySQL table:
TABLE: content_path
+--------------+--------+
| node | parent |
+--------------+--------+
| /works | / |
| /EIGHNER | / |
| / | 0 |
| /works/essay | /works |
+--------------+--------+
The table will never be very large, so recursion seems to be the best way to
traverse this hierarchal information. Obviously these are unix-style file
paths, but in researching this, I see the same sort of table often occurs in
product category problems. I also note that such tables don't comply with
database normalization --- which is over my head --- but many people are
attracted by the convenience of this kind of table.
I wrote (stole[1]) this function, which works.
<?php
function tree_slash_children(){
global $line;
$numargs = func_num_args();
if($numargs >= 1){$field = func_get_arg(0);}else{$field = 'node';}
if($numargs >= 2){$table = func_get_arg(1);}else{$table = 'content_path';}
if($numargs >= 3){$parent = func_get_arg(2);}else{$parent = '0';}
if($numargs >= 4){$level = func_get_arg(3);}else{$level = 0;}
$result = mysql_query('SELECT node FROM '.$table.
' WHERE parent="'.$parent.'"');
while ($row = mysql_fetch_array($result)) {
$line = $line . str_repeat('|',$level).$row['node'];
tree_slash_children($field, $table, $row['node'], $level+1);
}
}
?>
Because global $line = '' before the call, the return is:
/|/works||/works/essay|/EIGHNER
Now it occurs to me that the parent field is redundant.
If node = '/' we know there is no parent, and otherwise we know the parent
is node =~ s#/[^/]*$##.
I'd like to dispose of the parent field, but MySQL despite having a replace
function and having regex matching, does not have regex replacement.
I'd like to do something like:
function tree_slash_descend(){
global $line;
$numargs = func_num_args();
if($numargs >= 1){$field = func_get_arg(0);}else{$field = 'node';}
if($numargs >= 2){$table = func_get_arg(1);}else{$table = 'content_path';}
if($numargs >= 3){$parent = func_get_arg(2);}else{$parent = '';} //?
if($numargs >= 4){$level = func_get_arg(3);}else{$level = 0;}
$result = mysql_query('SELECT node FROM '.$table.
' WHERE [****some kind of test on node *****]');
while ($row = mysql_fetch_array($result)) {
$line = $line . str_repeat('|',$level).$row['node'];
tree_slash_children($field, $table, $row['node'], $level+1);
}
}
but I don't see it.
Any suggestions?
[1] based on
<http://www.sitepoint.com/article/hierarchical-data-database/>
Storing Hierarchical Data in a Database [PHP & MySQL Tutorials]
by Gijs Van Tulder
I was thinking of the regex problem backwards. I wanted to match a node to
its parent by stripping the last slash and name characters from the end
using a regular expression replacement function. MySQL doesn't have one.
But it does have a regex matching function, so I could match a parent to its
children because the children would be the node + / + some characters. I add
an ORDER BY to both queries so I could more easily verify that the function
without the parent column (first below) returned exactly the same thing as
the function using the parent column.
function tree_slash_descend(){
global $line;
$numargs = func_num_args();
if($numargs >= 1){$field = func_get_arg(0);}else{$field = 'node';}
if($numargs >= 2){$table = func_get_arg(1);}else{$table = 'content_path';}
if($numargs >= 3){$parent = func_get_arg(2);}else{$parent = '';}
if($numargs >= 4){$level = func_get_arg(3);}else{$level = 0;}
$query = 'SELECT node FROM ' . $table . ' WHERE node REGEXP '
. "'" . $parent . "(/|/[^/]+)\$' ORDER BY node ASC";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
if( $row['node'] == '/' ||
preg_match("#^$parent/[^/]+$#",$row['node']) ){
if ( !($row['node'] == '/') ){
$line = $line . str_repeat('|',$level+1);}
$line = $line . $row['node'];}
tree_slash_descend($field, $table, $row['node'], $level+1);
}
}
function tree_slash_children(){
global $line;
$numargs = func_num_args();
if($numargs >= 1){$field = func_get_arg(0);}else{$field = 'node';}
if($numargs >= 2){$table = func_get_arg(1);}else{$table = 'content_path';}
if($numargs >= 3){$parent = func_get_arg(2);}else{$parent = '0';}
if($numargs >= 4){$level = func_get_arg(3);}else{$level = 0;}
$result = mysql_query('SELECT node FROM '.$table.
' WHERE parent="'.$parent.'" ORDER BY node ASC');
while ($row = mysql_fetch_array($result)) {
$line = $line . str_repeat('|',$level).$row['node'];
// echo str_repeat('|',$level),$row['node'];
tree_slash_children($field, $table, $row['node'], $level+1);
}
}
/*
Article
Home » Server-side Coding » PHP & MySQL Tutorials » Storing
Hierarchical Data in a Database
Highlight On Highlight Off Print Email
Related Articles
1. Creating a Credit Card Validation Class With PHP
Rated : 8.8/10
2. Moving Beyond MySQL - High End Database Solutions
Rated : 8.1/10
About the Author
Gijs Van Tulder
Gijs Van Tulder Gijs is a full time Dutch student in economics and a
spare time Web developer. He spends his time developing scripts using
PHP, MySQL and other external programs. Visit him at
http://gvtulder.f2o.org/
Storing Hierarchical Data in a Database
By Gijs Van Tulder
April 30th 2003
Reader Rating: 9.2
Page: 123Next
Whether you want to build your own forum, publish the messages from a
mailing list on your Website, or write your own cms: there will be a
moment that you'll want to store hierarchical data in a database. And,
unless you're using a XML-like database, tables aren't hierarchical;
they're just a flat list. You'll have to find a way to translate the
hierarchy in a flat file.
Storing trees is a common problem, with multiple solutions. There are
two major approaches: the adjacency list model, and the modified
preorder tree traversal algorithm.
In this article, we'll explore these two methods of saving hierarchical
data. I'll use the tree from a fictional online food store as an
example. This food store organizes its food by category, by colour and
by type. The tree looks like this:
1105_tree
This article contains a number of code examples that show how to save
and retrieve data. Because I use that language myself, and many other
people use or know that language too, I chose to write the examples in
PHP. You can probably easily translate them to your own language of
choice.
The Adjacency List Model
The first, and most elegant, approach we'll try is called the
`adjacency list model' or the `recursion method'. It's an elegant
approach because you'll need just one, simple function to iterate
through your tree. In our food store, the table for an adjacency list
looks like this:
1105_table1
As you can see, in the adjacency list method, you save the `parent' of
each node. We can see that `Pear' is a child of `Green', which is a
child of `Fruit' and so on. The root node, `Food', doesn't have a
parent value. For simplicity, I've used the `title' value to identify
each node. Of course, in a real database, you'd use the numerical id of
each node.
Give Me the Tree
Now that we've inserted our tree in the database, it's time to write a
display function. This function will have to start at the root node --
the node with no parent -- and should then display all children of that
node. For each of these children, the function should retrieve and
display all the child nodes of that child. For these children, the
function should again display all children, and so on.
As you might have noticed, there's a regular pattern in the description
of this function. We can simply write one function, which retrieves the
children of a certain parent node. That function should then start
another instance of itself for each of these children, to display all
their children. This is the recursive mechanism that gives the
`recursion method' its name.
<?php
// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree,
// used to display a nice indented tree
function display_children($parent, $level) {
// retrieve all children of $parent
$result = mysql_query('SELECT title FROM tree '.
'WHERE parent="'.$parent.'";');
// display each child
while ($row = mysql_fetch_array($result)) {
// indent and display the title of this child
echo str_repeat(' ',$level).$row['title']."\n";
// call this function again to display this
// child's children
display_children($row['title'], $level+1);
}
}
?>
To display our whole tree, we'll run the function with an empty string
as $parent and $level = 0: display_children('',0); For our food store
tree, the function returns:
Food
Fruit
Red
Cherry
Yellow
Banana
Meat
Beef
Pork
Note that if you just want to see a subtree, you can tell the function
to start with another node. For example, to display the `Fruit'
subtree, you would run display_children('Fruit',0);
The Path to a Node
With almost the same function, it's possible to look up the path to a
node if you only know the name or id of that node. For instance, the
path to `Cherry' is `Food' > `Fruit' > `Red'. To get this path, our
function will have to start at the deepest level: `Cherry'. It then
looks up the parent of this node and adds this to the path. In our
example, this would be `Red'. If we know that `Red' is the parent of
`Cherry', we can calculate the path to `Cherry' by using the path to
`Red'. And that's given by the function we've just used: by recursively
looking up parents, we'll get the path to any node in the tree.
<?php
// $node is the name of the node we want the path of
function get_path($node) {
// look up the parent of this node
$result = mysql_query('SELECT parent FROM tree '.
'WHERE title="'.$node.'";');
$row = mysql_fetch_array($result);
// save the path in this array
$path = array();
// only continue if this $node isn't the root node
// (that's the node with no parent)
if ($row['parent']!='') {
// the last part of the path to $node, is the name
// of the parent of $node
$path[] = $row['parent'];
// we should add the path to the parent of this node
// to the path
$path = array_merge(get_path($row['parent']), $path);
}
// return the path
return $path;
}
?>
This function now returns the path to a given node. It returns that
path as an array, so to display the path we can use
print_r(get_path('Cherry')); If you do this for 'Cherry', you'll see:
Array
(
[0] => Food
[1] => Fruit
[2] => Red
)
Disadvantages
As we've just seen, this is a great method. It's easy to understand,
and the code we need is simple, too. What then, are the downsides of
the adjacency list model? In most programming languages, it's slow and
inefficient. This is mainly caused by the recursion. We need one
database query for each node in the tree.
As each query takes some time, this makes the function very slow when
dealing with large trees.
The second reason this method isn't that fast, is the programming
language you'll probably use. Unlike languages such as Lisp, most
languages aren't designed for recursive functions. For each node, the
function starts another instance of itself. So, for a tree with four
levels, you'll be running four instances of the function at the same
time. As each function occupies a slice of memory and takes some time
to initiate, recursion is very slow when applied to large trees.
Go to page: 1 2 3 Next
If you liked this article, share the love:
* Save to Del.icio.us
Learn more with easy-to-understand SitePoint books
* The PHP Anthology: Object Oriented PHP Solutions, Volume 2
* Build Your Own Database Driven Website Using PHP & MySQL, 3rd ed.
* The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks
Print-Friendly Version Email to a Friend Give Author Feedback Suggest
an Article Link to this Article
Sponsored Links
Popular Articles
Most Popular This Month
1. Build a phpBB Forum in 5 Steps
2. Advanced email in PHP
3. Storing Hierarchical Data in a Database
4. Build your own Database Driven Website using PHP & MySQL -
Third Edition
5. PHP and XML: Parsing RSS 1.0
More Articles
Guru Lists
So You Want To Be A PHP Guru?
Try this list, compiled by kyberfabrikken
1. Storing Hierarchical Data in a Database
2. Introducing PHP 5's Standard Library
3. PHP on the Command Line - Part 1
4. Apache HTTP Authentication with PHP
5. Advanced email in PHP
More Guru Lists
Topics
Before You Code
* Domain Names
* Hosting
* Site Planning
* Legal Issues
* Privacy and Trust
Design and Layout
* Usability and Information Architecture
* Accessibility
* Design Principles
* Design Practice
* Design Tips & Tricks
* Software Tutorials
* Flash Tutorials
Client-side Coding
* HTML & XHTML Tutorials
* CSS Tutorials
* JavaScript & Ajax Tutorials
* XML, XSLT & Web Services
Server-side Coding
* ASP & .NET Tutorials
* CGI & Perl Tutorials
* ColdFusion Tutorials
* Java and J2EE
* PHP & MySQL News & Interviews
* PHP & MySQL Tutorials
* PHP & MySQL Reviews and Apps
* Ruby & Rails
* Server Side Essentials
* Apache & IIS Configuration
Site Strategy
* Community
* Content
* eCommerce
Site Marketing
* Traffic Analysis
* Search Marketing
* Other Promotions
Sell Your Services
* Get Started
* Find Clients
* Work Smarter
SitePoint Marketplace
Buy, Sell, Trade-[graphic1.png]
Buy and sell Websites, templates, domain names, hosting, graphics and
more.
Tell me more...
SitePoint Jobs
» We're Hiring!
SitePoint Newsletters
Get all the latest tips on Advanced Server-side Coding by signing up to
all of SitePoint's informative newsletters.
* [_] SitePoint TechTimes
* [_] SitePoint Tribune
* [_] SitePoint Design View
* [_] Community Crier
____________________ Subscribe!
(_) HTML (_) Plain
View Our Privacy Policy
Sample Our Newsletter Archives
FREE PDF with any printed book!-[241bubble-2.jpg]
The Art & Science of CSS-[small-cover.png]
The Art &
Science
of CSS
Now FREE!
Download sample chapters of any of our popular books.
* Everything You Know About CSS Is Wrong! Everything You Know About
CSS Is Wrong!
+ Free Sample
+ More Info
+ Order Now
* The Principles of Beautiful Web Design-[books-index.gif] Learn How
to Create Beautiful Web Sites
+ Free Sample
+ More Info
+ Order Now
* Build Your Own Database Driven Website Using PHP &
MySQL-[books-index.gif] Learn PHP the Easy Way!
+ Free Sample
+ More Info
+ Order Now
* The CSS Anthology: 101 Essential Tips, Tricks & Hacks,
2<sup>nd</sup> Edition-[books-index.gif] 101 CSS Tips, Tricks &
Hacks
+ Free Sample
+ More Info
+ Order Now
* Build Your Own Web Site The Right Way Using HTML &
CSS-[books-index.gif] Build Your First Web Site
+ Free Sample
+ More Info
+ Order Now
Learn more with SitePoint books
* Boost Your Web Site's Income
* Learn Search Engine Marketing
* Win Bigger Freelance Clients
* Learn Email Marketing
* Learn Beautiful Website Design
* Learn CSS
* Learn JavaScript
* Learn PHP
* 101 CSS Tips, Tricks & Hacks
* 101 PHP Tips, Ticks & Hacks
* 101 JavaScript Tips, Tricks & Hacks
Related Forum Discussions
Related Forum: PHP
1. Password Recovory Script
2. MySql data from one table to another
3. Autoloading classes from multiple directories
4. Dynamically expanding array?
5. update multiple fields
* Advertise
* About Us
* Contact Us
* Site Map
* Write For Us
* RSS
* Newsletters
* Search
* Privacy
* Glossary
* Terms & Conditions
* Jobs
The contents of this webpage are copyright © 1998-2008 SitePoint Pty.
Ltd. All Rights Reserved.
Web Design & Development by SitePoint, Melbourne, Australia - Logo
Design, Web page Design - 99designs.com
/*
include_once('mysql-db.phpm');
function db_tree(){
global $mysqldb,$text_return;
$numargs = func_num_args();
if($numargs >= 1){$tree = func_get_arg(0);}else{$tree = 'content_path';}
if($numargs >= 2){$usedb = func_get_arg(1);}else{$usedb = $mysqldb;}
$text_return = '';
select_db();
$text_return = $text_return ."<select name='category'>\n";
$selcat="SELECT * from content_path order by content_node ASC";
$selcat2=mysql_query($selcat) or die(mysql_error());
if(mysql_num_rows($selcat2) == zero){ return FALSE;}
traverse('0',0,$selcat2,$text_return);
$text_return = $text_return . "</select><br>\n";
return $text_return;
}
function traverse($root, $depth, $sql, $text_return)
{
global $text_return;
$row=0;
while ($acat = mysql_fetch_array($sql))
{
if ($acat['parent'] == $root)
{
$text_return = $text_return . "<option value='" .
$acat['content_node'] . "'>";
$j=0;
while ($j<$depth)
{
$text_return = $text_return . " " ;
$j++;
}
if($depth>0)
{
$text_return = $text_return . "-";
}
$text_return = $text_return . $acat['content_node'] . "</option>\n";
mysql_data_seek($sql,0);
traverse($acat['content_node'], $depth+1,$sql,$text_return);
}
$row++;
@mysql_data_seek($sql,$row);
}
}
/*
/*
PHP Development Board php divider
User Options
Register--Login--Top 20 Posters--Search Topics
Forum Main>>Tutorials>>Listing categories and sub-categories in tree
format
New Topic-Reply
Author
Post
Chipmunk
[drinkingsquirrel.jpg]
Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
PM [Chipmunk]
View Member Photo
Last replied to on Wed Jul 30, 2008 07:15:19
Edit Post|Quote
__________________________________________________________________
This tutorial will show you the basics of recursion abd how to list
categories and sub-categories to the nth level in tree format in a
directory kind of script. For this example, we will need a SQL table
with the following:
A table called cl_categories with these fields.
1.CatID which is a bigint, primary, and auto-increment
2. CatName, which is varchar(255). This is the name of the category
3. CatParent, a bigint, which is the ID of the Parent category, if this
is a sub-category. If CatParent is 0, then it signifies that the
category is a top-level category.
To display these categories in a drop down box, we first need a select
form:
Code:
<?php
print "<select name='category'>";
$selcat="SELECT * from cl_categories order by CatName ASC";
$selcat2=mysql_query($selcat) or die("Could not select category");
traverse(0,0,$selcat2);
print "</select><br>";
?>
Transverse is the actual function that is going to generate the tree
structure for our categories and sub-categories. We initially pass it
values of 0,0,and $selectcat2 for its
root, depth, and the actual query data. We pass is 0 and 0 for root and
depth because initially we want to start from top level directories.
Now here the actual function:
Code:
<?php
function traverse($root, $depth, $sql)
{
$row=0;
while ($acat = mysql_fetch_array($sql))
{
if ($acat['CatParent'] == $root)
{
print "<option value='" . $acat['CatID'] . "'>";
$j=0;
while ($j<$depth)
{
print " ";
$j++;
}
if($depth>0)
{
print "-";
}
print $acat['CatName'] . "</option>";
mysql_data_seek($sql,0);
traverse($acat['CatID'], $depth+1,$sql);
}
$row++;
mysql_data_seek($sql,$row);
}
}
?>
This is pretty complicated. Basically its going through all the
categories in the while loop and seeing if the current categories is
equal to root. Since initially the root is zero, it just prints the
category without indentations. It also sets a variable, j, to keep
track of how deep in the tree a category is, so it can print the
appropriate number of indentations ( ) segments so the tree will
look right. Also if the depth of the categories is not zero,meaning its
a subcat, it will print a dash indicating its a subcat of a higher
level category. Then the function calls itself and adds one to the
depth only if the Catparent is not zero. This way, it will keep
transversing the nodes of the tree until it goes through all the
categories and subcategories. Calling itself and incrementing the depth
by 1 ensures proper level of the category within the tree. When the
while loop finishes, you will have all the categories displayed in tree
format. The MySQL_data_seek function is there to rebuffer the query so
you can go through it again at the end of each iteration.
-----------------------------
Chipmunk,
Supreme Administrator
__________________________________________________________________
rem
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 11681
[PM rem]
RPS score: 0
RPS challenge
Posted at Thu Feb 01, 2007 21:21:31
Edit post|Quote
__________________________________________________________________
This is a great tutorial but unfortunately it gives an error! Try it
out without using the lists or look at the HTML source code ;)
Do you think you could re-post this without the error? I tried to fix
it but didn't succeeded...
The error is:
Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 6 is
invalid for MySQL result index 4 (or the query data is unbuffered) in
.... etc ... on line 33
-----------------------------
-- Rem
__________________________________________________________________
Chipmunk
[drinkingsquirrel.jpg]
Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]
View Member Photo
Posted at Thu Feb 01, 2007 23:29:17
Edit post|Quote
__________________________________________________________________
It'll give you that error only if you don't already have a populated
list of MYSQL categories.
Since its listing the categories in tree format with mySQL categories,
this tutorial is no really relevant if you don't already have a
populated mySQL table of categories/sub-categories.
Alternativey if you don't want that error to appear when a table is
unpopulated. You could check if mysql_num_rows($sql) is zero or not and
only run mysql_data_seek if its not zero.
Note on the tutorial: I've only gotten it to work as a dropdown list. I
doesn't quite function right if you try to printing it out otherwise.
-----------------------------
Chipmunk,
Supreme Administrator
__________________________________________________________________
ithapiri
Rank:acorn
Group: members
Posts: 9
IP Logged
PM ID and RPS ID: 1380
[PM ithapiri]
Posted at Sat Feb 03, 2007 00:36:43
Edit post|Quote
__________________________________________________________________
see in this code add @ in front of the mysql_data_seek($sql,$row);
so it looks like
@mysql_data_seek($sql,$row);
it supresses the error shows above ....is it correct dear admin:cool
__________________________________________________________________
hugoman
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 11729
[PM hugoman]
RPS score: 0
RPS challenge
Posted at Mon Feb 12, 2007 11:52:00
Edit post|Quote
__________________________________________________________________
Hello, thats my first post... [smile.gif] ,
the script is that what i have searched over the internet, but how to
make it to display the categries in tree, <ul><li>....
Thanks ;)
-----------------------------
Hello boys and girls... nice forum [smile.gif] ))
__________________________________________________________________
Chipmunk
[drinkingsquirrel.jpg]
Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]
View Member Photo
Posted at Mon Feb 12, 2007 23:08:49
Edit post|Quote
__________________________________________________________________
Try just printing the entries instead of having them in a <select>
option box.
-----------------------------
Chipmunk,
Supreme Administrator
__________________________________________________________________
Moogie17
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 11938
[PM Moogie17]
RPS score: 0
RPS challenge
Posted at Wed Mar 14, 2007 17:34:38
Edit post|Quote
__________________________________________________________________
Thanks for this great bit of code, it's almost exactly what I'm looking
for. Don't mean to be cheeky, but can anyone here tell me how I can get
this to just output the contents to a variable instead of printing it
immediately? I've tried various things, but only seem to get one level
returned when I try to do it. No doubt someone here knows rather more
PHP than I and could assist!
Many thanks :D
-----------------------------
Moogie
__________________________________________________________________
Chipmunk
[drinkingsquirrel.jpg]
Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]
View Member Photo
Posted at Wed Mar 14, 2007 19:37:29
Edit post|Quote
__________________________________________________________________
You'd probably have to store it in a array with the following
structure:
$array[nodedepth][node]=value
Thats the only way I could see to successfully store the structure.
-----------------------------
Chipmunk,
Supreme Administrator
__________________________________________________________________
Moogie17
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 11938
[PM Moogie17]
RPS score: 0
RPS challenge
Posted at Thu Mar 15, 2007 14:22:51
Edit post|Quote
__________________________________________________________________
Thanks very much! I'm not very good with arrays, but I'll keep
tinkering and will probably get it working [smile.gif]
All the best!
Moogie
-----------------------------
Moogie
__________________________________________________________________
BilltheCat
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 14279
[PM BilltheCat]
RPS score: 0
RPS challenge
Posted at Mon Nov 26, 2007 02:13:54
Edit post|Quote
__________________________________________________________________
Hi,
I'm trying to take what you have here use it in a shopping cart where I
add the products. The old version doesn't allow more than one
subcategory, and I like the idea that yours allows unlimited.
But the old one does one thing this does not, and that is keeping the
category location when modifying products. If I add a product, then go
back and edit it, I can't tell from this script exactly which category
it was originally - and it saves in root if I don't change it.
Any ideas?
This is where the old script finds the option to maked "selected", how
can I integrate this with yours?
Code:
// build combo box options
$list = '';
foreach ($categories as $key => $value) {
$name = $value['name'];
$children = $value['children'];
$list .= "<optgroup label=\"$name\">";
foreach ($children as $child) {
$list .= "<option value=\"{$child['id']}\"";
if ($child['id'] == $cat_id) {
$list .= " selected";
}
$list .= ">{$child['name']}</option>";
}
$list .= "</optgroup>";
}
__________________________________________________________________
Chipmunk
[drinkingsquirrel.jpg]
Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]
View Member Photo
Posted at Wed Nov 28, 2007 16:34:19
Edit post|Quote
__________________________________________________________________
You have to select the product you are trying to modify, get its
category, and insert it as the default option. On the modify file,
there should be an ID or something that identifies the product you are
trying to modify.
-----------------------------
Chipmunk,
Supreme Administrator
__________________________________________________________________
al3loo
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 16847
[PM al3loo]
RPS score: 0
RPS challenge
Posted at Wed Jul 30, 2008 07:15:19
Edit post|Quote
__________________________________________________________________
Hi Everyone,
I like this code and this site .. but I want to ask how could I make it
select the option direct. i.e: If I in the admin and want to edit cat
so its should select the current category before I edit it, I think its
an (if condition) but I don't know how to use it !
((selected="selected"))
Also, I am asking If somebody could make a code to show the cat and
subcat like this:
Homepage >> category >> subcat ..
Thanks in advanced.
Regards,
__________________________________________________________________
Page: 1
Powered by Chipmunk Board
Flash games Ninja games-Web Design New York
*/
?>
--
Lars Eighner <http://larseighner.com/> usenet@xxxxxxxxxxxxxxx
Honesty is the best policy, but insanity is a better defense.
.
- References:
- Hierarch transversal problem with MySQL
- From: Lars Eighner
- Hierarch transversal problem with MySQL
- Prev by Date: Re: Solving undefined errors
- Next by Date: SOLVED (Was: Hierarch transversal problem with MySQL)
- Previous by thread: Re: Hierarch transversal problem with MySQL
- Next by thread: SOLVED (Was: Hierarch transversal problem with MySQL)
- Index(es):
Relevant Pages
|