Reputation: 13
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. This is the structure:
Home-index.php
About Us-#
-about.php
-annualreport.php
-awards.php
Services-#
-services.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. Thanks.
Upvotes: 0
Views: 410
Reputation: 77956
Your query is only grabbing items where mainlinks.id = babylinks.parentid
meaning it will ignore items where that relationship is non-existent.
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.
Upvotes: 1
Reputation: 22749
You have to use LEFT JOIN
to get items which don't have childs, try
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;
Upvotes: 1