我正在使用Laravel在我的数据库中更新多个记录,我将在显示我的设置后尝试描述该问题:
//页面lease_rates.blade.php:
@master('master.admin')
@section('content')
<h1>Edit Lease Rates</h1>
<form method="post" action="{{ URL::current(); }}">
<input type="hidden" name="0" value="0">
<table>
<tr>
<th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th>
</tr>
@foreach($lease_rates as $lease_rate)
@if($lease_rate->classes_letter=='A')
<tr><td>{{$lease_rate->lease_year}}</td>
@endif
<td><input type="text" name="{{ $lease_rate->id }}" value="{{ $lease_rate->rate }}"
size="5" /> {{ $lease_rate->id }}</td>
@if($lease_rate->classes_letter=='E')
</tr>
@endif
@endforeach
</table>
<input type="submit" value="Submit Changes" />
</form>
@endsection
//控制器lease_rates.php:
<?php
class Admin_Lease_Rates_Controller extends Base_Controller {
public $layout = 'master.admin';
public $restful = true;
public function get_index(){
$lease_rates = DB::table('lease_rates_test')->
order_by('lease_year', 'desc')->
order_by('classes_letter', 'asc')->
get();
$this->layout->nest('content','admin.lease_rates', array('lease_rates' => $lease_rates, ));
}
public function post_index(){
foreach(Input::all() as $id => $rate){
DB::table('lease_rates_test')->
order_by('lease_year', 'desc')->
order_by('classes_letter', 'asc')->
where('id', '=', $id)->
update(array('rate' => $rate));
}
return Redirect::to_action('admin');
}
}
?>
//表格结构:
id lease_year classes_letter rate
1 1998 A 10
2 1998 B 20
3 1998 C 30
4 1998 D 40
5 1998 E 50
6 1999 A 60
7 1999 B 70
8 1999 C 80
9 1999 D 90
10 1999 E 100
11 2000 A 110
12 2000 B 120
13 2000 C 130
14 2000 D 140
15 2000 E 150
//视图:
A B C D E
2000 [110]11 [120]12 [130]13 [140]14 [150]15
1999 [60]6 [70]7 [80]8 [90]9 [100]10
1998 [10]1 [20]2 [30]3 [40]4 [50]5
如果我不更改任何值并提交,则第一个和第三个“行”中的值交换如下:
A B C D E
2000 [10]11 [20]12 [30]13 [40]14 [50]15
1999 [60]6 [70]7 [80]8 [90]9 [100]10
1998 [110]1 [120]2 [130]3 [140]4 [150]5
第二次提交没有任何更改将其交换回来。为什么? Someting正在进行提交值的顺序,而不是将$ id与DB中的id列匹配,但我无法弄清楚在哪里。
谢谢大家好好看看!
编辑:显示值的顺序: 0,11,12,13,14,15,6,7,8,9,10,1,2,3,4,5 是它们存储到ID的顺序: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
lease_year和classes_letter的组合是唯一的,所以如果有比较id值的替代方法,我很乐意接受它。
EDIT2:这看起来是一个问题,php不喜欢名字以数字开头的输入项....
EDIT3:这就是我开始工作的方式:
//控制器lease_rates.php:
public function get_index(){
$lease_rates = DB::table('lease_rates_test')->
order_by('lease_year', 'desc')->
order_by('classes_letter', 'asc')->
get();
$this->layout->nest('content','admin.lease_rates', array('lease_rates' => $lease_rates, ));
}
public function post_index(){
$data = $_POST["lease_rates"];
$affected = array();
// update submitted rates!
for($i = 0; $i < count($data); $i++){
$affected[$i] = DB::table('lease_rates_test')->
where('id', '=', $data[$i]["id"])->
update(array('rate' => $data[$i]["rate"]));
}
// set the status message to an error by defult - it will be overwritten in the for loop below
Session::flash("status", "No lease rates updated! There must be a problem!");
// Check that at least one lease_rate was updated
for($k = 0; $k < count($affected); $k++){
if($affected[$k] > 0){
Session::flash("status", "Lease rates updated!");
}
}
return Redirect::to_action('admin.lease_rates@index');
}
//页面lease_rates.blade.php:
@master('master.admin')
@section('content')
<?php
$status = Session::get('status');
if($status){
echo '<div class="alert-box">'.$status.'</div>';
}
?>
<h1>Edit Lease Rates</h1>
<form method="post" action="{{ URL::current(); }}">
<table>
<tr>
<th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th>
</tr>
<?php $i = 0; ?>
@foreach($lease_rates as $lease_rate)
@if($lease_rate->classes_letter=='A')
<tr><td>{{$lease_rate->lease_year}}</td>
@endif
<td>
<input type="hidden" name="lease_rates[{{$i}}][id]" value="{{ $lease_rate->id }}" />
<input type="text" name="lease_rates[{{$i}}][rate]" value="{{ $lease_rate->rate }}" size="5" />
{{ $lease_rate->id }}
</td>
@if($lease_rate->classes_letter=='E')
</tr>
@endif
<?php $i++; ?>
@endforeach
</table>
<input type="submit" value="Submit Changes" />
</form>
@endsection