php - 选择一个选项的递归选择php codeigniter
问题描述
我有递归函数,它在我的选择下拉列表中填充数据。
我如何传递一个值,如果它与下拉列表中的任何值匹配,就会被选中?
这是我的模型上的功能:
function get_all_cat($parent,$level=0,$s){
$ui = '';
$this->db->order_by($this->id, $this->order);
$this->db->where('parent',$parent);
$query=$this->db->get($this->table)->result();
foreach ($query as $qu) {
if($s==$qu->id){
$y = 'selected';
}
$repeat = str_repeat('--', $level);
$ui .= '<option value="'. $qu->id .'" '.$y.' >' .$repeat. $qu->title.'</option>';
$new_level = $level+1;
$ui .= '' .$this->get_all_cat($qu->id,$new_level) .'';
}
return $ui;
}
这是我在视图中的选择选项:
<div class="form-group">
<select name="category" class="form-control chosen-select">
<option value="">Any Category</option>
<?php
$cat = $this->Dbc_categories_model->get_all_cat(0,0,$this-session->userdata('clicked_id');
echo $cat;
?>
</select>
</div>
解决方案
我捡到的几张纸条……
1.) 永远不要使用像 $s => 容易迷路的变量名称,并且该名称不会告诉您变量包含或做什么
2.)您没有将选定的值传递给隐蔽函数(缺少第三个参数)
否则看起来这是工作代码。
推荐阅读
- c - 使用 C 在 OpenMP 中执行后合并数据以打印
- reactjs - 如何使用钩子处理异步 Firebase 调用
- java - 在启动 Spring Boot Scheduler 之前如何进行一些预处理?
- ubuntu-18.04 - 神秘的共享文件夹
- c++ - C++ GDK3.0 这些像素值是什么?
- java - 是否有像“必须避免”这样的 javadoc 标签?
- python - 如何使用简单的基本脚本测试 pySerial?
- java - 无法使用 java 连接到 gmail smtp 套接字,但 telnet 可以访问套接字
- python - 难以创建 XLSM 文件
- javascript - 为数组的每个对象创建一个编辑函数