johan02
(Zaslsaz)
December 5, 2012, 8:38am
1
Hi .
I am new in yii world . i have already done pagination for data from one model but not able to do it for data from two models.i am not using Clistview or CgridView . i don’t know how to apply yii pagination in this functions.help me plz .
ChainController
public function actionView($slug) {
$model = $this->loadSlug($slug);
$this->render(
'view', array(
'model' => $model,
)
);
}
protected function loadSlug($slug) {
$chain = Chain::model()->find('nameSlug = :slug', array('slug' => $slug));
if (is_null($chain)) {
throw new CHttpException(404, 'page does not exist.');
}
return $chain ;
}
In view
<?php foreach($model->locations as $location): ?>
<?php echo GxHtml::link($location->name, array('location/view', 'slug' => $location->nameSlug), array('title' => $location->name)); ?>
<?php endforeach; ?>
In Model i have relations:
public function relations() {
return array(
'locations' => array(self::HAS_MANY, 'Location', 'chainId'),
);
}
seenivasan
(Chellamnivas)
December 6, 2012, 2:43pm
3
Dear Friend
You can do it in the following way.
$count=count($model->locations);
$pages=new CPagination($count);
$pages->pageSize=5;
$models=$model->locations;
$models=array_slice($models, $pages->getOffset(), $pages->getLimit());
<?php foreach($model->locations as $location): ?>
<?php echo GxHtml::link($location->name, array('location/view', 'slug' => $location->nameSlug), array('title' => $location->name)); ?>
<?php endforeach; ?>
$this->widget('CLinkPager', array(
'pages' => $pages,
));
?>
or more elegantly
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>new CArrayDataProvider($model->locations,array(
'pagination'=>array('pageSize'=>5),
)),
'itemView'=>'_view',
)); ?>
create a _view.php in the following way.
<?php echo GxHtml::link($data->name, array('location/view', 'slug' => $data->nameSlug), array('title' => $data->name)); ?>
Regards.
johan02
(Zaslsaz)
December 12, 2012, 2:41pm
4
seenivasan:
Dear Friend
You can do it in the following way.
$count=count($model->locations);
$pages=new CPagination($count);
$pages->pageSize=5;
$models=$model->locations;
$models=array_slice($models, $pages->getOffset(), $pages->getLimit());
<?php foreach($model->locations as $location): ?>
<?php echo GxHtml::link($location->name, array('location/view', 'slug' => $location->nameSlug), array('title' => $location->name)); ?>
<?php endforeach; ?>
$this->widget('CLinkPager', array(
'pages' => $pages,
));
?>
or more elegantly
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>new CArrayDataProvider($model->locations,array(
'pagination'=>array('pageSize'=>5),
)),
'itemView'=>'_view',
)); ?>
create a _view.php in the following way.
<?php echo GxHtml::link($data->name, array('location/view', 'slug' => $data->nameSlug), array('title' => $data->name)); ?>
Regards.
Thank YOu friend . i was disappointed but u saved me . i made some changes … it may helps some intelligent guys like me .
$model = $this->loadSlug($slug);
$count=count($model->locations);
$pages=new CPagination($count);
$pages->pageSize=15;
$models=$model->locations;
$models=array_slice($models, $pages->getOffset(), $pages->getLimit());
$this->render(
'view', array(
'model'=>$model,
'models' => $models,
'pages'=>$pages
)
);
in view
<?php foreach($models as $location):
echo GxHtml::link($location->name, array('location/view', 'slug' => $location->nameSlug));
endforeach; ?>
$this->widget('CLinkPager', array(
'pages' => $pages,
));
?>
johan02
(Zaslsaz)
December 12, 2012, 3:52pm
5
Dear friend .
the main problem is solved but how could i remove page(word) from the url .
i mean www.example/chain/walmart/page/2. i want to remove page (word) from url and url should be www.example/chain/walmart/2
in urlManager i have this rule :
‘chain/<slug:[\d\w-åäöÅÄÖéÉ]+>/*’ => ‘chain/view’
any solution ?
thank you thats work for me