[eluser]MEM[/eluser]
Again, the point is to have Parent
and Childs to appear on the same page. When we click on a specific Parent and the URI changes to that ID, all the parents that were on the page AND the childs (if they exist) of that specific parent, should appear:
The
model returns an hierarchical array*:
Code:
public function getSubCategorias($id_cat,$intDepth=2)
{
$strSQL = 'SELECT c.id_cat,c.nome_cat,c.parent_id_cat FROM categoria c';
$query = $this->db->query($strSQL);
$arrQuery = $query->result_array();
return $this->_parseResultTree($id_cat,$arrQuery,$intDepth, $intRunner=0);
}
private function _parseResultTree($intParentId,&$arrRows,$intDepth,$intRunner)
{
// stop at this depth, portanto, só devolve o array, caso a Depth seja igual ao Runner.
if($intDepth == $intRunner)
{
return array();
}
$arrChildren = array();
for($i=0;$i<count($arrRows);$i++)
{
if($intParentId == $arrRows[$i]['parent_id_cat'])
{
$arrChildren = array_merge($arrChildren,array_splice($arrRows,$i--,1));
}
}
$intChildren = count($arrChildren);
if($intChildren != 0)
{
for($i=0;$i<$intChildren;$i++)
{
$arrChildren[$i]['children'] = $this->_parseResultTree($arrChildren[$i]['id_cat'],$arrRows,$intDepth,$intRunner++);
}
}
return $arrChildren;
}
The controller,grabs the URI and pass that URI to the model and load a template:
Code:
function seccao($id)
{
/*
* Fetch category data from model
*/
$cat = $this->M_Categoria->getCategoria($id);
/*
* Fetch main categories menu. (it's another menu).
*/
$data['menuprincipal'] = $this->M_Categoria->getCategoriasPrincipais();
/*
* Fetch category tree lower level categories - 2 levels
*/
$data['menucategorias'] = $this->M_Categoria->getSubCategorias($cat->id_cat,2);
/*
* load data in template
*/
$data['main'] = 'v_categoria';
$data['titulo'] = "As nossas soluções para ".$cat->nome_cat;
$this->load->vars($data);
$this->load->view('geral_tpl');
}
The view:
Code:
function parse_into_menu($arrMenu,$strChildKey,$strNameKey,$strIdKey,$intRunner=0)
{
$strNL = "\n";
$intMenu = count($arrMenu);
//allow uri->segment inside a function
$CI =& get_instance();
for($i=0;$i<$intMenu;$i++)
{
if($i==0)
{
echo '<ul>',$strNL;
}
//Se o segment is equal to parent
if ($CI->uri->segment(3)==$arrMenu[$i]['parent_id_cat'])
{
// show child:
echo '<li>';
echo anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]),$strNL;
echo '</li>';
}
//if equal to child:
else if ($CI->uri->segment(3) == $arrMenu[$i][$strIdKey])
{
//show child:
echo '<li>';
echo anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]),$strNL;
echo '</li>';
//and childs child: (recursion)
echo '<li>';
parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$strIdKey,$intRunner+1);
echo '</li>';
}
//If not equal to anything:
else
{
//show childs.
echo '<li>';
echo anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]),$strNL;
echo '</li>';
}
if($i==($intMenu-1))
{
echo '</ul>',$strNL;
}
}//fim do ciclo for
}
parse_into_menu($menucategorias,'children','nome_cat','id_cat');
*The structure of the returned array:
Code:
$print_r($menucategorias);
:
Array (
[0] => Array (
[id_cat] => 6
[nome_cat] => Fitofármacos
[parent_id_cat] => 1
[children] => Array (
[0] => Array (
[id_cat] => 9
[nome_cat] => Herbicidas
[parent_id_cat] => 6
[children] => Array ( ) )
[1] => Array (
[id_cat] => 10
[nome_cat] => Insecticidas
[parent_id_cat] => 6
[children] => Array ( ) )
[2] => Array (
[id_cat] => 11
[nome_cat] => Fungicidas
[parent_id_cat] => 6
[children] => Array ( )
)
)
)
[1] => Array (
[id_cat] => 7
[nome_cat] => Adubos
[parent_id_cat] => 1
[children] => Array ( )
)
[2] => Array (
[id_cat] => 8
[nome_cat] => Sementes
[parent_id_cat] => 1
[children] => Array ( )
)
)
The issue must be related only with the view part. When we click on the parent link, i'm unable to show both parent and child, side by side.
Any help ???