Datamapper Nested Menu - El Forum - 08-31-2011
[eluser]Unknown[/eluser]
Hi guys, I am learning datamapper and I love it but I am having some issues.
I think I am not doing this on the right way, I would like to know the best way to do this, so please if you can....please help me.
I have this table :
Code: CREATE TABLE IF NOT EXISTS `menuoptions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`link` varchar(20) NOT NULL,
`order_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
And this records:
Code: INSERT INTO `menuoptions` (`id`, `parent_id`, `name`, `link`, `order_id`) VALUES
(1, 0, 'For Facilities', '', 0),
(2, 0, 'Field Maintenance', '', 0),
(3, 0, 'Game-Practice Equipment', '', 0),
(4, 0, 'Training & Conditioning', '', 0),
(5, 0, 'Books-Videos', '', 0),
(6, 1, 'Artificial Turf', '', 0),
(7, 1, 'Batting Cages', '', 0),
(8, 7, 'Indoor', '', 0),
(9, 7, 'Outdoor', '', 0),
(10, 3, '-View All-', '', 2),
(11, 3, 'Soccer', '', 1);
This is my model:
Code: class Menuoption extends DataMapper {
var $has_one = array('menuoption');
var $has_many = array('menuoption');
/**
* Constructor: calls parent constructor
*/
function __construct($id = NULL)
{
parent::__construct($id);
}
}
And this is my code:
Code: $menu=new Menuoption();
$menu->order_by('order_id')->get_where(array('parent_id'=>0));
$menu->check_last_query();
$str="";
foreach($menu as $m){
$str.= '<li><aa href="#">'.$m->name.'</a>';
$s=$menu->order_by('order_id')->get_where(array('parent_id'=>$m->id));
if($s->result_count()>0){
$str.= '<ul class="sub_menu">';
foreach($s as $s2){
$str.='<li><aa href="#">'.$s2->name.'</a>';
$d=$menu->order_by('order_id')->get_where(array('parent_id'=>$s2->id));
if($d->result_count()>0){
$str.='<ul>';
foreach($d as $d2){
$str.= '<li><a href="#">'.$d2->name.'</a></li>';
}
$str.= '</ul>';
}//endif
$str.="</li>";
}
$str.= '</ul>';
}//endif
$str.="</li>";
}
I know I am doing something bad, I've tried different ways without luck 
Thank you for your time.
|