PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE urunler (ID INT(11) PRIMARY KEY, kategoriDURUM INT(11), kategoriAD VARCHAR(255), kategoriUST INT(11)); INSERT INTO urunler (ID, kategoriDURUM, kategoriAD, kategoriUST) VALUES (36, 1, 'Elektronik', 0), (37, 1, 'Cep Telefonu', 36), (38, 1, 'Modeller', 37), (39, 1, 'Apple', 38), (40, 1, 'iPhone 11', 39), (41, 1, 'iPhone 12', 39), (42, 1, 'iPhone 13', 39), (43, 1, 'Giyim', 0), (44, 1, 'Erkek Giyim', 43), (45, 1, 'Kadın Giyim', 43);
Copy Clear
Copy Format Clear
<?php function find_subs($mainCategory, $arr, $str = '') { if (array_key_exists($mainCategory['ID'], $arr)) { $current = $arr[$mainCategory['ID']]; unset($arr[$mainCategory['ID']]); foreach ($current as $item) { echo $str . $item['kategoriAD'] . PHP_EOL; if (array_key_exists($item['ID'], $arr)) { $str .= $item['kategoriAD'] . ' --> '; find_subs($item, $arr, $str); } } } } $query = $pdo->query("SELECT kategoriUST, urunler.* FROM urunler")->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP); $mainCategories = reset($query); unset($query[key($query)]); foreach ($mainCategories as $mainCategory) { $str = $mainCategory['kategoriAD'] . ' --> '; echo $mainCategory['kategoriAD'] . PHP_EOL; find_subs($mainCategory, $query, $str); }
Show:  
Copy Clear