最新消息: 新版网站上线了!!!

yii利用CArrayDataProvider实现多个表搜索

 class SearchController extends Controller{
    public function actionsearch(){
        $array1 = '站内搜索';
        $this->setPageTitle("{$array1}_懒人程序");
        $query = Yii::app()->request->getQuery('wd');
        $connection = Yii::app()->db;
        $sql = "select * from {{news}} where title like '%{$query}%' or description like '%{$query}%'";
        $command = $connection->createCommand($sql);
        $result = $command->queryAll();
        $new = array();
        $news = array();
        foreach($result as $key=> $k){
            $new[$key]['id'] = $k['id'];
            $new[$key]['title'] = $k['title'];
            $new[$key]['description'] = $k['description'];
            $new[$key]['thumb'] = $k['thumb'];
            $new[$key]['url'] = Yii::app()->createUrl('post/view',array('id'=>$k['id'], 'title'=>date('Ymd',$k['inputtime'])));
        }
        $sql = "select * from {{daohang}} where title like '%{$query}%' or description like '%{$query}%'";
        $command = $connection->createCommand($sql);
        $results = $command->queryAll();
        foreach($results as $key=> $k){
            $news[$key]['id'] = $k['id'];
            $news[$key]['title'] = $k['title'];
            $news[$key]['thumb'] = $k['thumb'];
            $news[$key]['description'] = $k['description'];
            $news[$key]['url'] = Yii::app()->createUrl('post/danganview',array('id'=>$k['id']));
        }
        $_results = array_merge($new,$news);
        $dataProvider=new CArrayDataProvider($_results, array(
            'id'=>'user',
            'pagination'=>array(
                'pageSize'=>10,
                //  'pageVar'=>'page',
            ),
        ));
        //   print_r($result);
        $this->render('search',array('dataProvider'=>$dataProvider));
    }
}
视图search.php
 <?php
 
            $this->widget('zii.widgets.CListView', array(
                'dataProvider'=>$dataProvider,
                'itemView'=>'_search',   // refers to the partial view named '_post'
                //'viewData'=>array('dataProviders'=>$dataProvider->getData())
            ));
            ?>
视图_search.php(view)
<div class="sous">
    <div class="sous1"><a href="<?php echo $data['url'];?>"><?php echo $data['title'];?></a></div>
    <div class="sous2">
        <?php if(!empty($data['thumb'])):?><span><img src="<?php echo $data['thumb'];?>" width="90" height="90" /></span><?php endif;?>
        <?php echo $data['description'];?>..<a  target="_blank"  href="<?php echo $data['url'];?>">[详情]</a>
    </div>
 
</div>

转载请注明:谷谷点程序 » yii利用CArrayDataProvider实现多个表搜索