We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

retrieving hierarchial data from xml and saving to database nested set model

phparmy
phparmy asked
on
Medium Priority
955 Views
Last Modified: 2013-12-12
hello

i am using nested set model for saving hierarchial data  to mysql.
how is nested set model you are creating table and saving left and right number for each item. Sample one in attachment.

CREATE TABLE nested_category (
 category_id INT AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(20) NOT NULL,
 lft INT NOT NULL,
 rgt INT NOT NULL
);

INSERT INTO nested_category
VALUES(1,'ELECTRONICS',1,20),(2,'TELEVISIONS',2,9),(3,'TUBE',3,4),
(4,'LCD',5,6),(5,'PLASMA',7,8),(6,'PORTABLE ELECTRONICS',10,19),
(7,'MP3 PLAYERS',11,14),(8,'FLASH',12,13),
(9,'CD PLAYERS',15,16),(10,'2 WAY RADIOS',17,18);

i have a xml file

$xml = <<<XML
<category_list>
<category id="1">
       <title>Category 1</title>
       <category id="2">
             <title>Category 1.1</title>
       </category>
       <category id="3">
              <title>Category 1.2</title>
              <category id="4">
                  <title>Category 1.2.1</title>
              </category>
       </category>
       <category id="5">
                   <title>Category 1.3</title>
       </category>
</category>
<category id="6">
      <title>Category 2</title>
</category>
<category id="7">
      <title>Category 3</title>
</category>
</category_list>
XML;

i want to parse it and save to database method which i explained above. (SimpleXML or DOM method for parsing)

Thanks for your helps
hierarchical-data-3.png
hierarchical-data-4.png
Comment
Watch Question

Top Expert 2008
Commented:
Try this:
$counter = 1;
function set_left_right($node) {
  global $counter;
  $node->left = $counter++;
  foreach($node->category as $cat) 
    set_left_right($cat); # recursive
  $node->right = $counter++;
}
function write_nodes($node) {
  echo "insert into nested_category set 
    category_id={$node['id']}, 
    name='{$node->title}'
    lft=$node->left, 
    rgt=$node->right" . '<br />';
  $parent = $node['id'];
  foreach($node->category as $cat) 
    write_nodes($cat,$parent); # recursive
}
$p = new SimpleXMLElement($xml);
foreach($p->category as $node)  
  set_left_right($node);
foreach($p->category as $node)  
  write_nodes($node);

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2008

Commented:
You can delete line 15 and remove $parent parameter in line 17.

Author

Commented:
Thank you. It helped me very much.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.