Example #1
0
function vary_array($node, $id = 0, $level = 0)
{
    $arr = array();
    foreach ($node as $v) {
        if ($v['pid'] == $id) {
            $v['level'] = $level + 1;
            $v['child'] = vary_array($node, $v['id'], $level + 1);
            $arr[] = $v;
        }
    }
    return $arr;
}
 public function indexAction()
 {
     $category = M('cate')->order('sort')->select();
     $category = vary_array($category);
     //var_dump($category);die;
     $this->assign('category', $category);
     if (array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX']) {
         $this->display();
     } else {
         C('LAYOUT_ON', true);
         $this->display();
     }
 }
Example #3
0
 public function searchAction()
 {
     if (!$_GET['search']) {
         echo "搜索不能为空";
         return;
     }
     $db = M();
     if (!$_GET['p']) {
         $_GET['p'] = 0;
     }
     $query = "'%" . $_GET['search'] . "%'";
     $map['title'] = array('like', '%' . $_GET['search'] . '%');
     $blog = $db->field('title,content,id,time,cid')->table('blog')->where(array('del' => 0))->where($map)->order('time desc')->page($_GET['p'] . ',4')->select();
     $count = $db->table('blog')->where($map)->where(array('del' => 0))->count();
     // 查询满足要求的总记录数
     $Page = new \Think\Page($count, 4);
     // 实例化分页类 传入总记录数和每页显示的记录数
     $show = $Page->show();
     // 分页显示输出
     $this->assign('page', $show);
     // 赋值分页输出
     if (!$blog) {
         echo '<p>没有符合条件的结果</p>';
         return;
     }
     foreach ($blog as &$v) {
         $v['time'] = getdate($v['time']);
     }
     $this->assign('blog', $blog);
     if (array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX']) {
         $this->display();
     } else {
         $data = $db->field('name,id,pid')->table('cate')->select();
         $attr = D('Property')->relation(true)->select();
         $arr = vary_array($data);
         $this->assign('arr', $arr);
         $this->assign('attr', $attr);
         C('LAYOUT_ON', true);
         $this->display();
     }
 }