Jqgrid, Json Help

Hi,

I am trying to get JQGrid working just to see it work. I have managed to get it working with an array but am struggling when trying to use JSON. It may well be my JSON file being returned.

Anyway this is what things look like:

My View:


<?php

/* @var $this RabbitsectionController */

/* @var $model Rabbitsection */


 $baseUrl = Yii::app()->baseUrl; 

 $cs = Yii::app()->getClientScript();

 $cs->registerCssFile($baseUrl.'/css/ui.jqgrid.css');

 $cs->registerScriptFile($baseUrl.'/js/jquery-1.9.0.min.js');

 $cs->registerScriptFile($baseUrl.'/js/i18n/grid.locale-en.js');

 $cs->registerScriptFile($baseUrl.'/js/jquery.jqGrid.min.js');

 ?>

 <script type="text/javascript">

$(function () {

jQuery("#list4").jqGrid(

        { 

            url:'jsonlist',

            datatype: "json", 

            colNames:['Section ID','Section Name', 'Order'], 

            colModel:[

                {name:'id',index:'id', width:77}, 

                {name:'RabbitSectionName',index:'RabbitSectionName', width:200}, 

                {name:'RabbitSectionOrder',index:'RabbitSectionOrder', width:50}, ], 

            rowNum:10,

            rowList:[10,20,30], 

            pager: '#pager2', 

            sortname: 'id', 

            viewrecords: true, 

            sortorder: "desc", 

            caption:"JSON Example" 

        }); 

 jQuery("#list4").jqGrid('navGrid',

                            '#pager2',

                            {

                                edit:true,

                                add:true,

                                del:true

                            });

});


</script>



My Controller:


        public function actionList()

        {

		$this->render('list');

        }


        public function actionjsonlist()

        {

            $this->$page = $_GET['page']; 

            // get the requested page 

            $limit = $_GET['rows']; 

            // get how many rows we want to have into the grid 

            $sidx = $_GET['sidx']; 

            // get index row - i.e. user click to sort 

            $sord = $_GET['sord']; 

            // get the direction             

            $model=Rabbitsection::model()->findAll();


            $count=Rabbitsection::model()->count();

                    

            echo json_encode($model->getAttributes(array('id','RabbitSectionName','RabbitSectionOrder')), JSON_NUMERIC_CHECK);

            //echo json_encode($model->getAttributes(array('id','name','email')), JSON_NUMERIC_CHECK);

        }



Now I am getting the Title, The Grid Header and the paging controls but no data and the counter is 0 for page. Now I pretty much know I haven’t set the page counter etc correctly but need help in just getting data shown.

This is the sample from the demo page for php:


HTML

... <table id="list2"></table> <div id="pager2"></div>

Java Scrpt code

... jQuery("#list2").jqGrid({ 

url:'server.php?q=2', 

datatype: "json", 

colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'], 

colModel:[ 

{name:'id',index:'id', width:55}, 

{name:'invdate',index:'invdate', width:90}, 

{name:'name',index:'name asc, invdate', width:100}, 

{name:'amount',index:'amount', width:80, align:"right"}, 

{name:'tax',index:'tax', width:80, align:"right"}, 

{name:'total',index:'total', width:80,align:"right"}, 

{name:'note',index:'note', width:150, sortable:false} ], 

rowNum:10, 

rowList:[10,20,30], 

pager: '#pager2', 

sortname: 'id', 

viewrecords: true, 

sortorder: "desc", caption:"JSON Example" }); 


jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});


PHP with MySQL

$page = $_GET['page']; // get the requested page 

$limit = $_GET['rows']; // get how many rows we want to have into the grid 

$sidx = $_GET['sidx']; // get index row - i.e. user click to sort 

$sord = $_GET['sord']; // get the direction 

if(!$sidx) $sidx =1; // connect to the database 

$db = mysql_connect($dbhost, $dbuser, $dbpassword) 

or die("Connection Error: " . mysql_error()); 

mysql_select_db($database) or die("Error conecting to db."); 

$result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id"); 

$row = mysql_fetch_array($result,MYSQL_ASSOC); 

$count = $row['count']; 

if( $count >0 ) { 

$total_pages = ceil($count/$limit); } 

else { $total_pages = 0; } 

if ($page > $total_pages) 

$page=$total_pages; 

$start = $limit*$page - $limit; // do not put $limit*($page - 1) 

$SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id ORDER BY $sidx $sord LIMIT $start , $limit"; 

$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error()); 


$responce->page = $page; 

$responce->total = $total_pages; 

$responce->records = $count; 

$i=0; while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { 

$responce->rows[$i]['id']=$row[id]; 

$responce->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]); $i++; } 


echo json_encode($responce);

Now I am pretty sure I have everything for the HTML and the javascript (my View) right it is in the controller that my problem exists.

Any help would be greatly appreciated.

Neil

OK.

I have changed the controller to do the following in the json part:


        public function actionjsonlist()

        {

            // get the direction    

            $webresult->page = 1;

            $webresult->total  = 1;

            $webresult->records =8;


            $model=Rabbitsection::model()->findAll();


            i==0;

            foreach ($model as $row){

                $webresult->rows[$i]['id']=$row[id];

                $webresult->rows[$i]['cell']=array($row[id],$row[RabbitSectionName],$row[RabbitSectionOrder]); 

                $i++;

            }

                    


                    

            echo json_encode($webresult, JSON_NUMERIC_CHECK);



Using Firebug I have managed to get this out of the console:


<h1>PHP Error [2048]</h1>

<p>Creating default object from empty value (C:\xampp\htdocs\lagosyii\protected\controllers\RabbitsectionController.php:56)</p>

<pre>#0 C:\xampp\htdocs\lagosyii\protected\controllers\RabbitsectionController.php(56): CWebApplication->handleError()

#1 C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(49): RabbitsectionController->actionjsonlist()

#2 C:\xampp\htdocs\yii\framework\web\CController.php(308): CInlineAction->runWithParams()

#3 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(133): RabbitsectionController->runAction()

#4 C:\xampp\htdocs\yii\framework\web\filters\CFilter.php(40): CFilterChain->run()

#5 C:\xampp\htdocs\yii\framework\web\CController.php(1145): CAccessControlFilter->filter()

#6 C:\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(58): RabbitsectionController->filterAccessControl()

#7 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter()

#8 C:\xampp\htdocs\yii\framework\web\CController.php(291): CFilterChain->run()

#9 C:\xampp\htdocs\yii\framework\web\CController.php(265): RabbitsectionController->runActionWithFilters()

#10 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(282): RabbitsectionController->run()

#11 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(141): CWebApplication->runController()

#12 C:\xampp\htdocs\yii\framework\base\CApplication.php(169): CWebApplication->processRequest()

#13 C:\xampp\htdocs\lagosyii\index.php(13): CWebApplication->run()

</pre>


"NetworkError: 500 Internal Server Error - http://localhost/lagosyii/index.php/rabbitSection/jsonlist?_search=false&nd=1372927705318&rows=10&page=1&sidx=id&sord=desc"

I am sorry if this is really basic stuff but I am trying to get my head around this.

Thanks,

Neil