与孩子的父母子女建立动态导航

时间:2014-06-17 16:55:01

标签: php mysql navigation

建立网站导航时遇到问题。

这是我想要生成的结构:

<nav id="navig">
    <button id="menu-turner" onclick="return false;">Menu</button>
        <ul>
            <li><a href="/">Home</a></li>
            <li class="dropable"><a href="/samples/index.html">Samples</a><!--href="/index.html"-->
                <ul>
                    <li><a href="/samples/samples1.html">Samples1</a></li>
                    <li><a href="/samples/samples2.html">Samples2</a></li>
                    <li class="dropable"><a href="/samples/samples3.html">Samples3</a><!--href="/quartz-samples.html"-->
                    <ul>
                        <li><a href="/samples/samples3.1.html">Samples3.1</a></li>
                        <li><a href="/samples/samples3.2.html">Samples3.2</a></li>
                        <li><a href="/samples/samples3.3.html">Samples3.3</a></li>
                    </ul>
                    </li>
                </ul>
            </li>
            <li><a href="/quote/">Price</a></li>
            <li><a href="/recent-projects.html">Projects</a></li>
            <li><a href="/contact.html">Contact</a></li>
        </ul>
</nav>

pages包含有关页面的所有信息,其中top_nav_ID =表top_nav上的ID,其中包含有关类别的信息。

以下是pages的结构:

id | top_nav_id |parentpageID |menu_dropable|nav_anchor |link_show|pageURL
_____________________________________________________________________________________
1  | 1          | 0           | 0           |Home       |1        |index.html
2  | 2          | 2           | 1           |Samples    |1        |index.html
3  | 2          | 2           | 0           |Samples1   |1        |samples1.html
4  | 2          | 2           | 0           |Samples2   |1        |samples2.html
5  | 2          | 2           | 1           |Samples3   |1        |samples3.html
6  | 2          | 2           | 0           |Samples3.1 |1        |samples3.1.html
7  | 2          | 2           | 0           |Samples3.2 |1        |samples3.2.html
8  | 2          | 2           | 0           |Samples3.3 |1        |samples3.3.html
9  | 0          | 0           | 0           |Projects   |1        |recent-projects.html
10 | 0          | 0           | 0           |Contact    |1        |contact.html
11 | 0          | 0           | 0           |           |0        |search.html
12 | 3          | 0           | 0           |Price      |1        |price.html

top_nav

ID | link_lbl | link_dir | link_url     | link_title | link_show
_____________________________________________________________________
1  | Home     |          |  index.html  | Home       | 1
2  | Samples  | samples  |  index.html  | Samples    | 1
3  | Price    | quote    |  index.html  | Price      | 1

这是我的PHP:

    <?php
print<<<END
        <nav id="navig">
            <button id="menu-turner" onclick="return false;">Menu</button>
                <ul>\n
END;

    $query = 'SELECT * FROM top_nav WHERE link_show = 1' ;
    $resp = $db->prepare($query);
    $resp->execute();
    while ($page_info = $resp->fetch()){
        $category_pageID = $page_info['ID'];
        $category_directory = $page_info['link_dir'];

        $pages = 'SELECT * FROM pages WHERE top_nav_ID = :top_nav_ID AND parentpageID = 0 AND link_show = 1 ORDER BY id asc';
        $res = $db->prepare($pages);
        $res->execute(array(":top_nav_ID" => $category_pageID));
        while ($info = $res -> fetch()){
            if($category_pageID = $info['top_nav_ID']){
            $calss_dropable = ($info['menu_dropable'] == 0)? '' : ' class="dropable"' ;

                $query = 'SELECT * FROM pages WHERE top_nav_ID = :top_nav_ID AND parentpageID > 0 AND link_show = 1 ORDER BY id asc';
                $res = $db->prepare($query);
                $res->execute(array(":top_nav_ID" => $category_pageID));
                while ($infor = $res -> fetch()){
                    $subpageURL = Config::DS.$category_directory.Config::DS.$infor['pageURL'] ;
                    $readysubURL = '<li><a href="'.$subpageURL.'">'.$infor['nav_anchor'].'</a></li>'."\n";
                }
                $list_tags = ($info['menu_dropable'] == 0)? '' :  "\n".'<ul>'."\n".$readysubURL.'</ul>'."\n" ;
                $pageURL = ($category_directory == NULL)? Config::DS.$info['pageURL'] : Config::DS.$category_directory.Config::DS.$info['pageURL'] ;


        $readyURL = '<li'.$calss_dropable.'><a href="'.$pageURL.'">'.$info['nav_anchor'].'</a>'.$list_tags.'</li>';
            }
        echo $readyURL."\n";
        }
    }


$pages = 'SELECT * FROM pages WHERE link_show = 1 ORDER BY id asc';
$res = $db->prepare($pages);
$res->execute();

while ($info = $res -> fetch()){

    $top_nav_ID = $info['top_nav_ID'];

    if($top_nav_ID == 0){
        $pageURL = Config::DS.$info['pageURL'];
        $calss_dropable = ($info['menu_dropable'] == 0)? '' : ' class="dropable"' ;
        $list_tags = ($info['menu_dropable'] == 0)? '' :  '<ul>  </ul>' ;
        $readyURL = '<li'.$calss_dropable.'><a href="'.$pageURL.'">'.$info['nav_anchor'].'</a>'.$list_tags.'</li>';

        echo $readyURL."\n";
    }
}

print<<<END
                </ul>
        </nav>\n
END;

我已经成功了,我可以创建所需的结构,并且首先<li><a href="/samples/samples1.html">Samples1</a></li>,但仍然无法完成剩下的工作。请帮帮我

0 个答案:

没有答案