如何从CodeIgniter中的Controller中将选择框变量传递给View?

时间:2013-03-16 20:50:55

标签: codeigniter variables

我想将一个变量(selectbox id来自数据库)传递给控制器​​的视图,但不知道该怎么做。

3 个答案:

答案 0 :(得分:0)

假设您有一个名为文章的控制器和一个方法索引,那么在您的视图中,您将拥有:

<?php 
echo form_open('article/index');
echo form_input('text');
echo form_close;
?>

在你的控制器中有类似的东西:

public function index() 
{
   if($this->input->post()) { 
      $this_is_catched_text = $_POST['text'];
   }
}

这是没有验证和其他东西。只是你知道它是如何工作的。

答案 1 :(得分:0)

您原来的问题不是很清楚,但在阅读完评论后,您可以这样做。

控制器

//whatever function you're using to populate your original array below.
$data['all'] = $this->model_name->getData();
//then run that data through a foreach loop to populate the dropdown variable.
foreach($data['all'])
{
    $data['idDropDown'][$data['all']['id']]=$data['all']['ad'];
}

这将传递一个数组,如:[455] =&gt;Aliağa,[456] =&gt;Balçova视图为$ idDropDown,原始数据为$ all

然后在视图中使用了CI的表单下拉列表。

echo form_dropdown('id',$idDropDown);

答案 2 :(得分:0)

在你的情况下,它将是这样的

$ilce = array(array("id" => 455, "il_id" => 35,"ad" => "Aliağa"), 
              array("id" => 456, "il_id" => 35, "ad" => "Balçova"));

$options = array();

foreach($ilce as $x) {
    $options[$x['id']] = $x['ad']; 
} 

echo form_dropdown('names', $options);
相关问题