Jump to content

Recommended Posts

Hi, 

 

I need some help here, i cant manage to settle this problem. Wish someone can help me.

 

I want the result as below :-

<ul class="navigation">
<li><a href="main-1.php">main-1</a></li>

<li><a class="dropdown" href="main-2.php">main-2</a>
<ul class="dropdown">
<li><a href="sub-21.php"></a>sub-1</li>
<li><a href="sub-22.php"></a>sub-2</li>							
</ul></li>

<li><a class="dropdown" href="main-3.php">main-3</a>
<ul class="dropdown">
<li><a href="sub-31.php"></a>sub-1</li>
<li><a href="sub-32.php"></a>sub-2</li>
</ul></li>
</ul>

 

 

Here my output : -

<ul class="navigation">

<li><a class="dropdown" href="main-1.php">main-1</li>

<li><a class="dropdown" href="main-2.php">main-2</a>
<ul class="dropdown">
<li><a class="dropdown" href="sub-21.php">sub-1</a></li>
<li><a class="dropdown" href="sub-22.php">sub-2</a></li>
</ul></li>

<li><a class="dropdown" href="main-3.php">main-3</a>
<ul class="dropdown">
<li><a class="dropdown" href="sub-31.php">sub-1</a></li>
<li><a class="dropdown" href="sub-32.php">sub-2</a></li>
</ul></li>
</ul>

 

 

Additional Info :-

<?php
$mysqli = new mysqli('localhost', 'root', '', 'localhost_test');

$query = 'SELECT id, parent_id, name, link FROM categories';

$result = $mysqli->query($query);

if ($result->num_rows > 0) {
	$categories = array();

	while($row = $result->fetch_assoc())
	{
		if ($row['parent_id'] != 0) {
			$categories[$row['parent_id']]['sub'][] = $row;
		} else {
			$categories[$row['id']] = $row;
		}
	}
	
	echo '<ul class="navigation">';
	
	foreach($categories as $cat)
	{
		echo '<li><a class="dropdown" href="' . $cat['link'] . '">';
		
		if ($cat['parent_id'] == 0) {
			echo $cat['name'];	
		}
		
		if (isset($cat['sub']) && is_array($cat['sub'])) {
			echo '</a><ul class="dropdown">';
			
			foreach($cat['sub'] as $sub)
			{
				echo '<li><a class="dropdown" href="' . $sub['link'] . '">' . $sub['name'] . '</a></li>';
			}
			
			echo '</ul>';
		}
		
		echo '</li>';
	}
	
	echo '</ul>';
} else {
	echo 'There are no categories';
}
?>

 

SQL

-- --------------------------------------------------------
-- Host:                         localhost
-- Server version:               5.5.27 - MySQL Community Server (GPL)
-- Server OS:                    Win32
-- HeidiSQL version:             7.0.0.4053
-- Date/time:                    2013-03-08 09:28:54
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET FOREIGN_KEY_CHECKS=0 */;

-- Dumping database structure for localhost_test
CREATE DATABASE IF NOT EXISTS `localhost_test` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `localhost_test`;


-- Dumping structure for table localhost_test.categories
CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(10) unsigned NOT NULL DEFAULT '0',
  `name` varchar(100) NOT NULL,
  `link` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

-- Dumping data for table localhost_test.categories: 7 rows
/*!40000 ALTER TABLE `categories` DISABLE KEYS */;
INSERT INTO `categories` (`id`, `parent_id`, `name`, `link`) VALUES
	(1, 0, 'main-1', 'main-1.php'),
	(2, 0, 'main-2', 'main-2.php'),
	(3, 2, 'sub-1', 'sub-21.php'),
	(4, 2, 'sub-2', 'sub-22.php'),
	(5, 0, 'main-3', 'main-3.php'),
	(6, 5, 'sub-1', 'sub-31.php'),
	(7, 5, 'sub-2', 'sub-32.php');
/*!40000 ALTER TABLE `categories` ENABLE KEYS */;
/*!40014 SET FOREIGN_KEY_CHECKS=1 */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

 

 

Link to comment
https://forums.phpfreaks.com/topic/275392-navigation-menu-array/
Share on other sites

You already have it done dude, you just need to change one part.

 

The first echo inside the foreach loop is what your issue is.

 

Change:

 

echo '<li><a class="dropdown" href="' . $cat['link'] . '">';

 

To:

 

echo '<li><a href="' . $cat['link'] . '">';
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.