Skip to content

Instantly share code, notes, and snippets.

@AnrDaemon
Created October 5, 2017 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnrDaemon/68e7f1372e0a28d88d2490aacfff3a9b to your computer and use it in GitHub Desktop.
Save AnrDaemon/68e7f1372e0a28d88d2490aacfff3a9b to your computer and use it in GitHub Desktop.
<?php
$list = $_pdo->getAll('SELECT s1.`id` `id` FROM cat_section s1
LEFT JOIN cat_section s2 ON s1.`parent_id` = s2.`id`
WHERE s1.`parent_id` <> 0 AND s2.`id` IS NULL', [], \PDO::FETCH_COLUMN);
$iter = function($sid)
use(&$iter, $_pdo)
{
foreach($_pdo->getAll('SELECT s1.`id` `id` FROM cat_section s1
WHERE s1.`parent_id` = ?', [$sid], \PDO::FETCH_COLUMN) as $id
)
{
yield $id;
foreach($iter($id) as $cid)
{
yield $cid;
}
}
};
$data = $list;
foreach($list as $sid)
{
foreach($iter($sid) as $id)
{
$data[] = $id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment