我有一个填充的 CodeIgniter form_multiselect。里面有值
代码如下:
<select multiple="multiple" name="EMPLOYEES_id[]">
<option value="8">Employee 8</option>
<option value="1">Employee 1</option>
<option value="12">Employee 12</option>
</select>
这在您可以向多个用户发送消息的系统上运行。所以我想做的是说用户选择值 1 和值 8。我想使用相同的数据运行两次插入,但唯一的区别是数据库中的 EMPLOYEES_id 字段每次都不同。
我的模型如下:
function create()
{
$this->template->set('title', 'Create Message');
// Validate the form input
$this->form_validation->set_error_delimiters('<span class="help-inline">', '</span>');
$this->form_validation->set_rules('MESSAGE_summary', 'Message Summary', 'trim|required|xss_clean');
$this->form_validation->set_rules('MESSAGE_body', 'Message Body', 'trim|required|xss_clean');
if($this->form_validation->run() == FALSE)
{
// Get all users in same company as logged in user
$comapny_contacts = $this->Employees_model->read_company_contacts($this->viewinguser->BUSINESSES_id);
$comapny_contacts = $comapny_contacts->result();
foreach($comapny_contacts as $contacts)
{
$comapny_contacts_options[$contacts->EMPLOYEES_id] = $contacts->EMPLOYEES_firstname . ' ' . $contacts->EMPLOYEES_surname;
}
$this->template->set('comapny_contacts_options', $comapny_contacts_options);
// form has not been run or there are validation errors
$this->template->build('messages/create');
}
else
{
/*
* Lets create the new message in the database
*/
$insert_data = new stdClass();
$insert_data->MESSAGES_summary = $this->input->post('MESSAGE_summary');
$insert_data->MESSAGES_body = $this->input->post('MESSAGE_body');
$insert_data->MESSAGES_level = $this->input->post('MESSAGES_level');
$insert_data->MESSAGES_type = $this->input->post('MESSAGES_type');
$insert_data->MESSAGES_createdby = $this->activeuser->EMPLOYEES_id;
//$insert_data->EMPLOYEES_id = $this->input->post('EMPLOYEES_id');
foreach($this->input->post('EMPLOYEES_id') as $employee_id)
{
$insert_data->EMPLOYEES_id = $employee_id;
// Save to new message in the database
$this->Messages_model->create($insert_data);
}
/*
* Now lets save a notification email in the database to be sent to the user
*/
$notification_body = '
<p>A new message has been added for you by ' . $this->session->userdata('activeuser')->EMPLOYEES_firstname . '<br />
------------------------------</p>
<p><strong>Summary:</strong> ' . $this->input->post('MESSAGE_summary') . '</p>
<p>------------------------------<br />
<a class="btn" href="' . base_url('messages') . '">Click to view full messages</a>';
// Get the info for the user who the notification is for
$notify_user_info = $this->Employees_model->read_single_switch($this->input->post('EMPLOYEES_id'));
$notify_user_info = $notify_user_info->result();
// Set all the data
$insert_data = new stdClass();
$insert_data->NOTIFICATIONS_toname = $notify_user_info[0]->EMPLOYEES_firstname . ' ' . $notify_user_info[0]->EMPLOYEES_surname;
$insert_data->NOTIFICATIONS_toemail = $notify_user_info[0]->EMPLOYEES_email;
$insert_data->NOTIFICATIONS_subject = 'A new message has been added for you';
$insert_data->NOTIFICATIONS_htmlbody = $notification_body;
//$insert_data->EMPLOYEES_id = $this->input->post('EMPLOYEES_id');
foreach($this->input->post('EMPLOYEES_id') as $employee_id)
{
$insert_data->EMPLOYEES_id = $employee_id;
// Save to new notification to the database
$this->Notifications_model->create($insert_data);
}
// show user confirmation
$this->session->set_flashdata('success_message', 'New message created successfully.');
redirect('messages');
}
最佳答案
最简单的方法是设置所有通用参数,然后在每个 foreach 上插入数据$this->input->post('EMPLOYEES_id') 的迭代.
foreach($this->input->post('EMPLOYEES_id') as $eid){
// insert data using $uid as the employer id here
}
关于php - CodeIgniter 表单多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10721733/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我在事件管理员编辑页面中有嵌套资源,但我只想允许管理员编辑现有资源的内容,而不是添加新的嵌套资源。我的代码看起来像这样:formdo|f|f.inputsdof.input:authorf.input:contentf.has_many:commentsdo|comment_form|comment_form.input:contentcomment_form.input:_destroy,as::boolean,required:false,label:'Remove'endendf.actionsend但它在输入下添加了“添加新评论”按钮。我怎样才能禁用它,并只为主窗体保留f.ac
我目前正在尝试将ERB布局转换为HAML。这是我不断收到的错误:index.html.haml:18:syntaxerror,unexpected')'));}\n#{_hamlout.format_...这是HAML页面:.row-fluid.span6%h2TodoList.span6%h2{:style=>"text-align:right;"}document.write(today)%hr.divider.row-fluid.span6%h2.small_headNewTask=render:partial=>'layouts/form_errors',:locals=>{:
为现有模型生成单个文件(_form.html.erb)的命令是什么?在Rails3中工作。谢谢。 最佳答案 这听起来可能很傻,但请听我说完……当我想开始清洁时,我自己也做过几次这样的事情。以下是一个脚本,它将读取您的模式并生成必要的生成命令来重现它:require'rubygems'require'active_support/core_ext'schema=File.read('db/schema.rb')schema.scan(/create_table"(\w+)",.*?\n(.*?)\nend/m).eachdo|name
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
我不想显示表单,但前提是当前页面不是主页这是我目前所拥有的...我有我的路线设置:root'projects#index'我的看法:'projects',:action=>'index'))%>showsomestuff如果url是localhost:3000/projects,则不会显示但是它显示了它的localhost:3000所以我需要以某种方式确保它不会显示主页。另外,我有主页的搜索参数,但我仍然不想显示它是否像localhost:3000/projects?search=blahblahblah 最佳答案 使用root_p
我正在开发一个Rails2.3.1网站。在整个网站中,我需要一个用于在各种页面(主页、创建帖子页面、帖子列表页面、评论列表页面等)上创建帖子的表单——只要说这个表单需要在由各种Controller)。这些页面中的每一个都显示在相应的Controller/操作中检索到的各种其他信息。例如,主页列出了最新的10篇文章、从数据库中提取的内容等。因此,我已将帖子创建表单移动到它自己的部分中,并将该部分包含在所有必要的页面中。请注意,部分POST中的表单到/questions(路由到PostsController::create——这是默认的Rails行为)。我遇到的问题是当Posts表单没有正
是否有集成Postgresrangetypes的标准方法?使用Rails表单助手?我基本上需要一个最小和最大字段,它们在保存时转换为一个范围。有什么想法吗? 最佳答案 起初我在想这样的事情:classModeldelegate:begin,:end,to::range,prefix:true,allow_nil:true#Replace:rangewithyourfieldnameend获取方法:range_begin,range_end。我检查了文档,这些方法是只读的。所以你还需要二传手:classModeldelegate:be
我正试图在Rails中获得完整的页面缓存,但我在CSRF方面遇到了很大的障碍——或者可能只是我对它的理解。我目前有form_authenticity_token存储在cookie中的字符串,JS可以使用该cookie访问和重写header标签。我在生成的HTML中有两个地方可以找到标记:1)在头部2)在表单的隐藏输入元素中如前所述,这些哈希值彼此不同(在未启用缓存的开发模式下)。他们为什么不同?为什么我可以删除headmeta标签并保留表单输入并且允许请求?然而,当我删除表单输入标签并保留标题时,请求被拒绝了吗?实际上这意味着head标签是无用的,不是吗?我可以将表单输入标签重写为