私はたくさんのカテゴリーのデータベースを持っています、いくつかは子供です:
Array
(
[0] => Array
(
[id] => 1
[name] => Home Improvement
[slug] => Home-Improvement
[parent] =>
[user_id] => 1
[order] => 1
)
[1] => Array
(
[id] => 2
[name] => Asbestos Abatement & Removal
[slug] => Asbestos-Abatement-Removal
[parent] => 1
[user_id] => 1
[order] => 8
)
[2] => Array
(
[id] => 3
[name] => Asphalt & Asphalt Products
[slug] => Asphalt-Asphalt-Products
[parent] => 1
[user_id] => 1
[order] => 9
)
[3] => Array
(
[id] => 4
[name] => Bathroom
[slug] => Bathroom
[parent] => 1
[user_id] => 1
[order] => 10
)
[4] => Array
(
[id] => 5
[name] => Kitchen Cabinets
[slug] => Kitchen-Cabinets
[parent] => 1
[user_id] => 1
[order] => 11
)
[5] => Array
(
[id] => 6
[name] => Ceilings
[slug] => Ceilings
[parent] => 1
[user_id] => 1
[order] => 12
)
[6] => Array
(
[id] => 7
[name] => Cleaning
[slug] => Cleaning
[parent] => 1
[user_id] => 1
[order] => 13
)
[7] => Array
(
[id] => 8
[name] => Closet Organizers & Accessories
[slug] => Closet-Organizers-Accessories
[parent] => 1
[user_id] => 1
[order] => 14
)
[8] => Array
(
[id] => 9
[name] => Concrete
[slug] => Concrete
[parent] => 1
[user_id] => 1
[order] => 15
)
[9] => Array
(
[id] => 10
[name] => Contractors & Service Providers
[slug] => Contractors-Service-Providers
[parent] => 1
[user_id] => 1
[order] => 16
)
私が出力しようとしているのは次のようなものです。
<ul>
<li>Parent
<ul>
<li>Child</li>
</ul>
</li>
<li>Parent with no Children</li>
</ul>
PHPで再帰ツリースクリプトを作成しようとしていますが、行き詰まります。これが私がこれまでに持っているものです。私はelse:とendifの間で何をすべきかについて立ち往生しています。foreachで。(そして、ここで読みやすくするために、その構文を使用しています。)何か提案はありますか?
echo $this->categories->makeTree(0, $this->db->get('categories')->result_array());
public static function makeTree($parent, $array)
{
if (!is_array($array)) return '';
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
else:
endif;
endif;
$output .= '</li>';
endforeach;
$output .= '</ul>';
return $output;
}
編集1
foreachループでデータベース呼び出しを行っていますが、これを機能させることができました。これはおそらく最善のアイデアではありません。
public function makeTree($parent, $array)
{
if (!is_array($array)) return FALSE;
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
$subcategories = ci()->db->get_where('categories', array('parent' => $value['id']));
if ($subcategories->num_rows() > 0):
$output .= $this->makeTree($value['id'], $subcategories->result_array());
endif;
else:
$output .= $value['name'];
$output .= '</li>';
endif;
endif;
endforeach;
$output .= '</ul>';
return $output;
}
編集2
これが私の最終的な解決策であり、DBクエリを実行する代わりに配列を再利用します。
public function makeTree($parent, $array)
{
if (!is_array($array) OR empty($array)) return FALSE;
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
$matches = array();
foreach($array as $subkey => $subvalue):
if ($subvalue['parent'] == $value['id']):
$matches[$subkey] = $subvalue;
endif;
endforeach;
$output .= $this->makeTree($value['id'], $matches);
else:
$output .= $value['name'];
$output .= '</li>';
endif;
endif;
endforeach;
$output .= '</ul>';
return $output;
}