Jump to content

Smarty tree menu


feri_soft

Recommended Posts

Hello,

I am searching a way to make a tree menu for categories using php and smarty but as i am not very familiar with smarty syntax i cant do it with the functions i know. So here is what i have done:

 

<ul >
  {section name=cats loop=$cats_list}
       <li><a href="index.php?cat={$cats_list[cats].cid}" tabindex="1">{$cats_list[cats].cname}</a></li>
       {if isset($scats_list) }
       {section name=scats loop=$scats_list}
       <li><a href="index.php?cat={$scats_list[scats].scid}" tabindex="1">{$scats_list[scats].scname}</a></li>
       {/section}{/if}
  {/section}
  </ul>

 

And as of the php side:

 

if(isset($_GET['cat']) && is_numeric($_GET['cat'])){
$cat_id = (int)$_GET['cat'];
$subcats = mysql_query("SELECT * from cats where subof=$cat_id order by place");
$scats_ids = array();
$scats_names = array();
while($row1 = mysql_fetch_array($subcats)){
   $scats_ids[] = $row1['id'];
   $scats_names[] = $row1['name'];
   $scats_list[] = array(
	"scname" => $row1["name"],
	"scid" => $row1["id"],
);
}
$tuts -> smarty -> assign("scats_ids", $scats_ids);
$tuts -> smarty -> assign("scats_names", $scats_names);
$tuts -> smarty -> assign("scats_list", $scats_list);
}


$cats = mysql_query("SELECT * from cats where subof=0 order by place");
$cats_ids = array();
$cats_names = array();
while($row = mysql_fetch_array($cats)){
   $cats_ids[] = $row['id'];
   $cats_names[] = $row['name'];
   $cats_list[] = array(
	"cname" => $row["name"],
	"cid" => $row["id"],
);
}
$tuts -> smarty -> assign("cats_ids", $cats_ids);
$tuts -> smarty -> assign("cats_names", $cats_names);
$tuts -> smarty -> assign("cats_list", $cats_list);

 

So all is working great but instead of getting the subcats under the desired category when set in the query string i get the subcats under all the categories. So please tell me some way to do this using php and smarty. I am looking for something like the categories on pixel2life excluding the ajax stuff just the basic function. Its not needed to be done with lists i can use divs as well but i want help on the smarty functions needed for doing this. Thanks all!

Link to comment
https://forums.phpfreaks.com/topic/45335-smarty-tree-menu/
Share on other sites

Uhm.. wow. What you might want to do is rethink the data structure before you even get to smarty. With your current structure I think the only way to do this will loop through scat_names many times for each loop through scats_list. Which ends up being a lot of looping.

 

Try to set it up so you have the following as your data sctructure before even getting into smarty.

 

$scats_list = array(
     'cid' => 23
     , 'cname' => 'myname'
     , 'cats_names' => array ('name1', 'name2', 'name3')
);

Link to comment
https://forums.phpfreaks.com/topic/45335-smarty-tree-menu/#findComment-220109
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.