使用php和mysql生成的无序子列表的输出
我成功地创建了一个无序列表,其中包含数据库中父表中的值。每个父列表都应该有一个子无序列表,这取决于它在子表中是否有值。我创建了一个类来实现这一点,但是,子列表似乎破坏了父无序列表的输出。我一直不明白为什么!Am发布基于父id打印父列表和子列表的方法的代码;我怀疑错误来自我的foreach(s),但不确定,我可以发布整个代码,但不确定这是否正确使用php和mysql生成的无序子列表的输出,php,mysql,html-lists,Php,Mysql,Html Lists,我成功地创建了一个无序列表,其中包含数据库中父表中的值。每个父列表都应该有一个子无序列表,这取决于它在子表中是否有值。我创建了一个类来实现这一点,但是,子列表似乎破坏了父无序列表的输出。我一直不明白为什么!Am发布基于父id打印父列表和子列表的方法的代码;我怀疑错误来自我的foreach(s),但不确定,我可以发布整个代码,但不确定这是否正确 public function construct_list($type,$parent = true){ if($parent){
public function construct_list($type,$parent = true){
if($parent){
$this->load_parent_array = $this->load_parent_list();
switch($type){
case 'ul':
$this->list_string = '';
foreach($this->load_parent_array as $o_keys => $o_vals):
$this->list_string .= '<li>';
foreach($o_vals as $k_vals => $_vals):
if($k_vals === 'parent_id'){
$this->list_string .= '
<label class="inline-label">
<input data-toggle="child-id-'.$_vals.'" type="checkbox" name="parent_cat[]" value="'.$_vals.'" class="unicorn" />
';
}
if($k_vals === 'parent_category_names'){
$this->list_string .= '<span class="check-label">'.$_vals.'</span>
</label>';
}
//Inner Unordered list code
$this->load_child_array = $this->load_child_list($_vals);
if(!empty($this->load_child_array)){
if($k_vals === 'parent_id'){
$this->list_string .= '<ul id="child-id-'.$_vals.'" class="child-category">';
foreach($this->load_child_array as $o_keys => $o_vals):
$this->list_string .= '<li>';
foreach($o_vals as $v => $f){
if($v === 'child_category_name'){
$this->list_string .= '<label class="inline-label">
<input type="checkbox" name="child_category[]" value="'.$f.'" />
<span class="check-label">'.$f.'</span>
</label>';
}
}
$this->list_string .= '</li>';
endforeach;
$this->list_string .= '</ul>';
}
}
endforeach;
$this->list_string .= '</li>';
endforeach;
break;
case 'select':
$this->list_string = '<li>--Parent Category--</li>';
foreach($this->load_parent_array as $o_keys => $o_vals):
foreach($o_vals as $k_vals =>$_vals):
#$this->list_string .= '<option value="';
if($k_vals === 'parent_category_slug'){
$this->list_string .= '<option value="'.$_vals.'">';
}
if($k_vals === 'parent_category_names'){
$this->list_string .= $_vals.'</option>';
}
endforeach;
endforeach;
break;
}
}else{
}
return $this->list_string;
}
公共函数构造列表($type,$parent=true){
如果($parent){
$this->load_parent_array=$this->load_parent_list();
交换机($类型){
案例“ul”:
$this->list_string='';
foreach($this->load_parent_array as$o_keys=>$o_vals):
$this->list_string.='';
foreach($o_vals as$k_vals=>$):
如果($k_vals=='parent_id'){
$this->list_string.='
';
}
如果($k_vals==='parent_category_names'){
$this->list\u string.=''.$\u vals
';
}
//内部无序列表代码
$this->load\u child\u array=$this->load\u child\u list($\u vals);
如果(!empty($this->load_child_array)){
如果($k_vals=='parent_id'){
$this->list_string.='”;
foreach($this->load_child_array as$o_keys=>$o_vals):
$this->list_string.='- ';
foreach($v=>f){
如果($v==‘子类名称’){
$this->list_string.='
“.$f.”
';
}
}
$this->list_string.='
';
endforeach;
$this->list_string.='
';
}
}
endforeach;
$this->list_string.=' ';
endforeach;
打破
案例“选择”:
$this->list_string='--父类别-- ';
foreach($this->load_parent_array as$o_keys=>$o_vals):
foreach($o_vals as$k_vals=>$):
#$this->list_string.='