简体   繁体   English

php / mysql动态菜单

[英]php/mysql dynamic menu

I have an existing drop down menu that I am attempting to convert to PHP/MySQL to allow the client to edit the menu items. 我有一个现有的下拉菜单,我试图转换为PHP / MySQL,以允许客户端编辑菜单项。 This is the structure: 这是结构:

Home-index.php 首页 - 的index.php
About Us-# 关于我们-#
-about.php -about.php
-annualreport.php -annualreport.php
-awards.php -awards.php
Services-# 服务-#
-services.php -services.php
-loans.php -loans.php

and so forth. 等等。 The query I'm using is: 我正在使用的查询是:

select distinct mainlinks.id as daddyid, mainlinks.title as daddytitle, mainlinks.urlink as daddyurl, babylinks.id as babyid, babylinks.title as babytitle, babylinks.urlink as babyurl from mainlinks, babylinks where mainlinks.id = babylinks.parentid order by mainlinks.listorder";

And the result displayed with the code below: 结果显示如下代码:

$result = mysql_query($query) or die(mysql_error());
// keep track of previous maincategory
$previous_maincategory = NULL;

while ($row = mysql_fetch_assoc($result))
{

        // if maincategory has changed from previouscategory then display it
        if ($previous_maincategory != $row['daddytitle']) 
        {
                echo "<strong><h2>" . strtoupper($row['daddytitle']) . "</h2></strong>";
        }

        echo '<a onclick="return confirmSubmit()" class="inside" href="deletesubcategory.php?id=';
        echo $row['babyid'];
        echo '">';
        echo $row['babytitle'];
        echo "</a>";
        echo "<br/>";

        // record what the previous category was
        $previous_maincategory = $row['daddytitle'];
}

It only displays the items that have a child element, not the parent items that do not have child elements. 它仅显示具有元素的项目, 而不显示没有子元素的父项目。 Any ideas? 有任何想法吗? I'm guessing the problem is with the query where clause but I can't figure out how to grab what I need. 我猜测问题是查询where子句但我无法弄清楚如何抓住我需要的东西。 Thanks. 谢谢。

You have to use LEFT JOIN to get items which don't have childs, try 您必须使用LEFT JOIN来获取没有孩子的项目,请尝试

select distinct mainlinks.id as daddyid, mainlinks.title as daddytitle, mainlinks.urlink as daddyurl, babylinks.id as babyid, babylinks.title as babytitle, babylinks.urlink as babyurl 
from mainlinks 
  LEFT JOIN babylinks ON mainlinks.id = babylinks.parentid
order by mainlinks.listorder;

Your query is only grabbing items where mainlinks.id = babylinks.parentid meaning it will ignore items where that relationship is non-existent. 您的查询只是抓取where mainlinks.id = babylinks.parentid项目,这意味着它将忽略该关系不存在的项目。

Try this: 尝试这个:

SELECT DISTINCT mainlinks.id as daddyid, mainlinks.title as daddytitle, mainlinks.urlink as daddyurl, babylinks.id as babyid, babylinks.title as babytitle, babylinks.urlink as babyurl FROM mainlinks LEFT JOIN babylinks ON mainlinks.id = babylinks.parentid ORDER BY mainlinks.listorder

Using a LEFT JOIN will tell your query to pull all the mainlinks regardless if they have a baby link, but only pull baby links that have a parent. 使用LEFT JOIN将告诉您的查询拉出所有主链接,无论它们是否有婴儿链接,但只提取具有父级的婴儿链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM