Да натиснем още малко. Днес успях да реализирам това, което бях замислил като код за моето дропдаун меню най-накрая с 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:
Код:
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
}
mysqli_query($conn, "SET CHARACTER SET utf8");
function mysql_select($table, $what = array('*'), $where = array(array(), array()), $order = array(array(), array()), $limit = ''){
global $conn;
global $glang;
$sql = 'SELECT ';
if(!$table){
die($glang['mysql_select_table_not_null']);
}
switch(count($what)){
case 0:
die($glang['mysql_select_what_not_null']);
break;
case 1:
if($what[0]=='*'){
$sql.= '*';
}else{
$sql.= '`'.$what[0].'`'; }
break;
}
if((count($what)>=2)&&(count($what)!=1)){
for($i=0;$i<=count($what)-1;$i++){
$sql.='`'.$what[$i].'`';
if($i!=count($what)-1){
$sql.=', ';
}else{
$sql.=' ';
}
}
}
$sql.= 'FROM `'.$table.'` ';
if(!is_array($order)){die('Order must be array. Please, select value.');}
if(!is_array($order[0])){die('Order0 must be array. Please, select value.');}
if(!is_array($order[1])){die('Order1 must be array. Please, select value.');}
if(count($order[0])!=count($order[1])){die('Order count must be equal. Please, select value.');}
if((count($where[0])==0)&&(count($where[1])==0)){
if(count($order[0])>=1){
$sql .= 'ORDER BY ';
for($i=0;$i<=count($order[0])-1;$i++){
$sql.='`'.$order[0][$i].'` '.$order[1][$i];
if($i!=count($order[0])-1){
$sql.=', ';
}
}
}
$result = mysqli_query($conn, $sql);
$rezult = array();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
array_push($rezult, $row);
}
return $rezult;
} else {
return false;
}
}else{
if(!is_array($where)){ die($glang['mysql_select_is_not_array_where']);}
if(!is_array($where[0])){ die($glang['mysql_select_is_not_array_where0']);}
if(!is_array($where[1])){ die($glang['mysql_select_is_not_array_where1']);}
if(count($where[0])!=count($where[1])){ die($glang['mysql_select_is_not_the_same_where']);}
$sql.='WHERE ';
for($i=0;$i<=count($where[0])-1;$i++){
if($i!=count($where[0])-1){
$sql.= '`'.$where[0][$i].'` = \''.$where[1][$i].'\' AND ';
}else{
$sql.= '`'.$where[0][$i].'` = \''.$where[1][$i].'\'';
}
}
if(count($order[0])>=1){
$sql .= 'ORDER BY ';
for($i=0;$i<=count($order[0])-1;$i++){
$sql.='`'.$order[0][$i].'` '.$order[1][$i];
if($i!=count($order[0])-1){
$sql.=', ';
}
}
}
if($limit){
$sql .= 'LIMIT '.$limit;
}
$result = mysqli_query($conn, $sql);
$rezult = array();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
array_push($rezult, $row);
}
return $rezult;
} else {
return false;
}
}
}
function has_children($catid, $orders = 'ASC'){
$rezult = mysql_select('user_product_categories', '*', array(array('main'), array($catid)), array(array('orders'), array($orders)));
if(is_null($rezult)){
return false;
}else{
return $rezult;
}
}
function get_children($id = 0, $orders = 'ASC'){
global $conn;
$menu = array();
$sql = "SELECT * FROM `user_product_categories` WHERE `main` = ".$id." ORDER BY `orders` ".$orders;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if(has_children($row['id'])){
array_push($menu, array($row, get_children($row['id'])));
}else{
array_push($menu, $row);
}
}
} else {
return false;
}
return $menu;
}
//print_r(get_children(1));
function menu($menu = array(), $id =0, $orders = 'ASC'){
$query = mysql_select('user_product_categories', '*', array(array('main'), array($id)), array(array('orders'), array($orders)));
foreach($query as $var){
if(has_children($var['id'])){
$children = get_children($var['id']);
array_push($menu, array($var, $children));
}else{
array_push($menu, array($var));
}
}
return $menu;
}
print_r(menu());
Дотук изкарва структурата ето така:
Код:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Electronics
[main] => 0
[level] => 0
[orders] => 1
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 2
[name] => Computers & Tablets
[main] => 1
[level] => 1
[orders] => 1
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
[1] => Array
(
[0] => Array
(
[id] => 7
[name] => Fashion
[main] => 2
[level] => 3
[orders] => 2
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
)
)
[1] => Array
(
[id] => 3
[name] => Cameras & Photos
[main] => 1
[level] => 1
[orders] => 2
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
[2] => Array
(
[id] => 4
[name] => TV Audio & Surveillance
[main] => 1
[level] => 1
[orders] => 3
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
[3] => Array
(
[id] => 5
[name] => Video Games & Consoles
[main] => 1
[level] => 1
[orders] => 4
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
[4] => Array
(
[0] => Array
(
[id] => 6
[name] => Cell phones & Accessories
[main] => 1
[level] => 1
[orders] => 5
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
[1] => Array
(
[0] => Array
(
[id] => 9
[name] => Shop Cellphones & Accessories
[main] => 6
[level] => 2
[orders] => 1
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
[1] => Array
(
[id] => 10
[name] => Cellphones & Smartphones
[main] => 6
[level] => 2
[orders] => 2
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
)
)
)
)
[1] => Array
(
[0] => Array
(
[id] => 8
[name] => Home & Garden
[main] => 0
[level] => 0
[orders] => 3
[user_added] => 1
[user_edited] => 0
[time_added] => 1533326058
[time_edited] => 0
[user_ip] => 1
)
)
)
Сега как да оформя тази структура в меню?