简体   繁体   中英

Can you notice what's wrong with my PHP or MYSQL code?

I am trying to create a category menu with sub categories. I have the following MySQL table:

--
-- Table structure for table `categories`
--

CREATE TABLE IF NOT EXISTS `categories` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(1000) NOT NULL,
  `slug` varchar(1000) NOT NULL,
  `parent` int(11) NOT NULL,
  `type` varchar(255) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=66 ;

--
-- Dumping data for table `categories`
--

INSERT INTO `categories` (`ID`, `name`, `slug`, `parent`, `type`) VALUES
(63, 'Party', '/category/party/', 0, ''),
(62, 'Kitchen', '/category/kitchen/', 61, 'sub'),
(59, 'Animals', '/category/animals/', 0, ''),
(64, 'Pets', '/category/pets/', 59, 'sub'),
(61, 'Rooms', '/category/rooms/', 0, ''),
(65, 'Zoo Creatures', '/category/zoo-creatures/', 59, 'sub');

And the following PHP:

<?php

include("connect.php");

echo "<ul>";

$query = mysql_query("SELECT * FROM categories");

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

    $catId   = $row['id'];
    $catName = $row['name'];
    $catSlug = $row['slug'];
    $parent  = $row['parent'];
    $type    = $row['type'];

    if ($type == "sub") {

        $select = mysql_query("SELECT name FROM categories WHERE ID = $parent");

        while ($row = mysql_fetch_assoc($select)) {
            $parentName = $row['name'];
        }

        echo "<li>$parentName >> $catName</li>";

    }
    else if ($type == "") {
        echo "<li>$catName</li>";
    }

}

echo "</ul>";

?>

Now Here's the Problem,

It displays this:

* Party
* Rooms >> Kitchen
* Animals
* Animals >> Pets
* Rooms
* Animals >> Zoo Creatures

I want it to display this:

* Party
* Rooms >> Kitchen
* Animals >> Pets >> Zoo Creatures

Is there something wrong with my loop? I just can't figure it out.

Both 'Zoo Creatures' and 'Pets' have 'Animals' (59) as their parent.
To do what you want you probably need to set 'Pets' as a parent for 'Zoo Creatures'.
To do that replace last SQL line with this:

(65, 'Zoo Creatures', '/category/zoo-creatures/', 64, 'sub');

Change your first select statement to:

SELECT * FROM categories WHERE parent = 0

You'll also need to recursively call the code in your $type == "sub" code.

Your type field appears to duplicate the functionality of the parent field. If the menu item has a parent, is not a submenu?

echo '<ul>';

$parents = mysql_query('SELECT * FROM categories WHERE parent = 0');

while ($row = mysql_fetch_assoc($parents)) {
  $catId = $row['ID'];
  $catName = $row['name'];
  $catSlug = $row['slug'];
  $parent = $row['parent'];
  $type = $row['type'];

  $output = "<li>$catName";
  $children = mysql_query("SELECT name FROM categories WHERE parent = $catId");
  while ($child = mysql_fetch_assoc($children)) {
    $childName = $child['name'];
    $output .= " &gt;&gt; $childName";
  }

  $output .= '</li>';

  echo $output;
}

echo '</ul>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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