Проблем с извеждането на меню

C++ JAVA
Post Reply
User avatar
xlebabarov
Нов
Нов
Posts: 186
Joined: Tue Nov 10, 2009 6:24 am

Проблем с извеждането на меню

Post by xlebabarov » Wed Oct 31, 2018 3:32 pm

Здравейте :cry: :cry: :cry:

Code: Select all

	//////SELECTING MAX SUB LEVEL
	$sql = "SELECT `level` FROM `user_product_categories` ORDER BY `user_product_categories`.`level` DESC LIMIT 1";
	$result = mysqli_query($conn, $sql);
	
	if (mysqli_num_rows($result) > 0) {
		// output data of each row
		while($row = mysqli_fetch_assoc($result)) {
			$maxsublevel = $row['level'];
		}
	} else {
		echo "0 results";
	}
	/////////////////////////////////////////////


	$new = array();
	for&#40;$i=0;$i<=$maxsublevel;$i++&#41;&#123;
		$rezult = mysql_select&#40;'user_product_categories', '*', array&#40;array&#40;'level'&#41;, array&#40;$i&#41;&#41;, array&#40;array&#40;'orders'&#41;, array&#40;$orders&#41;&#41;&#41;;
		foreach&#40;$rezult as $var&#41;&#123;
			array_push&#40;$new, array&#40;'id'=>$var&#91;'id'&#93;, 'main'=>$var&#91;'main'&#93;, 'level'=>$i, &#41;&#41;;
		&#125;
	&#125;

$sortArray = array&#40;&#41;; 

foreach&#40;$new as $person&#41;&#123; 
    foreach&#40;$person as $key=>$value&#41;&#123; 
        if&#40;!isset&#40;$sortArray&#91;$key&#93;&#41;&#41;&#123; 
            $sortArray&#91;$key&#93; = array&#40;&#41;; 
        &#125; 
        $sortArray&#91;$key&#93;&#91;&#93; = $value; 
    &#125; 
&#125; 

$orderby = "level"; //change this to whatever key you want from the array 

array_multisort&#40;$sortArray&#91;$orderby&#93;,SORT_ASC,$new&#41;; 

print_r&#40;$new&#41;;
До тук с този код успях да изкарам менюто от базата данни като то изглежда по следния начин:

Code: Select all

Array
&#40;
    &#91;0&#93; => Array
        &#40;
            &#91;id&#93; => 1
            &#91;main&#93; => 0
            &#91;level&#93; => 0
        &#41;

    &#91;1&#93; => Array
        &#40;
            &#91;id&#93; => 8
            &#91;main&#93; => 0
            &#91;level&#93; => 0
        &#41;

    &#91;2&#93; => Array
        &#40;
            &#91;id&#93; => 2
            &#91;main&#93; => 1
            &#91;level&#93; => 1
        &#41;

    &#91;3&#93; => Array
        &#40;
            &#91;id&#93; => 3
            &#91;main&#93; => 1
            &#91;level&#93; => 1
        &#41;

    &#91;4&#93; => Array
        &#40;
            &#91;id&#93; => 4
            &#91;main&#93; => 1
            &#91;level&#93; => 1
        &#41;

    &#91;5&#93; => Array
        &#40;
            &#91;id&#93; => 5
            &#91;main&#93; => 1
            &#91;level&#93; => 1
        &#41;

    &#91;6&#93; => Array
        &#40;
            &#91;id&#93; => 6
            &#91;main&#93; => 1
            &#91;level&#93; => 1
        &#41;

    &#91;7&#93; => Array
        &#40;
            &#91;id&#93; => 9
            &#91;main&#93; => 6
            &#91;level&#93; => 2
        &#41;

    &#91;8&#93; => Array
        &#40;
            &#91;id&#93; => 10
            &#91;main&#93; => 6
            &#91;level&#93; => 2
        &#41;

    &#91;9&#93; => Array
        &#40;
            &#91;id&#93; => 7
            &#91;main&#93; => 2
            &#91;level&#93; => 3
        &#41;

&#41;
Въпроса ми е как аджеба да изкарам менюто след този масив?
Трябва да се наредят по Main и после по Level за да излезне нещо такова:

1:
-2
--7
-3
-4
-5
-6
--9
--10
Гошо пича ;) Понякога питам въпросчета с повишена трудност, но какво да се прави :)

raiden
Гуру
Гуру
Posts: 2182
Joined: Fri Dec 08, 2006 8:13 am
Answers: 190
Location: Варна

Post by raiden » Wed Oct 31, 2018 5:30 pm

Това не е ли същото: http://web-tourist.net/forum/viewtopic.php?t=129912

User avatar
xlebabarov
Нов
Нов
Posts: 186
Joined: Tue Nov 10, 2009 6:24 am

Post by xlebabarov » Thu Nov 01, 2018 1:55 pm

Подобно но тук съм стигнал до другаде
Гошо пича ;) Понякога питам въпросчета с повишена трудност, но какво да се прави :)

raiden
Гуру
Гуру
Posts: 2182
Joined: Fri Dec 08, 2006 8:13 am
Answers: 190
Location: Варна

Re: Проблем с извеждането на меню

Post by raiden » Thu Nov 01, 2018 7:43 pm

Интересен подход към сортирането :shock:
Но всъщност не ти трябва сортиране а групиране, вижда се че има йерархична структура и данните ти трябва да я копират. Примера който съм ти написал в другата тема ще ти я създаде, след това трябва само да изпечаташ менюто. За да работи с неограничен брой нива ти трябва 1 рекурсивна функция:
[php]function printMenu($menu) {
foreach($menu as $item) {
echo str_repeat("-", $item['level']) . $item['id'];
echo ($item['level'] == 0) ? ":<br>" : "<br>";
if (isset($item['subs']) && is_array($item['subs'])) {
printMenu($item['subs']);
}
}
}[/php]
Ето ти направо цял пример:
[php]<?php
$array = [
0 => ['id' => 1, 'main' => 0, 'level' => 0],
1 => ['id' => 2, 'main' => 1, 'level' => 1],
2 => ['id' => 3, 'main' => 1, 'level' => 1],
3 => ['id' => 4, 'main' => 1, 'level' => 1],
4 => ['id' => 5, 'main' => 1, 'level' => 1],
5 => ['id' => 6, 'main' => 1, 'level' => 1],
6 => ['id' => 7, 'main' => 2, 'level' => 2],
7 => ['id' => 9, 'main' => 6, 'level' => 2],
8 => ['id' => 10, 'main' => 6, 'level' => 2],
];

function nestSubs($array) {
$result = array_reduce(
$array,
function ($result, $i) {
$result[$i['level']][$i['id']] = $i;
return $result;
},
[]
);
$maxLevel = max(array_keys($result));
for ($i = $maxLevel; $i > 0; $i--) {
foreach($result[$i] as $sub) {
$result[$i - 1][$sub['main']]['subs'][$sub['id']] = $sub;
}
unset($result[$i]);
}

return $result[0];
}

function printMenu($menu) {
foreach($menu as $item) {
echo str_repeat("-", $item['level']) . $item['id'];
echo ($item['level'] == 0) ? ":<br>" : "<br>";
if (isset($item['subs']) && is_array($item['subs'])) {
printMenu($item['subs']);
}
}
}

printMenu(nestSubs($array));[/php]
1:
-2
--7
-3
-4
-5
-6
--9
--10

User avatar
xlebabarov
Нов
Нов
Posts: 186
Joined: Tue Nov 10, 2009 6:24 am

Post by xlebabarov » Fri Nov 02, 2018 3:56 pm

Да натиснем още малко. Днес успях да реализирам това, което бях замислил като код за моето дропдаун меню най-накрая с 3000 зора това е кода:

[sql]-- phpMyAdmin SQL Dump
-- version 4.8.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Sep 06, 2018 at 09:20 PM
-- Server version: 10.1.34-MariaDB
-- PHP Version: 7.2.7

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES cp1251 */;

--
-- Database: `blank_website`
--

-- --------------------------------------------------------

--
-- Table structure for table `user_product_categories`
--

CREATE TABLE `user_product_categories` (
`id` int(11) NOT NULL,
`name` varchar(225) COLLATE cp1251_bulgarian_ci NOT NULL,
`main` int(11) NOT NULL,
`level` int(11) NOT NULL,
`orders` int(11) NOT NULL,
`user_added` int(11) NOT NULL,
`user_edited` int(11) NOT NULL,
`time_added` int(11) NOT NULL,
`time_edited` int(11) NOT NULL,
`user_ip` varchar(225) COLLATE cp1251_bulgarian_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=cp1251 COLLATE=cp1251_bulgarian_ci;

--
-- Dumping data for table `user_product_categories`
--

INSERT INTO `user_product_categories` (`id`, `name`, `main`, `level`, `orders`, `user_added`, `user_edited`, `time_added`, `time_edited`, `user_ip`) VALUES
(1, 'Electronics', 0, 0, 1, 1, 0, 1533326058, 0, '1'),
(2, 'Computers & Tablets', 1, 1, 1, 1, 0, 1533326058, 0, '1'),
(3, 'Cameras & Photos', 1, 1, 2, 1, 0, 1533326058, 0, '1'),
(4, 'TV Audio & Surveillance', 1, 1, 3, 1, 0, 1533326058, 0, '1'),
(5, 'Video Games & Consoles', 1, 1, 4, 1, 0, 1533326058, 0, '1'),
(6, 'Cell phones & Accessories', 1, 1, 5, 1, 0, 1533326058, 0, '1'),
(7, 'Fashion', 0, 0, 2, 1, 0, 1533326058, 0, '1'),
(8, 'Home & Garden', 0, 0, 3, 1, 0, 1533326058, 0, '1'),
(9, 'Shop Cellphones & Accessories', 6, 2, 1, 1, 0, 1533326058, 0, '1'),
(10, 'Cellphones & Smartphones', 6, 2, 2, 1, 0, 1533326058, 0, '1');

-- --------------------------------------------------------

--
-- Table structure for table `user_product_categories_lang`
--

CREATE TABLE `user_product_categories_lang` (
`id` int(11) NOT NULL,
`cat_id` int(11) NOT NULL,
`lang_id` int(11) NOT NULL,
`name` varchar(225) COLLATE cp1251_bulgarian_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=cp1251 COLLATE=cp1251_bulgarian_ci;

--
-- Dumping data for table `user_product_categories_lang`
--

INSERT INTO `user_product_categories_lang` (`id`, `cat_id`, `lang_id`, `name`) VALUES
(1, 1, 1, 'Електроника'),
(2, 1, 2, 'Electronics'),
(3, 2, 2, 'Computers & Tablets'),
(4, 3, 2, 'Cameras & Photos'),
(5, 4, 2, 'TV Audio & Surveillance'),
(6, 5, 2, 'Video Games & Consoles'),
(7, 6, 2, 'Cell phones & Accessories'),
(8, 2, 1, 'Компютри и таблети'),
(9, 3, 1, 'Камери и снимки'),
(10, 4, 1, 'ТВ Аудио и Охранителна техника'),
(11, 5, 1, 'Видео игри и конзоли'),
(12, 6, 1, 'Мобилни телефони & Аксесоари'),
(13, 7, 2, 'Fashion'),
(14, 8, 2, 'Home & Garden'),
(15, 7, 1, 'Мода'),
(16, 8, 1, 'Дом & Градина'),
(17, 9, 2, 'Shop Cellphones & Accessories'),
(18, 10, 2, 'Cellphones & Smartphones'),
(19, 9, 1, 'Клетъчни телефони & Аксесоари'),
(20, 10, 1, 'Клетъчни телефони & Смартфони');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `user_product_categories`
--
ALTER TABLE `user_product_categories`
ADD PRIMARY KEY (`id`);

--
-- Indexes for table `user_product_categories_lang`
--
ALTER TABLE `user_product_categories_lang`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `user_product_categories`
--
ALTER TABLE `user_product_categories`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;

--
-- AUTO_INCREMENT for table `user_product_categories_lang`
--
ALTER TABLE `user_product_categories_lang`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=21;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
[/sql]

add_cat.php:

Code: Select all


$conn = mysqli_connect&#40;$servername, $username, $password, $dbname&#41;;
// Check connection
if &#40;!$conn&#41; &#123;
    die&#40;"Connection failed&#58; " . mysqli_connect_error&#40;&#41;&#41;;
&#125;
&#125; 
mysqli_query&#40;$conn, "SET CHARACTER SET utf8"&#41;;

function mysql_select&#40;$table, $what = array&#40;'*'&#41;, $where = array&#40;array&#40;&#41;, array&#40;&#41;&#41;, $order = array&#40;array&#40;&#41;, array&#40;&#41;&#41;, $limit = ''&#41;&#123;
	global $conn;
	global $glang;
	
	
	$sql = 'SELECT ';
	if&#40;!$table&#41;&#123;
		die&#40;$glang&#91;'mysql_select_table_not_null'&#93;&#41;;
	&#125;
	switch&#40;count&#40;$what&#41;&#41;&#123;
		case 0&#58;
		die&#40;$glang&#91;'mysql_select_what_not_null'&#93;&#41;;
		break;
		case 1&#58;
		if&#40;$what&#91;0&#93;=='*'&#41;&#123;
		$sql.= '*';
		&#125;else&#123;
		$sql.= '`'.$what&#91;0&#93;.'`';			&#125;
		break;
	&#125;
	if&#40;&#40;count&#40;$what&#41;>=2&#41;&&&#40;count&#40;$what&#41;!=1&#41;&#41;&#123;
		for&#40;$i=0;$i<=count&#40;$what&#41;-1;$i++&#41;&#123;
			$sql.='`'.$what&#91;$i&#93;.'`';
			if&#40;$i!=count&#40;$what&#41;-1&#41;&#123;
			$sql.=', ';
			&#125;else&#123;
			$sql.=' ';	
			&#125;
		&#125;
		
	&#125;
	
	$sql.= 'FROM `'.$table.'` ';
	
	if&#40;!is_array&#40;$order&#41;&#41;&#123;die&#40;'Order must be array. Please, select value.'&#41;;&#125;
	if&#40;!is_array&#40;$order&#91;0&#93;&#41;&#41;&#123;die&#40;'Order0 must be array. Please, select value.'&#41;;&#125;
	if&#40;!is_array&#40;$order&#91;1&#93;&#41;&#41;&#123;die&#40;'Order1 must be array. Please, select value.'&#41;;&#125;
	if&#40;count&#40;$order&#91;0&#93;&#41;!=count&#40;$order&#91;1&#93;&#41;&#41;&#123;die&#40;'Order count must be equal. Please, select value.'&#41;;&#125;
	
	if&#40;&#40;count&#40;$where&#91;0&#93;&#41;==0&#41;&&&#40;count&#40;$where&#91;1&#93;&#41;==0&#41;&#41;&#123;	
	if&#40;count&#40;$order&#91;0&#93;&#41;>=1&#41;&#123;
			$sql .= 'ORDER BY ';
			for&#40;$i=0;$i<=count&#40;$order&#91;0&#93;&#41;-1;$i++&#41;&#123;
				$sql.='`'.$order&#91;0&#93;&#91;$i&#93;.'` '.$order&#91;1&#93;&#91;$i&#93;;
				if&#40;$i!=count&#40;$order&#91;0&#93;&#41;-1&#41;&#123;
					$sql.=', ';
				&#125;
			&#125;
		&#125;
	
	
	$result = mysqli_query&#40;$conn, $sql&#41;;
	$rezult = array&#40;&#41;;
	if &#40;mysqli_num_rows&#40;$result&#41; > 0&#41; &#123;
		// output data of each row
		while&#40;$row = mysqli_fetch_assoc&#40;$result&#41;&#41; &#123;
			array_push&#40;$rezult, $row&#41;;
		&#125;
		return $rezult;
	&#125; else &#123;
		return false;
	&#125;
	
	&#125;else&#123;
		
		if&#40;!is_array&#40;$where&#41;&#41;&#123; die&#40;$glang&#91;'mysql_select_is_not_array_where'&#93;&#41;;&#125;
		if&#40;!is_array&#40;$where&#91;0&#93;&#41;&#41;&#123; die&#40;$glang&#91;'mysql_select_is_not_array_where0'&#93;&#41;;&#125;
		if&#40;!is_array&#40;$where&#91;1&#93;&#41;&#41;&#123; die&#40;$glang&#91;'mysql_select_is_not_array_where1'&#93;&#41;;&#125;
		if&#40;count&#40;$where&#91;0&#93;&#41;!=count&#40;$where&#91;1&#93;&#41;&#41;&#123; die&#40;$glang&#91;'mysql_select_is_not_the_same_where'&#93;&#41;;&#125;
		$sql.='WHERE ';
	for&#40;$i=0;$i<=count&#40;$where&#91;0&#93;&#41;-1;$i++&#41;&#123;
		if&#40;$i!=count&#40;$where&#91;0&#93;&#41;-1&#41;&#123;
			$sql.= '`'.$where&#91;0&#93;&#91;$i&#93;.'` = \''.$where&#91;1&#93;&#91;$i&#93;.'\' AND ';
		&#125;else&#123;
			$sql.= '`'.$where&#91;0&#93;&#91;$i&#93;.'` = \''.$where&#91;1&#93;&#91;$i&#93;.'\'';

		&#125;
	&#125;
		if&#40;count&#40;$order&#91;0&#93;&#41;>=1&#41;&#123;
			$sql .= 'ORDER BY ';
			for&#40;$i=0;$i<=count&#40;$order&#91;0&#93;&#41;-1;$i++&#41;&#123;
				$sql.='`'.$order&#91;0&#93;&#91;$i&#93;.'` '.$order&#91;1&#93;&#91;$i&#93;;
				if&#40;$i!=count&#40;$order&#91;0&#93;&#41;-1&#41;&#123;
					$sql.=', ';
				&#125;
			&#125;
		&#125;

		if&#40;$limit&#41;&#123;
			$sql .= 'LIMIT '.$limit;
		&#125;

	$result = mysqli_query&#40;$conn, $sql&#41;;
	$rezult = array&#40;&#41;;
	if &#40;mysqli_num_rows&#40;$result&#41; > 0&#41; &#123;
		// output data of each row
		while&#40;$row = mysqli_fetch_assoc&#40;$result&#41;&#41; &#123;
			array_push&#40;$rezult, $row&#41;;
		&#125;
		return $rezult;
	&#125; else &#123;
		return false;
	&#125;

	&#125;
	
	
	
&#125;

function has_children&#40;$catid, $orders = 'ASC'&#41;&#123;
	$rezult = mysql_select&#40;'user_product_categories', '*', array&#40;array&#40;'main'&#41;, array&#40;$catid&#41;&#41;, array&#40;array&#40;'orders'&#41;, array&#40;$orders&#41;&#41;&#41;;
	
	if&#40;is_null&#40;$rezult&#41;&#41;&#123;
		return false;
	&#125;else&#123;
		return $rezult;
	&#125;
&#125;


function get_children&#40;$id = 0, $orders = 'ASC'&#41;&#123;
	global $conn;
	$menu = array&#40;&#41;;
	$sql = "SELECT * FROM `user_product_categories` WHERE `main` = ".$id." ORDER BY `orders` ".$orders;
	$result = mysqli_query&#40;$conn, $sql&#41;;
	
	if &#40;mysqli_num_rows&#40;$result&#41; > 0&#41; &#123;
		// output data of each row
		while&#40;$row = mysqli_fetch_assoc&#40;$result&#41;&#41; &#123;
			if&#40;has_children&#40;$row&#91;'id'&#93;&#41;&#41;&#123;
				array_push&#40;$menu, array&#40;$row, get_children&#40;$row&#91;'id'&#93;&#41;&#41;&#41;;
			&#125;else&#123;
			array_push&#40;$menu, $row&#41;;
			&#125;
		&#125;
	&#125; else &#123;
		return false;
	&#125;
		
	
	return $menu;
&#125;

//print_r&#40;get_children&#40;1&#41;&#41;;

function menu&#40;$menu = array&#40;&#41;, $id =0, $orders = 'ASC'&#41;&#123;
	$query = mysql_select&#40;'user_product_categories', '*', array&#40;array&#40;'main'&#41;, array&#40;$id&#41;&#41;, array&#40;array&#40;'orders'&#41;, array&#40;$orders&#41;&#41;&#41;;
	
	foreach&#40;$query as $var&#41;&#123;
		if&#40;has_children&#40;$var&#91;'id'&#93;&#41;&#41;&#123;
			$children = get_children&#40;$var&#91;'id'&#93;&#41;;
			array_push&#40;$menu, array&#40;$var, $children&#41;&#41;;
			
		&#125;else&#123;
			array_push&#40;$menu, array&#40;$var&#41;&#41;;			
		&#125;
	&#125;
	


			
	return $menu;
&#125;

print_r&#40;menu&#40;&#41;&#41;;
Дотук изкарва структурата ето така:

Code: Select all

Array
&#40;
    &#91;0&#93; => Array
        &#40;
            &#91;0&#93; => Array
                &#40;
                    &#91;id&#93; => 1
                    &#91;name&#93; => Electronics
                    &#91;main&#93; => 0
                    &#91;level&#93; => 0
                    &#91;orders&#93; => 1
                    &#91;user_added&#93; => 1
                    &#91;user_edited&#93; => 0
                    &#91;time_added&#93; => 1533326058
                    &#91;time_edited&#93; => 0
                    &#91;user_ip&#93; => 1
                &#41;

            &#91;1&#93; => Array
                &#40;
                    &#91;0&#93; => Array
                        &#40;
                            &#91;0&#93; => Array
                                &#40;
                                    &#91;id&#93; => 2
                                    &#91;name&#93; => Computers & Tablets
                                    &#91;main&#93; => 1
                                    &#91;level&#93; => 1
                                    &#91;orders&#93; => 1
                                    &#91;user_added&#93; => 1
                                    &#91;user_edited&#93; => 0
                                    &#91;time_added&#93; => 1533326058
                                    &#91;time_edited&#93; => 0
                                    &#91;user_ip&#93; => 1
                                &#41;

                            &#91;1&#93; => Array
                                &#40;
                                    &#91;0&#93; => Array
                                        &#40;
                                            &#91;id&#93; => 7
                                            &#91;name&#93; => Fashion
                                            &#91;main&#93; => 2
                                            &#91;level&#93; => 3
                                            &#91;orders&#93; => 2
                                            &#91;user_added&#93; => 1
                                            &#91;user_edited&#93; => 0
                                            &#91;time_added&#93; => 1533326058
                                            &#91;time_edited&#93; => 0
                                            &#91;user_ip&#93; => 1
                                        &#41;

                                &#41;

                        &#41;

                    &#91;1&#93; => Array
                        &#40;
                            &#91;id&#93; => 3
                            &#91;name&#93; => Cameras & Photos
                            &#91;main&#93; => 1
                            &#91;level&#93; => 1
                            &#91;orders&#93; => 2
                            &#91;user_added&#93; => 1
                            &#91;user_edited&#93; => 0
                            &#91;time_added&#93; => 1533326058
                            &#91;time_edited&#93; => 0
                            &#91;user_ip&#93; => 1
                        &#41;

                    &#91;2&#93; => Array
                        &#40;
                            &#91;id&#93; => 4
                            &#91;name&#93; => TV Audio & Surveillance
                            &#91;main&#93; => 1
                            &#91;level&#93; => 1
                            &#91;orders&#93; => 3
                            &#91;user_added&#93; => 1
                            &#91;user_edited&#93; => 0
                            &#91;time_added&#93; => 1533326058
                            &#91;time_edited&#93; => 0
                            &#91;user_ip&#93; => 1
                        &#41;

                    &#91;3&#93; => Array
                        &#40;
                            &#91;id&#93; => 5
                            &#91;name&#93; => Video Games & Consoles
                            &#91;main&#93; => 1
                            &#91;level&#93; => 1
                            &#91;orders&#93; => 4
                            &#91;user_added&#93; => 1
                            &#91;user_edited&#93; => 0
                            &#91;time_added&#93; => 1533326058
                            &#91;time_edited&#93; => 0
                            &#91;user_ip&#93; => 1
                        &#41;

                    &#91;4&#93; => Array
                        &#40;
                            &#91;0&#93; => Array
                                &#40;
                                    &#91;id&#93; => 6
                                    &#91;name&#93; => Cell phones & Accessories
                                    &#91;main&#93; => 1
                                    &#91;level&#93; => 1
                                    &#91;orders&#93; => 5
                                    &#91;user_added&#93; => 1
                                    &#91;user_edited&#93; => 0
                                    &#91;time_added&#93; => 1533326058
                                    &#91;time_edited&#93; => 0
                                    &#91;user_ip&#93; => 1
                                &#41;

                            &#91;1&#93; => Array
                                &#40;
                                    &#91;0&#93; => Array
                                        &#40;
                                            &#91;id&#93; => 9
                                            &#91;name&#93; => Shop Cellphones & Accessories
                                            &#91;main&#93; => 6
                                            &#91;level&#93; => 2
                                            &#91;orders&#93; => 1
                                            &#91;user_added&#93; => 1
                                            &#91;user_edited&#93; => 0
                                            &#91;time_added&#93; => 1533326058
                                            &#91;time_edited&#93; => 0
                                            &#91;user_ip&#93; => 1
                                        &#41;

                                    &#91;1&#93; => Array
                                        &#40;
                                            &#91;id&#93; => 10
                                            &#91;name&#93; => Cellphones & Smartphones
                                            &#91;main&#93; => 6
                                            &#91;level&#93; => 2
                                            &#91;orders&#93; => 2
                                            &#91;user_added&#93; => 1
                                            &#91;user_edited&#93; => 0
                                            &#91;time_added&#93; => 1533326058
                                            &#91;time_edited&#93; => 0
                                            &#91;user_ip&#93; => 1
                                        &#41;

                                &#41;

                        &#41;

                &#41;

        &#41;

    &#91;1&#93; => Array
        &#40;
            &#91;0&#93; => Array
                &#40;
                    &#91;id&#93; => 8
                    &#91;name&#93; => Home & Garden
                    &#91;main&#93; => 0
                    &#91;level&#93; => 0
                    &#91;orders&#93; => 3
                    &#91;user_added&#93; => 1
                    &#91;user_edited&#93; => 0
                    &#91;time_added&#93; => 1533326058
                    &#91;time_edited&#93; => 0
                    &#91;user_ip&#93; => 1
                &#41;

        &#41;

&#41;
Сега как да оформя тази структура в меню?

User avatar
xlebabarov
Нов
Нов
Posts: 186
Joined: Tue Nov 10, 2009 6:24 am

Post by xlebabarov » Tue Nov 06, 2018 4:29 pm

Code: Select all

function map&#40;$array&#41;&#123;
	
	function array_list&#40;$array&#41;&#123;
		foreach&#40;$array as $var&#41;&#123;
		if&#40;is_array&#40;$var&#41;&#41;&#123;
			if&#40;array_key_exists&#40;'id', $var&#41;&#41;&#123;
				echo&#40;'<tr><td>'&#41;;
				for&#40;$i=1;$i<=$var&#91;'level'&#93;;$i++&#41;&#123;
					echo&#40;'-'&#41;;
				&#125;
				
				echo&#40;$var&#91;'name'&#93;.'</td></tr>'&#41;;
			&#125;else&#123;
				array_list&#40;$var&#41;;
			&#125;		
		&#125;
		&#125;
	&#125;
	echo&#40;'<table>'&#41;;
	array_list&#40;$array&#41;;
	echo&#40;'</table>'&#41;;
 &#125;
 
 map&#40;menu&#40;&#41;&#41;;
:o :o :o

Резултат:

Code: Select all

Electronics
-Computers & Tablets
---Fashion
-Cameras & Photos
-TV Audio & Surveillance
-Video Games & Consoles
-Cell phones & Accessories
--Shop Cellphones & Accessories
--Cellphones & Smartphones
Home & Garden
Сега как да дам точка на себе си? :?:

Post Reply