猿问

如何将我的递归函数与另一个参数结合起来

我有一个递归运行的脚本,并且能够显示多个级别的子菜单。问题是这样的:目前我在我的 CMS 中使用两个部分。


类别和文章。


文章可以在类别下,类别可以在其他类别(子类别)下,但文章不能在其他文章下。


目前我的脚本只适用于类别,但如果例如在我的 CMS 中article 1并且category1都在category0我想显示它们,而不仅仅是category1.


在我的数据库中,结构是这样的:


`snm_categories` - contains category data

`id` - the id of a category

parent_id - the id of it's parent category, this is `1` if it has no parent



`snm_content` - contains the article data

`catid` - the id of the category it falls under, in my above example this will be the same id as the parent_id of `category1`

这是我的脚本:


<?PHP

// Get all categories and articles

$menu = "

SELECT cat.id as cat_id, cat.level, cat.parent_id, cat.title as cat_title, cat.alias as cat_alias, cat.published, cat.rgt, cnt.state, cnt.id as content_id, cnt.catid, cnt.title as content_title, cnt.alias as content_alias

FROM snm_categories cat

LEFT JOIN snm_content cnt

ON cnt.catid = cat.id

WHERE cat.id NOT IN (1, 2, 3, 4, 5, 7, 8, 22)

AND cat.published = 1

GROUP BY cat.id

ORDER BY cat.rgt ASC";

$menuconn = $conn->query($menu);

// Create new array

$menuData = array(

    'items' => array(),

    'parents' => array()

);

// Create new array with `items` and `parents`, which contain cat_id and parent_id

while($menu = $menuconn->fetch_assoc())

{

    $menuData['items'][$menu['cat_id']] = $menu;

    $menuData['parents'][$menu['parent_id']][] = $menu['cat_id'];

}


// Function to create menu, $parentId is 1 (when categories have no parent and are topcategories)

function buildMenu($parentId, $menuData)

{

    $html = '';

    if (isset($menuData['parents'][$parentId]))

    {

        // If parent_id is 1 put a <li> around it (because it's not a subcat) if not put an <ul> around

        if($parentId == '1'){

          $html = '<li>';

        }else{

          $html = '<ul class="sub-menu">';

        }


阿晨1998
浏览 138回答 2
2回答

郎朗坤

我知道这与您的代码非常不同,但是我几周前针对类似情况编写了此代码,因此我根据您的情况对其进行了一些调整:<?phpecho get_items(1);function get_items($id){&nbsp; &nbsp; global $conn; //db connection object&nbsp; &nbsp; $html = '';&nbsp; &nbsp; $sql = mysqli_query($conn, "SELECT * FROM snm_content WHERE catid = $id");&nbsp; &nbsp; while($row = mysqli_fetch_assoc($sql)){&nbsp; &nbsp; &nbsp; &nbsp; $html .= '<li>'.$row['title'].'</li>';&nbsp; &nbsp; }&nbsp; &nbsp; $sql = mysqli_query($conn, "SELECT * FROM snm_categories WHERE parent_id = $id");&nbsp; &nbsp; while($row = mysqli_fetch_assoc($sql)){&nbsp; &nbsp; &nbsp; &nbsp; $html .= '<li>'.$row['title'].'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>'.get_items($row['id']).'</ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>';&nbsp; &nbsp; }&nbsp; &nbsp; return $html;}?>我认为您可以轻松修改它以获得您需要的确切 html 标记,但本质就在那里。如果您对此有任何疑问,请不要犹豫!顺便说一下,我使用了和你一样的数据库结构,我在 snm_content 和 snm_categories 中输入了一些行,这是我得到的输出:<li>cat 0&nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; <li>article 0</li>&nbsp; &nbsp; &nbsp; &nbsp; <li>cat 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>article 1</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>article 2</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; <li>cat 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>article 3</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; </ul></li><li>cat 3&nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; <li>article 4</li>&nbsp; &nbsp; </ul></li>

慕斯王

我看到的第一个问题是在下面的代码中while($submenu = $submenuconn->fetch_assoc()){&nbsp; &nbsp; &nbsp; $artikelsubs = '<li class="menu-item"><a href="info/'.$submenu['alias'].'">'.$submenu['title'].'</a>';&nbsp; &nbsp; }如果有 10 篇文章,您存储的是最后一篇吗?此外,li由于某种原因,您没有结束标签。由于我没有数据库来检查这一点,但根据我的理解,循环应该如下所示while($submenu = $submenuconn->fetch_assoc()){&nbsp; &nbsp; &nbsp; $artikelsubs .= '<li class="menu-item"><a href="info/'.$submenu['alias'].'">'.$submenu['title'].'</a></li>';&nbsp; &nbsp; }此外,如果文章列表基于 ,cat_id那么您也应该存储此数据$menuData['articles'][$menu['cat_id']] = $artikelsubs;并确保artikelsubs在内部 while 循环之前清空。然后你需要替换你的$html .= $artikelsubs;从menuData对象中获取它
随时随地看视频慕课网APP
我要回答