没有在gridview或列表视图中的yii中的分页

时间:2015-08-03 15:07:05

标签: php yii

如何在没有任何网格视图和列表视图的情况下在Yii中进行分页 我在他们的堆栈中待了两天 这些是我的控制器

public function actionIndex()
{
    $criteria = new CDbCriteria;
    // $criteria ->order = 'date';
    $pages = new CPagination(OfferEvents::model()->count());
    $pages ->pageSize = 2;
    $pages -> applyLimit($criteria);
    $post= OfferEvents::model()->findAll($criteria);
    $this -> render('index', array('post' => $post, 'pages' => $pages));
}

和我的观点

<?php foreach($models as $model): ?>
<?php for($x = 0; $x < $arrlength; $x++) {   ?>    

    <?php if($catiddet == 1){?>




                <div class="col-sm-4">
                <div class="list-box fadeInDown ">
                <div class="img-box-1">
                <img src="<?php echo $offerListArray[$x]['offerimg']; ?>" style="width:360px;height:202px;" />
                <a href="#" class="view-btn" d='modal-launcher' data-toggle="modal" 
                data-target="#detailsOffeEve" onClick="showdetails(<?php echo $offerListArray[$x]['id']; ?>)">View Event</a>
                </div>
                <div class="text-box">

                <?php 



                $date2=$offerListArray[$x]['enddate']  ;
                $diff = (strtotime($date2)- strtotime($now))/24/3600;


                ?> 


                <div class="round-box clear"><span><h1><?php echo $diff ?><small>Days</small></h1></span></div>
                <p style="height:19px;overflow: hidden;"> <b ><?php echo $offerListArray[$x]['offertitle']; ?> </b></p>
                <p style="height: 80px;overflow: hidden;"><?php echo $offerListArray[$x]['description']; ?> </p>
                </div>

                </div>
                </div>
                <?php } else{?>
                <div class="col-sm-4"  >
                    <div class="list-box">
                    <div class="img-box-1">
                    <?php   $filepathnw=$offerListArray[$x]['offerimg'];
                    //if (file_exists($filepathnw)) {  $filepathSrc=Yii::app()->baseUrl.'/'.$filepathnw;   } else 
                    //{  $filepathSrc='http://www.childnet.com/pimages/1351720/thumbnail4.jpg';   }  
                    ?>
                    <img src="<?php echo $filepathnw; ?>" />
                    <a href="#" class="view-btn" d='modal-launcher' data-toggle="modal" 
                    data-target="#detailsOffeEve" onClick="showdetails(<?php echo $offerListArray[$x]['id']; ?>)">View Offer</a>
                    </div>     
                    <div class="text-box" style="padding:2px !important;">


                        <?php 



                        $date2=$offerListArray[$x]['enddate']  ;
                        $diff = (strtotime($date2)- strtotime($now))/24/3600;


                        ?> 


                    <span><h1><?php echo $diff ?><small>Days</small></h1></span>
                    <span><h1><?php echo $offerListArray[$x]['discountper']; ?>%<small>DISCOUNT</small></h1></span>
                    <span><h1>$<?php echo $offerListArray[$x]['discountperamt']; ?><small>You Save</small></h1></span>
                    <div class="text-box" style="padding:15px;">
                    <p style="height:19px;overflow: hidden;">
                    <b ><?php echo $offerListArray[$x]['offertitle']; ?> </b></p>
                    <p style="height: 80px;overflow: hidden;"><?php echo $offerListArray[$x]['description']; ?> </p>
                    </div> </div>
                    </div>                    
                    </div> 
                    <?php } ?>



<?php } ?>

<?php endforeach; ?>

// display pagination
<?php $this->widget('CLinkPager', array(
    'pages' => $pages,
)) ?>

out put很好......但数据重复......

   select * FROM offer_events WHERE enddate >= '$now' AND (title like '%$locationdet%' OR description like '%$locationdet%') AND type = '$catiddet' ORDER BY id DESC LIMIT 6 ");  
        $datalat = $dbCommand->queryAll();     $numlatData=count($datalat);
        // echo "<pre>"; print_r($datalat); echo "</pre>"; 
        $latlongdats=''; 
        if($numlatData!=0){ 
        foreach($datalat as $expertn)
        {    
        $ids=$expertn['id'];    
        $offertitle=$expertn['title'];    
        if($expertn['image']!=""){ $imgUrl=$expertn['image'];  } else { $imgUrl='image-not-available.png'; }
                $infowinimgpath='theme/images/OfferEvents/thumb/'.$imgUrl;
                if (file_exists($infowinimgpath)) {  $infowinimgpathSrc=Yii::app()->baseUrl.'/'.$infowinimgpath;   } else 
                {  $infowinimgpathSrc=Yii::app()->baseUrl.'/theme/images/OfferEvents/thumb/image-not-available.png';    }   
        $offerimg=$infowinimgpathSrc;    
        $latinow=$expertn['latitude'];       
        $longinow=$expertn['longitude'];        
        $latlongdats.="['".$ids."', ".$latinow.",".$longinow.",'".$offertitle."','".$offerimg."','".$expertn['startdate']."'],";
        $offertList  = array(
        "id"  => $ids,
        "offertitle"=> $offertitle,
        "offerimg" => $offerimg, 
        "description" => $expertn['description'],   
        "startdate" => $expertn['startdate'],   
        "enddate" => $expertn['enddate'],   
        "discountper" => $expertn['discountper'],   
        "discountperamt" => $expertn['discountperamt']  

        );
        array_push($offerListArray,$offertList);
        }
        $zoomlevel=3;
        $latlongdatsfinal=rtrim($latlongdats, ",");
        } else { $latlongdatsfinal="['','',''],";   $ErrorMsgmap="No Result Found.";  }

}

你能否解释一下这是怎么发生的?

1 个答案:

答案 0 :(得分:1)

您可以使用CActiveDataProvider来执行您的分页需求

public function actionIndex()
{
    $criteria = new CDbCriteria;
    // $criteria ->order = 'date';
    $post= new CActiveDataProvider('OfferEvents', array(
                                   'criteria' => $criteria,    
                                   'pagination'=>array('pageSize' => 2),
    ));
    $this -> render('index', array('post' => $post));
}

在您的视图中,不使用foreach循环,而是使用ListView / GridView并在您的案例中传递CActiveDataProvider实例($ post)

$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$post,
'itemView'=>'_post',   // refers to the partial view named '_post'

));

“_ postt.php”子视图文件将包含

$data->property // whatever properties you have set for 'OfferEvents'