php - 如何将我的数据库特定列值插入 View 页面中的列表

标签 php html codeigniter

我想将“category_name”列到列表中。我试过了,但我做不到。

这是我的 Controller ,我正在尝试 2 调用 fn get_cat。 然后我希望将该数组传递到我的 View 页面

Controller 分类列表

 public function get_categorylist() 
 {
  $data['records']= $this->categorylist_model->get_cat();
  $this->load->view('categorylist_view',$data);  

 }

模型 Categorylist_model

public function get_cat()
 {

 $this->db->select('category_name');
 $this->db->from('amm_report_category');
 $query = $this->db->get();
       if($query->num_rows() == 1) 
       { 
        return $query->result();
       }
        else
        {
        return false;
        }

    }     

查看categorylist_view

<body>
<div>
<form action="<? echo base_url(); ?>categorylist/get_categorylist"  method="post">
  <select name="categorylist">
  <? foreach ($result as $var) 
  {?> 
 <option value="<?echo $var->category_name;?>"><?echo $var->category_name;?>       </option>
  <?}?>
  </select>

  <p><input type="submit" value="get_categorylist" name=""/></p>                                         
  </form>
  </div>
  </center>
  </body>

最佳答案

模型

  public function get_cat()
 {

   $this->db->select('catogory_id,category_name');
   $this->db->from('amm_report_category');
   $query = $this->db->get();
   if($query->num_rows() == 1) 
   { 
    return $query->result();
   }
    else
    {
    return false;
    }

} 

查看

     <select name="categorylist">
     <? foreach ($records as $var) 
     {?> 
     <option value="<?echo $var->category_id;?>"><?echo $var->category_name;?>       </option>
     <?}?>
     </select>       

关于php - 如何将我的数据库特定列值插入 View 页面中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31017767/

相关文章:

mysql - phpass 将输入密码与存储的密码进行匹配

javascript - 如何在这篇文章中创建全局变量?

php - 随机且唯一的 Mysql 选择查询

html - 在 CSS Grid 1 行多列的行元素中

jquery - 调用添加表单时无法自动填充 id 字段

html - 设计可重复使用的 Polymer 元素的策略

html - 通过实际 z-index 比较 HTML 元素

php - 使用 PHP 设置百分比格式

php - 使用 PHP 语言将 URL 中的所有空格和特殊符号替换为破折号

php - 用UTF-8编码解决BOM