codeigniter views with dynamic parameters
I have a edit view whose url is /group/edit/1 where 1 is the group id which is dynamic.
I am validating the form data in controller as :
if ($this->form_validation->run() == FAL开发者_如何学编程SE)
{
$this->load->view('group/edit', $data);
}
How do I pass the id parameter "1" to this view ?
Below option does not work since the url has to be group/edit/1
$this->load->view('edit', $data);
You're thinking about this wrong. You want to have a view called edit.php and pass the number 1 into it, or perhaps more to the point, you want to get the data for 1 from your model and pass the return value of your model into your view. Consider this:
controller
function edit($id)
{
$data['item_info'] = $this->whateverModel->getItem($id);
$this->load->view('edit', $data);
}
Then in your edit view, you can refer to the data like this:
view
echo $item_info['id'];
echo $item_info['name']; //or whatever you pass back from the model
Is this what you mean?
$this->load->view('group/edit/'.$parameter, $data);
That would make it load your 1
, as you are simply defining the path of the VIEW document
Per CodeIgniters reference this is the format:
$this->load->view('folder_name/file_name');
http://codeigniter.com/user_guide/general/views.html
Not sure if you are confused by routing vs views, here is a quick difference:
If you want http://www.example/group/edit/10 then you need a route setup, not a view.
If you want http://www.example/ and to display the contents from page main.php (in your views folder) then you do $this->load->view('main');
inside your primary controller.
This is what I am doing. Is this acceptable ?
Controller action :
function edit($id)
{
$group_q = $this->db->query("SELECT * FROM groups WHERE id = ?", array($id));
$group_data = $group_q->row();
/* Form fields */
$data['name'] = array(
'name' => 'name',
'id' => 'name',
'value' => $group_data->name,
);
$options = array("A", "B", "C", "D");
$data['group_parent'] = $options;
$data['group_parent_status'] = $group_data->parent_id;
$data['group_id'] = $id;
/* Form validations */
$this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[2]');
if ($this->form_validation->run() == FALSE)
{
if ($this->input->post('submit', TRUE))
{
$data['name']['value'] = $this->input->post('name', TRUE);
$data['group_parent_status'] = $this->input->post('group_parent', TRUE);
}
$this->load->view('group/edit', $data);
}
else
{
$data_name = $this->input->post('name', TRUE);
$data_parent_id = $this->input->post('group_parent', TRUE);
$data_id = $id;
if ( ! $this->db->query("UPDATE groups SET name = ?, parent_id = ? WHERE id = ?", array($data_name, $data_parent_id, $data_id)))
{
$this->session->set_flashdata('error', "Error");
$this->load->view('group/edit', $data);
} else {
$this->session->set_flashdata('message', "Success");
redirect('account');
}
}
return;
}
View file :
<?php
echo form_open('group/edit/' . $group_id); /***** NOTE THIS STEP *****/
echo "<p>";
echo form_label('Name', 'name');
echo "<br />";
echo form_input($name);
echo "</p>";
echo "<p>";
echo form_label('Parentp', 'group_parent');
echo "<br />";
echo form_dropdown('group_parent', $group_parent_active);
echo "</p>";
echo form_hidden('group_id', $group_id);
echo form_submit('submit', 'Submit');
echo form_close();
?>
精彩评论