开发者

Create a nested list

How would I create a nested list, I currently have this

public function getNav($cat,$subcat){
    //gets all sub categories for a specific category
    if(!$this->checkValue($cat)) return false;  //checks data

    $query = false;
    if($cat=='NULL'){
        $sql = "SELECT itemID, title, parent, url, description, image 
                FROM p_cat 
                WHERE deleted = 0
                AND parent is NULL
                ORDER BY position;";
        $query = $this->db->query($sql) or die($this->db->error);
    }else{
        //die($cat);
        $sql = "SET @parent = (SELECT c.itemID FROM p_cat c WHERE url = '".$this->sql($cat)."' AND deleted = 0);

                SELECT c1.itemID, c1.title, c1.parent, c1.url, c1.description, c1.image,开发者_C百科 (SELECT c2.url FROM p_cat c2 WHERE c2.itemID = c1.parent LIMIT 1) as parentUrl
                FROM p_cat c1
                WHERE c1.deleted = 0
                AND c1.parent = @parent
                ORDER BY c1.position;";
        $query = $this->db->multi_query($sql) or die($this->db->error);
        $this->db->store_result(); $this->db->next_result();
        $query = $this->db->store_result();
    }
    return $query;
}


public function getNav($cat=false, $subcat=false){
        //gets a list of all categories form this level, if $cat is false it returns top level nav

    if($cat==false || strtolower($cat)=='all-products') $cat='NULL';
    $ds = $this->data->getNav($cat, $subcat);
    $nav = $ds ? $ds : false;
    $html = '';

    //create html
    if($nav){
        $html = '<ul>';
        //var_dump($nav->fetch_assoc());
        while($row = $nav->fetch_assoc()){
            $url = isset($row['parentUrl']) ? $row['parentUrl'].'/'.$row['url'] : $row['url'];
            $current = $subcat==$row['url'] ? ' class="current"' : '';
            $html .= '<li'.$current.'><a href="/'.$url.'/">'.$row['title'].'</a></li>';
        }
        $html .='</ul>';
    }
    return $html;
}

The sql returns parents and children, for each parent I need the child to nest in a list.


It's been a while since I've done any PHP but wondering if this will work. Inside your while loop, could you just recurse and add the output of getNav($cat = $row['url']) again in a new <li>, kind of like:

while($row = $nav->fetch_assoc()){
    $url = isset($row['parentUrl']) ? $row['parentUrl'].'/'.$row['url'] : $row['url'];
    $current = $subcat==$row['url'] ? ' class="current"' : '';
    $html .= '<li'.$current.'><a href="/'.$url.'/">'.$row['title'].'</a>';
    /* Add subNav HTML */
    $html .= $this->getNav($row['url']);
    $html .= '</li>';
}

It seems that "getNav" refers to both the function to get the SQL as well as the function that renders the nav HTML - maybe change the HTML getNav function name to getNavHtml just for clarity. Also seems like the $subcat argument may be able to be removed from the SQL getNav function since it is never used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜