php Codeigniter 从视图中将多个参数传递给控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12504137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter passing multiple parameters to controller from view?
提问by Carl Bembridge
I have a system that is outputting a number of images, with a A link next to them to set them as the album cover.
我有一个正在输出许多图像的系统,它们旁边有一个 A 链接,可以将它们设置为专辑封面。
I have multiple albums, and in my database have a field called "is_feature" that is set to 1 if the image is the cover, and 0 if it isnt.
我有多个相册,并且在我的数据库中有一个名为“is_feature”的字段,如果图像是封面,则设置为 1,如果不是,则设置为 0。
I don't know the best way of selecting the image, i originally outputted something like below;
我不知道选择图像的最佳方式,我最初输出如下内容;
<a href="/admin/set_photo/'.$image_id.'" title="Set this photo as the feature photo">Set</a>
(image_id is the images id obviously), this function would call the model and set all other photos "is_feature" field to 0, and this photos "is_feature" to 1.
(image_id 显然是图像 id),此函数将调用模型并将所有其他照片的“is_feature”字段设置为 0,并将此照片的“is_feature”字段设置为 1。
The problem is it is wiping all the other album features as well. I almost need to pass to variables in the A link, the first being the id of the image, the second being the id of the album, then my model function can only set "is_feature" to 0 where album_id = the id of the album passed.
问题是它也抹去了所有其他专辑功能。我几乎需要传递给A链接中的变量,第一个是图像的id,第二个是相册的id,那么我的模型函数只能将“is_feature”设置为0,其中album_id =相册的id通过。
Is there anyway to pass two variables like this? Or am i going about this in totally the wrong way?
反正有没有像这样传递两个变量?或者我是否以完全错误的方式解决这个问题?
回答by Nishant Jani
You can set the values in the URL as query parameters
您可以将 URL 中的值设置为查询参数
<a href="/admin/set_photo?var1=<?= $image_id;?>&var2=<?= $size;?>"
title="Set this photo as the feature photo"> Set </a>
Which you can retrieve in the controller
您可以在控制器中检索
$image_id = $this->input->get('var1');
$image_size = $this->input->get('var2');
回答by wesside
Uh what? You can pass whatever you need.
呃什么?你可以通过任何你需要的。
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
回答by vivek
Depending upon data typeas string or as array, there are 3 ways of passing data (You can use any of them explained below, BASED upon YOUR REQUIREMENT):
根据字符串或数组的数据类型,有 3 种传递数据的方式(您可以使用下面解释的任何一种,根据您的要求):
Through View
通过视图
//For data you collected through POST method from FORM, collect them as array
$data=array(
'employee_name' => $this->input->post('emp_name'),
'employee_email' => $this->input->post('emp_email'),
'employee_password' => $this->input->post('emp_password')
);
$this->load-> view(''mynextpage", $data)
Through Controller
通过控制器
redirect('controller_name/index'.$valueofcustomer);
OR
redirect(base_url()."controller_name/index/".$valueofcustomer);
//then in your 'view', you can access value of customer like this:
$v_o_c = $this->uri->segment(3);
echo "your value is " .$v_o_c;
Through Session
通过会话
$data = array(
'user_name' => $user_name,
'is_logged_in' => true
);
$this->session->set_userdata($data); //set the session
redirect('another_controller/index');
//then access those in another_controller like this:
$in = $this->session->set_userdata('$data');
Note: Session data will available only for redirect and lost on next page request

