problem with CPagination

i have a problem with my CPagination…

in my code, pagination doesn’t work.

it show all data in 1 page, but i want show 10 data per page

this is code in controller




public function actionReport()

	{

		$data=new Report;

		if(isset($_GET['Report']))

		{

			$count=Yii::app()->db->createCommand("select count(*) from barang inner join harga on barang.id_barang = harga.id_barang where id_barang=$this->id_barang")->queryScalar();

			$sql="select * from barang inner join harga on barang.id_barang = harga.id_barang where id_barang=$this->id_barang";

			$cmd=Yii::app()->db->createCommand($sql);

			

			$pages = new CPagination($count);

                        $pages->pageSize=120;

                        $models = $cmd->queryAll();

                        $pages->applyLimit($cmd);


                        $this->render('all_report', array(

			              'models' => $models,

				      'pages' => $pages

			));

		}

		else

			$this->render('allReport',array('model'=>$data));

	}



this is in view




foreach ($models as $i)

	{

		$i["id_barang"];

		$i["nama barang"];

		$i["jumlah"];

		$i["stock"];

	}

	

	$this->widget('CLinkPager', array('pages' => $pages,));



please help me…

i don’t understand with this error…

(sorry for my english, my english is not so good)

change this line to:




  $pages->pageSize=10;



if you only want 10 per page, as this configures the amount of rows per page

i change the code like




  $pages->pageSize=10;



but still not working…

there is something wrong with my code?

As I can see, your output of the models has no relation to your linkpager.

You request all models by ‘queryAll’ and afterwards you display all the models.

On ‘queryAll’ you have no criteria built by the pagination.

You have to ‘applyLimit’ first with a CDbCritera object and afterwards you should query your data with this criteria.

pagination->applyLimit assigns the limit and offset of the current page to the critera.

Take a look at the example on top of the CPagination class reference or search for other examples in the forum/wiki …

how to solve this problem?

i don’t use CDbCriteria.

I would use a CSqlDataProvider




$count=Yii::app()->db->createCommand("select count(*) from barang inner join harga on barang.id_barang = harga.id_barang where id_barang=$this->id_barang")->queryScalar();


$sql="select * from barang inner join harga on barang.id_barang = harga.id_barang where id_barang=$this->id_barang";

 $dataProvider=new CSqlDataProvider($sql, array(

    'totalItemCount'=>$count,

    'pagination'=>array(

        'pageSize'=>10,

    ),

));


$this->render('all_report', array(

                                 'dataProvider' => $dataProvider,

                        ));




In you all_report view you can use a CListView




$this->widget('zii.widgets.CListView', array(

    'dataProvider'=>$dataProvider,

    'itemView'=>'_view',   // refers to the partial view named '_view'

 

));



i ussualy use CSqlDataProvider, but for my table, the header like this.

i don’t know use CSqlDataProvider with this table.

Maybe it’s possible to do with CGridView too.

Try yourself with the help of my last wiki-article CGridView: complex datacolumns

Otherwise you can implement this manually by using the SqlDataProvider like above too.

your view





$models = $dataProvider->getData(); //load the current page records


foreach ($models as $i)

        {

                $i["id_barang"];

                $i["nama barang"];

                $i["jumlah"];

                $i["stock"];

        }

        

        //use the pagination from the dataProvider

        $this->widget('CLinkPager', array('pages' => $dataProvider->pagination,));