天天看点

PHP 根据传参不同切换页面

如下图:服务管理和回收站要显示不同的列表数据,但是样式基本相同,就不用写两个页面了,只需要在页面跳转的时候携带不同参数,查询相对应的数据,对样式的不同之处判断显示即可 

PHP 根据传参不同切换页面

这是跳转页面的代码:我携带的参数分别是"all"和"deleted" (选中样式也要判断)

<li class="<?php echo $cate == 'all' ? 'active':'' ; ?>"><a href="{:U('Business/index',array('cate'=>'all'))}" target="_blank" rel="external nofollow" >服务管理</a></li>
<li><a href="{:U('Business/add')}" target="_blank" rel="external nofollow"  target="_self">添加服务</a></li>
<li class="<?php echo $cate == 'deleted' ? 'active':'' ; ?>"><a href="{:U('Business/index',array('cate'=>'deleted'))}" target="_blank" rel="external nofollow"  target="_self">回收站</a></li>
           

部分的标签“服务管理”需要,但是“回收站”不需要,所以先给一个样式

<style>
  .hidden{
	display: none;
  }
</style>
           

根据控制器里传过来的参数判断需不需要显示(这里是“一键上架”,“一键下架”,“删除”按钮的显示)

<button class="<?php echo $cate == 'deleted' ? 'hidden':'' ; ?> btn btn-primary btn-small js-ajax-submit" type="submit" data-action="{:U('Business/top',array('top'=>1))}" data-subcheck="true">一键上架</button>
<button class="<?php echo $cate == 'deleted' ? 'hidden':'' ; ?> btn btn-primary btn-small js-ajax-submit" type="submit" data-action="{:U('Business/top',array('untop'=>1))}" data-subcheck="true">一键下架</button>
<button class="<?php echo $cate == 'deleted' ? 'hidden':'' ; ?> btn btn-danger btn-small js-ajax-submit" type="submit" data-action="{:U('Business/delete')}" data-subcheck="true" data-msg="您确定删除吗?">删除</button>

<!-- a标签的判断显示 -->
<a href="{:U('Business/delete',array('id'=>$vo['id']))}" target="_blank" rel="external nofollow" >
  <?php echo ($cate == "all") ? '| 删除':'' ?>
</a>
           

控制器代码:

public function index()
    {
        //搜索
        $search_key = I('post.keyword', '');
        if (!empty($search_key)) {
            $where['title'] = ['like', '%' . $search_key . '%'];
        }
        //页面传过来的参数
        $cate = I('get.cate/s','all');
        if($cate == 'all'){
            $where['delete_time'] = ['EQ', 0];
        }else{
            $where['delete_time'] = ['NEQ', 0];
        }
        $count = $this->business_item_model->where($where)->count();
        $page = $this->page($count, 20);
        $list = $this->business_item_model
            ->field('id,title,img,link,price,read_count,pay_count,is_show,`status`')
            ->where($where)
            ->order(['create_time' => 'desc'])
            ->limit($page->firstRow, $page->listRows)
            ->select();
        foreach ($list as &$vo) {
            $vo['img'] = sp_get_host() . C("TMPL_PARSE_STRING.__UPLOAD__") . $vo['img'];
        }
        $this->assign("list", $list);
        $this->assign("cate", $cate);
        $this->assign("search_key", $search_key);
        $this->assign("page", $page->show("Admin"));
        $this->display();
    }