在数据库列中存储数组

时间:2016-05-22 03:48:28

标签: php mysql laravel laravel-5

我在视图中有这个表单:

Your Name: <input type="name" name="name" id="name" ><br><br>
Your Contact No. <input type="contact" name="contact" id="contact" ><br><br>
Todays date:<mark>{{$mytime->format('Y-m-d, l')}}</mark><br><br>
<br><br>BOOK SEATS:<br>
@foreach($seats as $seat)
@if($seat->available=='1')
<input type="checkbox" name="check[]" value="{{$seat->book}}" >{{$seat->book}}<br><br>
 @else
<input type="checkbox" name="check[]" value="{{$seat->book}}" disabled>{{$seat->book}}<br><BR>
@endif
@endforeach
<button type="submit" class="btn btn-default">Book</button>
</form>

此表单重定向到此控制器,该控制器插入名称联系人以及使用循环选中复选框的值。这样,如果我检查两个值并提交表单,由于for循环,将插入两行name contact,但具有不同的booked_seatsid(i用id作为自动增量)。有没有办法将选中的值插入到只有一行的scheduled_seats中。或者对于当时插入的任何行(选中的值)使id相同。

 public function book(Request $request)
  {
    $name=$request->get('name');
    $contact=$request->get('contact');
    $check=$request->get('check');
    $totalcheckboxchecked=sizeof('$check'); 
for($i=0;$i<=$totalcheckboxchecked;$i++)
{
    if (array_key_exists($i,$check) )
{   
    $booked=$check[$i];
    $book=bus::insert(['name'=>$name,'contact'=>$contact, 'booked_seats'=>$booked, 'active'=>'1']);
    seats::where('book',$booked)->update(['available'=>'0']);

}
}
        return redirect('/bus')
            ->with('message','booked successfully!!!');

}

1 个答案:

答案 0 :(得分:1)

您可以使用序列化

将检查的值存储在数据库中
#include<stdio.h>

int main()
{
    int i;
    for(i = 0; i <= 6; i++)
    {
         printf("%c\n", i);
    }
}

然后你可以像这样保存在数据库中而不使用for循环

$booked = serialize ($check);

要检索值,请使用unserialize

$book=bus::insert(['name'=>$name,'contact'=>$contact, 'booked_seats'=>$booked, 'active'=>'1']);
    seats::where('book',$booked)->update(['available'=>'0']);

这是完整的代码。

unserialize($booked);