我以前从未使用过 session ,我正在尝试找出处理此问题的最佳方法。我基本上想做的是:
只需一步选择服务
两步选择时间
3步评论和预订
我可以使用 mysql 让它毫无问题地工作。我通常会做的就是在每个步骤之后将信息保存到数据库中,当我进入审核部分时,我将保存所有信息并且没问题。
但是我认为这不是解决这个问题的最佳方法,并且可能会在以后出现问题(如果他们停在第 2 步等等)
我决定尝试 Laravel 4 session ,保存 session 并继续下一步非常容易。然而,当我到达最后一步时,我需要连接 mysql 表以完整显示有关他们预订的信息。我可以使用 session 信息来加入信息吗?我可以使用 session 数据库来保存此信息吗?或者使用不同的表?
我的 Controller 在查看信息后发布:
public function getReview() {
//sets id of user
$user = User::find(Auth::user()->id);
//gets the time and date that they booked from @getSchedule
$scheduler = Session::get('schedule');
//formats time to put in db
$date = strtotime($scheduler['date']);
//same thing as the line above
$dateFormat = date('Y-m-d',$date);
//model to save the schedule
$schedule = new Schedule();
$schedule->userID = $user->id;
$schedule->date = $dateFormat;
$schedule->block = $scheduler['timeslot'];
$schedule->status = "1";
$schedule->save();
//gets the services the user picked from @getServices
$service = Session::get('service');
//saves the services as individual rows in db table
foreach($service as $services)
{
if(!empty($services)) {
$service = new Service();
$service->userID = $user->id;
$service->services = $services;
$service->save();
}
}
return Redirect::to('dashboard');
}
这是 GET 审核页面(我在其中遇到所有 JOINS 问题)
public function showReview() {
$user = User::find(Auth::user()->id);
//show the information and confirm that they want all this crap...if they do..save it and return them to their dashboard
$time = DB::table('bk_schedule')
->leftJoin('bk_timeslot', 'bk_schedule.block', '=', 'bk_timeslot.id')
->where('bk_schedule.id', Auth::user()->id)->first();
$date = strtotime($time->date);
$dateFormat = date('D: F d, Y',$date);
$service = Session::get('service');
$serviceSummary = DB::table('bk_service')
->leftJoin('pr_service', 'pr_service.id', '=', 'bk_service.services')
->where('bk_service.userID', Auth::user()->id)
->get();
$total = DB::table('bk_service')
->leftJoin('pr_service', 'pr_service.id', '=', 'bk_service.services')
->where('bk_service.userID', Auth::user()->id)
->sum('pr_service.price');
return View::make('book.review', array('pageTitle' => 'Booking Appointment Review and Finalize', 'service' => $service, 'date' => $dateFormat,
'time' => $time, 'email' => $user->email, 'serviceSummary' => $serviceSummary, 'total' => $total));
}
是否可以在GET中保存信息,如果不提交到POST则将其删除?我可以使用我的 session 数据并使用我拥有的 MySQL 查询吗?
最佳答案
您不明白 session 是什么,使用您的方法,用户将无法同时填写多个表单(在多个选项卡中打开)。
因此,执行此操作的一般方法是:
- 首页仅显示带有一些字段的 HTML 代码
- 用户选择它们并将数据发布回服务器
- 服务器验证数据并打开另一个包含新字段的 HTML 页面,并使用在步骤 1 中选择的值添加多个“隐藏”字段(当然服务器可以向 page1 显示错误消息)
- 用户发布此表单,服务器可以打开第三个表单,其中新的可见字段和所有以前的字段都存储在隐藏输入中
- 最后,用户将表单发布到最后一页,您可以在其中获得所有先前页面的所有数据。
请注意:另一种方法是在 session 中存储“临时”数据,为此您需要在第二步中获取一些生成的 ID,并将其传递到页面,如前所述
关于php - 如何使用Native session和MySQL连接信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19285100/