Laravel PHP:使用其他分页页面时,所选类别总数更改/重置为“0”
我有一个Laravel PHP应用程序,它允许用户从下拉框中选择一个类别,在用户做出选择后,应用程序通过表单提交通过匹配所选类别来检索结果。我目前分页的结果数量为每页10。检索结果后,将显示2个总计 1所有类别的结果总数。 2所选类别的结果数 提交表格后,将正确显示上述总计,但仅显示结果的第一页。查看结果的其他页面时会出现问题,例如,如果下拉框中共有15条记录,类别1包含12条记录,则当用户选择此类别然后提交表单时,将显示以下输出: 第1页 总记录:15 类别结果:12 现在,当使用Laravel分页链接时,使用 如果用户导航到第2页,将显示以下输出: 第2页 总记录:15 类别结果:0 我已经尝试了几种方法来解决这个问题,方法是使用“$\u GET[]”命令和“$\u SERVER['QUERY\u STRING']'以确定在导航到结果的其他页面时是否同时添加了“页面”和“类别”变量,以便可以根据URL本身中选定的类别再次查询类别结果的数量。当导航到其他结果页面时,我仍然无法使类别结果的数量保持在正确的数量 控制器:Laravel PHP:使用其他分页页面时,所选类别总数更改/重置为“0”,php,laravel-4,Php,Laravel 4,我有一个Laravel PHP应用程序,它允许用户从下拉框中选择一个类别,在用户做出选择后,应用程序通过表单提交通过匹配所选类别来检索结果。我目前分页的结果数量为每页10。检索结果后,将显示2个总计 1所有类别的结果总数。 2所选类别的结果数 提交表格后,将正确显示上述总计,但仅显示结果的第一页。查看结果的其他页面时会出现问题,例如,如果下拉框中共有15条记录,类别1包含12条记录,则当用户选择此类别然后提交表单时,将显示以下输出: 第1页 总记录:15 类别结果:12 现在,当使用Larave
我已经尝试找到解决此问题的方法一段时间了,我不确定为什么对于其他分页页面,类别总数更改为“0”,而不是保留在正确的类别编号上,在本例中为“12”,即使是在根据附加了所选类别名称的URL创建条件之后。非常感谢您的帮助,谢谢 通过使用@chepe263提供的解决方案,我修改了我的控制器和视图,因此现在通过分页导航到其他结果页面时,类别总数不再重置为0 控制器: 视图:
希望此解决方案能帮助其他遇到类似问题的人 代码中有很多重复,类似这样的代码可能更容易理解,应该可以解决类别问题。但无法测试,obvs
public function index()
{
$totalVideos = Video::count();
$totalPictures = Picture::count();
$vdo = Video::query();
$pic = Picture::query();
if ($category = Input::get('category')) {
$vdo->where('category', $category);
$pic->where('category', $category);
$totalCatPics = Picture::where('category', $category)->count();
$totalCatVids = Video::where('category', $category)->count();
} else {
$totalCatPics = $totalPictures;
$totalCatVids = $totalVideos;
}
$allvids = $vdo->paginate(10);
$allpics = $pic->paginate(10);
$data = compact('allvids','allpics', 'totalCatVids', 'totalCatPics', 'totalVideos', 'totalPictures', 'url_page');
$data['category'] = $category;
$this->layout->content = \View::make('home.picsvids.pics_vids_overview', $data);
}
在第一个if中,您将为类别指定一个值,而不是比较值。//您可以在中保存类别的值session@chepe263在会话中保存category的值成功了!我将用您的解决方案编辑上面的答案。非常感谢你!请看下面我的答案。您的代码运行完美,而且非常整洁!谢谢你的回答!
public function index()
{
$vdo = Video::query();
$pic = Picture::query();
if($category = Input::get('category')){
$vdo->where('category', $category);
$pic->where('category', $category);
/* Old code */
// $urlcategory = $_GET['category'];
$urlcategory = $_SERVER['QUERY_STRING'];
$url_cat_page = parse_str($urlcategory);
$url_page = $category;
$totalCatVids = $vdo->where('category', $category);
$totalCatPics = $pic->where('category', $category);
$allvids = $vdo->paginate(10);
$allpics = $pic->paginate(10);
$totalVideos = Video::all();
$totalPictures = Picture::all();
$data = compact('allvids','allpics', 'totalCatVids', 'totalCatPics', 'totalVideos', 'totalPictures', 'url_page');
$data['category'] = Input::get('category');
$this->layout->content = \View::make('home.picsvids.pics_vids_overview', $data);
}
else if (isset($_GET['category']) && $_GET['category'] == $category)
{
$vdo->where('category', $category);
$pic->where('category', $category);
$totalCatVids = $vdo->where('category', $category);
$totalCatPics = $pic->where('category', $category);
$allvids = $vdo->paginate(10);
$allpics = $pic->paginate(10);
$totalVideos = Video::all();
$totalPictures = Picture::all();
$data = compact('allvids','allpics', 'totalCatVids', 'totalCatPics', 'totalVideos', 'totalPictures', 'url_page');
$data['category'] = Input::get('category');
$this->layout->content = \View::make('home.picsvids.pics_vids_overview', $data);
}
else
{
$urlcategory = '';
$url_page = $category;
$totalCatVids = Video::all();
$totalCatPics = Picture::all();
$allvids = $vdo->paginate(10);
$allpics = $pic->paginate(10);
$totalVideos = Video::all();
$totalPictures = Picture::all();
$data = compact('allvids','allpics', 'totalCatVids', 'totalCatPics', 'totalVideos', 'totalPictures', 'url_page');
$data['category'] = Input::get('category');
$this->layout->content = \View::make('home.picsvids.pics_vids_overview', $data);
}
}
$totalCatPics = $pic->where('category', $category);
$_SESSION['totalcatpics'] = $totalCatPics->count();
<b>Category Results: {{ $_SESSION['totalcatpics'] }}</b><br>
public function index()
{
$totalVideos = Video::count();
$totalPictures = Picture::count();
$vdo = Video::query();
$pic = Picture::query();
if ($category = Input::get('category')) {
$vdo->where('category', $category);
$pic->where('category', $category);
$totalCatPics = Picture::where('category', $category)->count();
$totalCatVids = Video::where('category', $category)->count();
} else {
$totalCatPics = $totalPictures;
$totalCatVids = $totalVideos;
}
$allvids = $vdo->paginate(10);
$allpics = $pic->paginate(10);
$data = compact('allvids','allpics', 'totalCatVids', 'totalCatPics', 'totalVideos', 'totalPictures', 'url_page');
$data['category'] = $category;
$this->layout->content = \View::make('home.picsvids.pics_vids_overview', $data);
}