首页 > 解决方案 > 在代码点火器中,我只想向用户显示分配给当前用户的那些主题的主题列表

问题描述

我只想授予访问权限,这些主题在管理员上传主题时分配给我正在使用主题 id(sid) 的用户。并且通过视图文件,我试图将主题的 sid 与该 m-topic 表中的 a 匹配,因此我将值 og 一个表存储在变量 $rs 和 $ra 上的 secong 中,然后在我的视图文件中使用 if 条件我的代码——

型号代码:

public function Select($Table, $Fields = '*', $Where = 1)
    {
        /*
         *  Select Fields
         */
        if ($Fields != '*') {
            $this->db->select($Fields);
        }
        /*
         *  IF Found Any Condition
         */
        if ($Where != 1) {
            $this->db->where($Where);
        }
        /*
         * Select Table
         */
        $query = $this->db->get($Table);

        /*
         * Fetch Records
         */

        return $query->result();
    }

控制器代码:

public function dashboard()
{
  $data['rs'] = $this->lib_model->Select('v_subject_faculty_mapping', 'id,Subject,fid,sid', array('fid' => $this->session->EmpId, 'status' => 0 ));
  $data['ra'] = $this->lib_model->Select('m_topic', 'id,sid,topic', array('status' => 0 ));
  /*
   * CK Editor
   */
  $path = '../assets/ckfinder';
  $width = '85%';
  //Loading Library For Ckeditor
  $this->load->library('ckeditor');
  $this->load->library('ckfinder');
  //configure base path of ckeditor folder
  $this->ckeditor->basePath = base_url('assets/ckeditor/');
  $this->ckeditor->config['toolbar'] = 'Full';
  $this->ckeditor->config['language'] = 'en';
  $this->ckeditor->config['width'] = $width;
  //configure ckfinder with ckeditor config
  $this->ckfinder->SetupCKEditor($this->ckeditor, $path);

  $this->load->view('f/f_header',$data);
  $this->load->view('f/dashboard');
  $this->load->view('f/f_footer');
}

查看代码:

                <label>Question Topic</label>
                <select class="form-control" required name="S" id="S" style="border: double">
                    <option selected="">Select Topic</option>
                    <?php
                    if ($rs[sid] == $ra[sid]) {
                        # code...

                    foreach ($ra as $r)
                    {
                        ?>
                        <option value="<?=$r->id;?>"><?=$r->topic;?></option>
                        <?php
                    }}
                    ?>
                </select>
            </div>

标签: codeignitercodeigniter-3codeigniter-2

解决方案


您可以使用模型中的连接功能连接这两个表,然后您不必检查分配课程的其他条件。 在模型中试试这个:

$this->db->select('t1.fields,t2.fields');
$this->db->from('user_table t1');
$this->db->join('course_table t2','t2.userid = t1.id'); // you can change the table name as per your database.
if ($Where != 1) {
   $this->db->where($Where);
}
$query = $this->db->get();
return $query->result();

在控制器中只需调用上面在模型中编写的函数:

$data['rs'] = $this->lib_model->Select(array('fid' => $this->session->EmpId, 'status' => 0 ));

鉴于您不需要检查额外的条件,您只需尝试使用 foreach 循环:

<?php  foreach($rs as $r){ ?>

<option value="<?=$r->id;?>"><?=$r->topic;?></option>

<?php } ?>

推荐阅读