i want to build nav menu from main and subcategory,,i tried more
things and choose tree behavior,,the problem is how to use The
extracted data to build dropdown menu like this

categories/index.ctp

<?php
 // this code for extract main and sub category from tree behavior
echo $html->link("Add Category",array('action'=>'add'));
echo "<ul>";
  foreach($categories as $key=>$value){

  echo "<li>$value &nbsp;</li>";
  }
  echo "</ul>";
?>
     ///  i want to extract tree data like this code
    <ul id="nav" class="dropdown dropdown-horizontal">


            <li><a href="./">Home</a></li>

                    // the main category
            <li><span class="dir">Products</span>
                <ul>
                                   // the sub category
                    <li><a href="./" class="dir">New</a></li>
                    <li><a href="./" class="dir">Used</a></li>
                    <li><a href="./">Featured</a></li>
                    <li><a href="./">Top Rated</a></li>

                </ul>
            </li>


        </ul>
categories_controller.php

<?php
  class CategoriesController extends AppController {
 var $name = 'Categories';
 function index() {
  $categories = $this->Category->generatetreelist(null, null, null,
'&nbsp;&nbsp;&nbsp;');
  // debug ($this->data); die;
  $this->set(compact('categories'));

  }

  function add() {

  if (!empty($this -> data) ) {
  $this->Category->save($this -> data);
  $this->Session->setFlash('A new category has been added');
  $this->redirect(array('action' => 'index'));
  } else {
  $parents[0] = "[ Top ]";
  $categories = $this->Category->generatetreelist(null,null,null," -
");
  if($categories) {
  foreach ($categories as $key=>$value)
  $parents[$key] = $value;
  }
  $this->set(compact('parents'));
  }

  }

  function edit($id=null) {
  if (!empty($this->data)) {
  if($this->Category->save($this->data)==false)
  $this->Session->setFlash('Error saving Category.');
  $this->redirect(array('action'=>'index'));
  } else {
  if($id==null) die("No ID received");
  $this->data = $this->Category->read(null, $id);
  $parents[0] = "[ Top ]";
  $categories = $this->Category->generatetreelist(null,null,null," -
");
  if($categories)
  foreach ($categories as $key=>$value)
  $parents[$key] = $value;
  $this->set(compact('parents'));
  }
  }
 function delete($id=null) {
  if($id==null)
  die("No ID received");
  $this->Category->id=$id;
  if($this->Category->removeFromTree($id,true)==false)
  $this->Session->setFlash('The Category could not be deleted.');
  $this->Session->setFlash('Category has been deleted.');
  $this->redirect(array('action'=>'index'));
  }


}
  ?>

database table

-- Table structure for table `categories`
--

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(10) DEFAULT NULL,
  `lft` int(10) DEFAULT NULL,
  `rght` int(10) DEFAULT NULL,
  `name` varchar(255) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `categories`
--

INSERT INTO `categories` (`id`, `parent_id`, `lft`, `rght`, `name`)
VALUES
(1, NULL, 1, 14, 'Tutorials'),
(2, 1, 2, 7, 'PHP'),
(3, 1, 8, 13, 'MySQL'),
(4, 2, 3, 4, 'CakePHP'),
(5, 2, 5, 6, 'cake'),
(6, 3, 9, 10, 'phpmyadmin'),
(7, 3, 11, 12, 'sql');

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to