[英]Laravel PHP: Selected Category total changes/resets to '0' when using additional pagination pages
I have a Laravel PHP application that allows the user to select a category from a drop down box and after the user makes their selection, the application then retrieves the results via a Form Submission by matching on the selected category. 我有一个Laravel PHP应用程序,该应用程序允许用户从下拉框中选择一个类别,并且在用户做出选择之后,该应用程序然后通过与选定类别匹配的表单提交来检索结果。 I am currently paginating the number of results to 10 per page. 我目前正在将每页10个结果分页。 After results are retrieved, there are 2 totals that are displayed 检索结果后,将显示2个总计
1) The total number of results from all categories. 1)所有类别的结果总数。
2) The number of results from the selected category. 2)所选类别的结果数。
Once the form is submitted, the above totals are displayed correctly but only for the first page of results. 提交表单后,以上总计将正确显示,但仅显示在结果的第一页。 The problem occurs when viewing additional pages of results, for example if there are 15 total records and Category1 from the drop down box contains 12 records , when the user selects this category and then submits the form, the following output will be displayed: 查看结果的其他页面时会发生问题,例如,如果总共有15条记录,并且下拉框中的Category1包含12条记录 ,则当用户选择此类别然后提交表单时,将显示以下输出:
Page 1 第1页
Total Records: 15 总记录:15
Category Results: 12 分类结果:12
Now when using the Laravel pagination links, created in the view using 现在,当使用Laravel分页链接时,在视图中使用
{{ $allpics->appends(array('category' => $category))->links()}}
If the user navigates to Page 2, the following output is displayed: 如果用户导航到页面2,则显示以下输出:
Page 2 第2页
Total Records: 15 总记录:15
Category Results: 0 分类结果:0
I have tried several approaches to fixing this problem by using the ' $_GET[] ' command with ' $_SERVER['QUERY_STRING'] ' to determine if both a ' page ' and ' category ' variable are appended when navigating to additional pages of results so that the number of Category results can then be queried again based on the selected category within the URL itself. 我尝试了几种方法来解决此问题,方法是将' $ _GET [] '命令与' $ _SERVER ['QUERY_STRING'] '一起使用,以确定在导航到以下页面的附加页面时是否同时附加了' page '和' category '变量结果,以便可以根据URL本身中选择的类别再次查询Category结果的数量。 I still cannot get the number of category results to remain at the correct number when navigating to additional pages of results. 导航到结果的其他页面时,我仍然无法使类别结果的数量保持在正确的数量。
Controller: 控制器:
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);
}
}
I have been trying to find a solution to this problem for awhile now and I'm not sure why the category total changes to '0' for additional pages of pagination instead of remaining at the correct category number, in this case '12' even after creating conditions based on the URL being appended with the selected category name. 我已经尝试寻找此问题的解决方案已有一段时间了,我不确定为什么其他分页页面的类别总数变为'0'而不是保持正确的类别编号,在这种情况下甚至是'12'在根据添加了所选类别名称的URL创建条件之后。 Any help is greatly appreciated, thank you! 任何帮助,不胜感激,谢谢!
There's quite a lot of repetition in your code, something like this might be easier to follow and should solve the category issue. 您的代码中有很多重复,类似这样的内容可能更容易理解并且应该解决类别问题。 Can't test it though, 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);
}
By using the solution provided by @chepe263, I modified my controller and view so now the category totals are no longer reset to 0 when navigating to additional pages of results via pagination. 通过使用@ chepe263提供的解决方案,我修改了控制器并查看了内容,现在通过分页导航到其他结果页面时,类别总数不再重置为0。
Controller: 控制器:
$totalCatPics = $pic->where('category', $category);
$_SESSION['totalcatpics'] = $totalCatPics->count();
View: 视图:
<b>Category Results: {{ $_SESSION['totalcatpics'] }}</b><br>
Hope this solution helps anybody else that runs into a similar problem! 希望此解决方案可以帮助遇到类似问题的其他任何人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.