在thinkphp中解决使用分页类时,最后一页全部删除后跳转到上一页时出现的bug

本处理方法同样适用于在多条件搜索时删除出现的bug情况,放心使用。(摘自本机Exam考试系统)

 

html代码段:

 1 <table class="table">
 2     <thead>
 3         <tr>
 4             <th>所属分类</th>
 5             <th>问题</th>
 6             <th>正确答案</th>
 7             <th>创建时间</th>
 8             <th>操作</th>
 9         </tr>
10     </thead>
11     <tbody>
12         <foreach name="subject" item="vo">
13             <tr>
14                 <td class="td">{$vo.cate}</td>
15                 <td>{$vo.question}</td>
16                 <td>{$vo.answer}</td>
17                 <td>{$vo.createtime}</td>
18                 <td>
19                     <a href="__CONTROLLER__/check_answer?id={$vo.id}">查看回答</a>|
20                     <a href="__CONTROLLER__/subject_edit?id={$vo.id}">修改</a>|
21                     <a href="__CONTROLLER__/subject_dele?id={$vo.id}">删除</a>
22                 </td>
23             </tr>
24         </foreach>
25     </tbody>
26 </table>

 

 

js代码段:

 1 <script type="text/javascript">
 2   $(document).ready(function(){
 3     if($(".td").length == 0)
 4        {
 5          $.ajax
 6           ({
 7               type:"post",
 8               url : "__CONTROLLER__/subject_dele",
 9               data : {
10                         page:'endPage'
11                      },
12                dataType:"json",    
13                success:function(msg)
14                {
15                    if(msg.countTable == "exist")
16                     {
17                        window.location.replace("http://"+msg.href);
18                     }
19                }
20           });
21         }
22   });
23 </script>

 

 

 

php代码段:

 1   public function subject_dele()
 2     {
 3         $subjectDB = M('subject');
 4         $recommend_anwser_DB = M('recommend_anwser');
 5 
 6         //【重点】删除跳转页面 Start
 7         $pageStr = strstr($_SERVER['HTTP_REFERER'],"subject_list/");
 8         $pageStr_no_condition = substr($pageStr, strlen('subject_list/'));
 9 
10         $countTable = $subjectDB->count(); //判断是否是空表
11         if($countTable == 0)
12         {
13             $ajaxInfo['countTable'] = 'empty';
14         }
15         else
16         {
17             $ajaxInfo['countTable'] = 'exist';
18         }
19 
20         if($_POST['page'] == 'endPage')
21         {
22             //兼顾多条件查询且页面处于最后一页,则拼接在删除后要跳转的URL Start
23             $p_num = substr($pageStr_no_condition, strlen('/p/')+strpos($pageStr_no_condition, '/p/'),(strlen($pageStr_no_condition) - strpos($pageStr_no_condition, '.'))*(-1));
24             $p_num = intval($p_num) - 1;
25 
26             $pageStr_arr_1 = explode(".", $pageStr_no_condition);
27             $pageStr_arr_2 = explode("/", $pageStr_arr_1[0]);
28             for ($i=0; $i < count($pageStr_arr_2)-1; $i++) 
29             { 
30                $pageStr_new .= $pageStr_arr_2[$i]."/";
31             }
32 
33             $pageStr_condition = $pageStr_new.$p_num.".html";
34             $ajaxInfo['href'] = $_SERVER['SERVER_ADDR'].__MODULE__.'/'.CONTROLLER_NAME."/subject_list/".$pageStr_condition;
35             $this->ajaxReturn($ajaxInfo);
36             exit();
37             //End
38         }
39         //End
40         
41         $recommend_anwser_DB->where(" subject_id = ".intval($_GET['id']))->delete();
42         $subjectRe = $subjectDB->where(" id =  ".intval($_GET['id']))->delete();
43        
44         if($subjectRe)
45         {
46             echo("<script type='text/javascript'> alert('删除成功!');location.href='".__MODULE__.'/'.CONTROLLER_NAME."/subject_list/$pageStr_no_condition';</script>");
47                 exit(); 
48         }
49         else
50         {
51             echo("<script type='text/javascript'> alert('删除失败!');location.href='".__MODULE__.'/'.CONTROLLER_NAME."/subject_list/$pageStr_no_condition';</script>");
52                 exit(); 
53         }
54     }

 

posted @ 2018-06-16 13:30  哟风  Views(436)  Comments(0)    收藏  举报