我在下面有这个菜单功能,用于获取论坛和类别列表。
每次我需要获得一个新关卡时,我必须在菜单功能中添加一个foreach循环。我希望能够只有一个或两个foreach循环,可以获得属于该论坛的级别
问题:我怎样才能在菜单中只有一个或两个foreach循环 可以获得多个类别的功能
菜单功能
public function menu() {
$html = '';
$html .= '<select class="form-control">';
$html .= '<option value="0" selected="selected">None</option>';
foreach ($this->get_parent_forums() as $parent) {
if ($parent->pid == '0') {
$html .= '<option value="" class="optionParent">' . $parent->name . '</option>';
foreach ($this->get_child_forums($parent->fid) as $childs) {
if ($childs->pid == $parent->fid) {
$html .= '<option value="" class="optionChild">' .$childs->name. '</option>';
foreach ($this->get_child_forums($childs->fid) as $grandchilds) {
if ($grandchilds->pid == $childs->fid) {
$html .= '<option value="" class="optionChild">' .$grandchilds->name. '</option>';
}
}
}
}
}
}
$html .= '</select>';
return $html;
}
完整控制器代码
<?php
class Forum_management extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin/forum/forum_model');
}
public function add() {
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->form_validation->set_rules('title', 'title', 'required');
if ($this->form_validation->run() == TRUE) {
$this->forum_model->insert();
}
$data['forums_select'] = $this->menu();
$this->load->view('template/forums/forum_add_view', $data);
}
public function menu() {
$html = '';
$html .= '<select class="form-control">';
$html .= '<option value="0" selected="selected">None</option>';
foreach ($this->get_forums() as $parent) {
if ($parent->pid == '0') {
$html .= '<option value="" class="optionParent">' . $parent->name . '</option>';
foreach ($this->get_child_forums($parent->fid) as $childs) {
if ($childs->pid == $parent->fid) {
$html .= '<option value="" class="optionChild">' .$childs->name. '</option>';
foreach ($this->get_child_forums($childs->fid) as $grandchilds) {
if ($grandchilds->pid == $childs->fid) {
$html .= '<option value="" class="optionChild">' .$grandchilds->name. '</option>';
}
}
}
}
}
}
$html .= '</select>';
return $html;
}
public function get_forums() {
$this->db->select('*');
$this->db->from('forum');
$this->db->where('pid', '0');
$query = $this->db->get();
return $query->result();
}
public function get_child_forums($fid) {
$this->db->select('*');
$this->db->from('forum');
$this->db->where('pid', $fid);
$query = $this->db->get();
return $query->result();
}
public function has_parent($fid) {
$this->db->select('pid');
$this->db->from('forum');
$this->db->where('fid', $fid);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
}
答案 0 :(得分:1)
您可以在option
方法中直接使用get_forums($pid)
代码。我希望它对你有所帮助。
Forum_model.php
<?php
public function get_forums($pid) {
$forums = '';
$this->db->select('*');
$this->db->from('forum');
$this->db->or_where('pid', $pid);
$query = $this->db->get();
foreach($query->result_array() as $result){
$forums .= '<option value="'.$result['fid'].'" class="optionChild">'.$result['name'].'</option>';
$this->get_forums($result['fid'])
}
return $forums;
}
?>
在查看页面中:
<select name="pid" class="form-control">
<option value="0">None</option>
<?php foreach ($categories as $category) {?>
<option value="<?php echo $category['fid'];?>" class="optionParent"><?php echo $category['name'];?></option>
<?php echo $category['forums']; ?>
<?php } ?>
</select>
答案 1 :(得分:1)
我有一个解决方案。
我从这里得到了这个想法https://gist.github.com/YavorK/5578b4f1b32fe125e0f7eab214270c30
select选项的一些小改动而不是ul。并改为制作图书馆。
<?php
class Forum_select {
private $CI;
public function __construct() {
$this->CI =& get_instance();
}
public function generate($name = 'pid') {
$html = '';
$html .= '<select name="'.$name.'" class="form-control">';
$html .= $this->create_option($this->getforums());
$html .= '</select>';
return $html;
}
function create_option($items, $startingParentId = 0)
{
$htmlOutput = '';
foreach ($items as $item) {
if ($item['pid'] != $startingParentId) {
continue;
}
$htmlOutput .= '<option value="'. $item['fid'] .'">' . $item['name'] . '</option>';
$htmlOutput .= $this->create_option($items, $item['fid']);
}
return $htmlOutput;
}
public function getforums() {
$this->CI->db->select('*');
$this->CI->db->from('forum');
$query = $this->CI->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
}