我正在尝试创建一个编辑页面,其中包含一些复选框,其中的值是从数据库中循环而来的。我尝试了不同的代码组合,但是我似乎在使其正常工作方面遇到了问题,尤其是复选框(复选框是我唯一遇到问题的复选框)。
我的代码发生的事情是......
例如,如果所有的复选框都被选中,而我取消选中第二个和第三个
✓ Dj Lorem ■ Dj Ipsum
■ Dj Dolor ✓ Dj Sit
✓ Dj Amet
提交后是这样的
✓ Dj Lorem ✓ Dj Ipsum
✓ Dj Dolor ■ Dj Sit
■ Dj Amet
选中的内容上升/最后一个复选框总是未选中的,依此类推(如果取消选中 2 个复选框,则最后 2 个复选框未选中)
edit_news.php( View )
Edit News
<?php
$id = $news_id;
$attributes = array('class' => 'form form-horizontal','id'=>'form_news','name'=>'form_news');
echo form_open(base_url() . 'cms/update_news/'.$id, $attributes);
?>
News Title: <input type="text" class="w300" name="news_title" id="news_title" value="<?php echo $news1->news_title; ?>">
<?php
$i=0;
foreach($dj->result() as $d)
{
$q = $cdj->row($i);
?>
<div class="span3">
<label class="checkbox">
<input type="checkbox" name="cats[]" value="<?php echo $d->UID; ?>"<?php if($q->checked=="yes"){echo "checked";}else if($q->checked==""){};?> /> DJ <?php echo $d->dj_name;?>
<input type="hidden" name="dj_id[]" value="<?php echo $d->UID; ?>">
</label>
</div>
<?php
$i=$i+1;
}
?>
<input type="submit" class="btn btn-primary pull-right" value="Save">
</form>
View php 看起来像这样
Edit News
News Title: ___Party at 6____
Choose a DJ
■ Dj Lorem ■ Dj Ipsum
■ Dj Dolor ■ Dj Sit
■ Dj Amet
cms.php( Controller )
public function news_edit($id)
{
$data['dj'] = $this->cms_model->get_dj();
$data['news1'] = $this->cms_model->get_one_news($id);
$data['djnews'] = $this->cms_model->get_djnews($id);
$data['cdj'] = $this->cms_model->get_cdj($id);
$data['id'] = $id;
$this->load->vars($data);
$this->load->view('admin/news_edit');
}
public function update_news($id)
{
$this->cms_model->update_news($id);
redirect(base_url() . "cms/news_edit/".$id);
}
cms_model.php(模型)
function get_dj(){
return $this->db->select()->from("roster")->get();
}
function get_one_news($id){
return $this->db->select()->from("news")->where("UID",$id)->get()->row();
}
function get_cdj($id){
return $this->db->select()->from("news_dj")->where("news_id",$id)->get();
}
function update_news($id)
{
$news_title = $this->input->post("news_title");
$cats = $this->input->post("cats");
$dj_id = $this->input->post("dj_id");
$newdata = array('news_title'=>$news_title
);
$this->db->where('UID', $id);
$this->db->update('news', $newdata);
$i=0;
foreach($dj_id as $d)
{
if ($cats[$i] == ""){$check="no";}
else{$check="yes";}
$dj = array('dj_id'=>$d,'news_id'=>$id,'checked'=>$check);
$this->db->where('dj_id', $d);
$this->db->where('news_id', $id);
$this->db->update('news_dj', $dj);
$i=$i+1;
}
}
新闻(sql)
UID news_title
6 Party at 6
7 Event Cancelled on Monday
8 and so on
花名册(sql)
UID news_title
1 Lorem
2 Ipsum
3 Dolor
4 Sit
5 Amet
news_dj(sql)
dj_id news_id checked
1 6 yes
2 6 yes
3 6 yes
4 6 no
5 6 no
我的想法用完了,我似乎无法修复它
谢谢
最佳答案
你的设置很奇怪。 cats[] 将仅包含被检查的元素,因此检查 $cats[$i] != "" 是否意味着它会被计数有多少被检查并在你的循环中,将分配列出的第一个 x 数量的 DJ。设置 HTML 时,我建议
name="cats[<?php echo $d->UID; ?>]" value=...
这样,它就会按照您在更新循环中期望的方式进行索引。这将是解决问题的最简单方法 - 否则,您可以检查在更新中针对 DJ id 检查的值。但同样,我选择解释如何让它以最快的速度工作 ^^
编辑
如果您不想更改您的 HTML,您可以更改您的支票:
if ($cats[$i] == ""){$check="no";}
else{$check="yes";}
收件人:
$check = (in_array($d, $cats)) ? "yes" : "no";
因为这将查看当前 DJ 是否在提交的复选框值列表中。
关于php - 需要帮助创建可编辑的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15845073/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法