我正在尝试通过从下拉菜单中选择一个选项来动态查询我的数据库,并使用GET方法将其值分配回控制器中的变量,然后在模型中使用,但我不确定如何去做。
我的代码如下
视图 - 保留下拉菜单
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Latest Hurling Fixtures<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="<?php echo site_url('user/get_fixtures?fixture_type=Hurling&fixture_level=Roinn 1A') ?>">Roinn 1A</a></li>
<li class="active"><a href="<?php echo site_url('user/get_fixtures?fixture_type=Hurling&fixture_level=Roinn 1B') ?>">Roinn 1B</a></li>
<li><a href="<?php echo site_url('user/get_fixtures?fixture_type=Hurling&fixture_level=Roinn 2A') ?>">Roinn 2A</a></li>
<li><a href="<?php echo site_url('user/get_fixtures?fixture_type=Hurling&fixture_level=Roinn 2B') ?>">Roinn 2B</a></li>
</ul>
</li>
我正在尝试将值"投掷"和"Roinn 1A"等(取决于选择的菜单项)分配回控制器中"get_fixtures"函数中的变量,称为"用户"
get_fixtures function
public function get_fixtures()
{
$data['title']= 'Hurling';
$this->load->model('fixture_model');
$fixture_round =$_GET['fixture_type'];
$fixture_level =$_GET['fixture_level'];
$data['results'] = $this->fixture_model->fixtures();
$this->load->view('include/header',$data);
$this->load->view('pages/hurling_view.php', $data);
$this->load->view('include/footer',$data);
}
然后,我想在查询数据库的模型中使用这些变量。
型
function fixtures()
{
//Query the fixture table for every record and row
$results = array();
$this->db->select('tf.*, t1.*, fixture.*, venue.*, t2.team_id as team_id_2, t2.team_name as team_name_2, t2.team_logo as team_logo_2, fixture_text, fixture_type, fixture_comp, fixture_date, fixture_level');
$this->db->from('team_has_fixture as tf');
$this->db->join('team as t1', 't1.team_id = tf.team_team_id');
$this->db->join('team as t2', 't2.team_id = tf.team_team_id2');
$this->db->join('fixture', 'tf.fixture_fixture_id = fixture.fixture_id');
$this->db->join('venue', 'fixture_venue_id = venue.venue_id');
$this->db->where('fixture_type', $fixture_type);
$this->db->where('fixture_level', $fixture_level);
$query = $this->db->get();
//echo $this->db->last_query(); // Used this to see what my latest query contained
if($query->num_rows() > 0)
{
$results = $query->result();
}
//var_dump($query->result()); // used this to see that the results of my query contained
return $results;
}
}
当我运行它时,我收到未定义的变量错误。
如果在配置文件中启用它:$config['enable_query_strings'] = TRUE;
你可以像这样使用 get() 方法
$this->input->get('param_name');
所以在你的例子中,也许它会是
$fixture_round = $this->input->get('fixture_type');
经过更多的挖掘,我发现以下内容对我有用。
步骤1 - 在应用程序文件夹的配置文件中更改URI协议。
从这里更改它
$config['uri_protocol'] = 'AUTO';
对此
$config['uri_protocol'] = "PATH_INFO";
步骤 2 - 将以下内容添加到控制器中的构造方法
//Allow querystrings for this constructor
parse_str($_SERVER['QUERY_STRING'],$_GET);
步骤3 - 我在视图中为我的下拉菜单使用了以下代码 - 下拉菜单项从我的控制器调用一个名为"get_fixtures"的方法,称为"用户"
视图
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Latest Hurling Fixtures<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="<?php echo site_url('user/get_fixtures?fixture_type=Hurling&fixture_level=Roinn 1A') ?>">Roinn 1A</a></li>
<li><a href="<?php echo site_url('user/get_fixtures?fixture_type=Hurling&fixture_level=Roinn 1B') ?>">Roinn 1B</a></li>
<li><a href="<?php echo site_url('user/get_fixtures?fixture_type=Hurling&fixture_level=Roinn 2A') ?>">Roinn 2A</a></li>
<li><a href="<?php echo site_url('user/get_fixtures?fixture_type=Hurling&fixture_level=Roinn 2B') ?>">Roinn 2B</a></li>
<li><a href="<?php echo site_url('user/hurl_all_ireland') ?>">All Ireland Championship</a></li>
</ul>
</li>
我的控制器中的get_fixtures函数包含以下内容
public function get_fixtures()
{
//I used the commented out lines below to test that I was getting a value back from the GET in my dropdown menu
//$fixture_type = $_GET['fixture_type'];
//$fixture_level = $_GET['fixture_level'];
//echo $fixture_type;
//echo $fixture_level;*/
$data['title']= 'Hurling';
$this->load->model('fixture_model');
$data['results'] = $this->fixture_model->fixtures();
$this->load->view('include/header',$data);
$this->load->view('pages/hurling_view.php', $data);
$this->load->view('include/footer',$data);
}
步骤 4 - 最后,在查询数据库之前,我在模型中设置变量
型
function fixtures()
{
//Query the fixture table for every record and row
// Set the variables to the values selected from the drop down menu
$fixture_type = $_GET['fixture_type'];
$fixture_level = $_GET['fixture_level'];
$fixture_round = "Round 3";
//$fixture_level = "Roinn 1A";
$results = array();
$this->db->select('tf.*, t1.*, fixture.*, venue.*, t2.team_id as team_id_2, t2.team_name as team_name_2, t2.team_logo as team_logo_2, fixture_text, fixture_type, fixture_comp, fixture_date, fixture_level');
$this->db->from('team_has_fixture as tf');
$this->db->join('team as t1', 't1.team_id = tf.team_team_id');
$this->db->join('team as t2', 't2.team_id = tf.team_team_id2');
$this->db->join('fixture', 'tf.fixture_fixture_id = fixture.fixture_id');
$this->db->join('venue', 'fixture_venue_id = venue.venue_id');
$this->db->where('fixture_type', $fixture_type);
$this->db->where('fixture_round', $fixture_round);
$this->db->where('fixture_level', $fixture_level);
$query = $this->db->get();
//echo $this->db->last_query(); // Used this to see what my latest query contained
if($query->num_rows() > 0)
{
$results = $query->result();
}
//var_dump($query->result()); // used this to see that the results of my query contained
return $results;
}
它非常适合我:)