Cgridview With Textfield

Hi… I’m pretty new to Yii and I have been creating an admin system. I got stuck with my cgridview, I want to have one textfield from a fuction in my model and a dropdownlist. I have looked around and can’t really find the answer I’m looking for online. Think it’s my controller that i am doing wrong. I want to save those fields to different table in my database.

DB tables used

Visitor_master (name,surname,mobile,email, invited by)

visitor_comment (id,comment,visitor_id,date)

visitor_status (id, description, visitor_id, date)

In my view… admin

/************************************************************************************************************/

<?php $form=$this->beginWidget(‘CActiveForm’, array(

    'id'=&gt;'menu-grid',


    'enableAjaxValidation'=&gt;true,

)); ?>

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'menu-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


	'name',


	'surname',


	'mobile',


	'email',


	'invited_by',


						


	array(


	'header'=&gt;'comment',


    'value'=&gt;'CHTML::textField(&#036;test-&gt;comment,&#036;data-&gt;Comments(),array(&#092;'width&#092;'=&gt;20,&#092;'maxlength&#092;'=&gt;3))',


    'type'=&gt;'raw',


    'htmlOptions'=&gt;array('width'=&gt;'20px'),


  ), 


  


	array(


                    'header' =&gt; 'Comment',


                    'type' =&gt; 'text',


                    'value' =&gt; ('&#036;data-&gt;Comments()'),


     ),


	array(


                    'header' =&gt; 'status',


                    'type' =&gt; 'text',


                    'value' =&gt; ('&#036;data-&gt;Statuses()'),


					


     ),


	


		array(


		'class'=&gt;'CButtonColumn',


		'template'=&gt;'{update}',


			


	),


),

)); ?>

<div class="row buttons">

	&lt;?php echo CHtml::submitButton(&#036;test-&gt;isNewRecord ? 'Save' : 'Save'); ?&gt;


	&#60;&#33;--&lt;button type=&quot;sumbit&quot;&gt;Next Step&lt;/button&gt;--&#62;


&lt;/div&gt;

<?php $this->endWidget(); ?>

/***********************************************************************************************************/

model : visitor_master

/************************************************************************************************************/

	public function Comments()

{

    &#036;comments = array();


	&#036;count_array = count(&#036;this-&gt;visitorComments);


	&#036;v=0;


    foreach(&#036;this-&gt;visitorComments as &#036;comment){


            &#036;v++;


			&#036;comments[] = &#036;comment-&gt;comment;


				if (&#036;count_array = &#036;v){


				//echo implode(&#036;comments);

}

	 return implode(&#036;comments);

}

}

// function to diplay all status for the visitors

public function Statuses()

{

    &#036;statuses = array();


	&#036;count_array = count(&#036;this-&gt;visitorStatuses);


	&#036;v=1;


    foreach(&#036;this-&gt;visitorStatuses as &#036;status){


			


			if (&#036;count_array = &#036;v){


					&#036;statuses[] = &#036;status-&gt;description;


			}


			return implode(&quot;&#092;n&quot;, &#036;statuses);


			&#036;v++;


	}

}

/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'visitor_master';


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('name, surname, mobile, email, invited_by, leader_id', 'required'),


		array('name, surname, invited_by', 'length', 'max'=&gt;30),


		array('mobile', 'length', 'max'=&gt;12),


		array('email', 'length', 'max'=&gt;40),


		array('leader_id', 'length', 'max'=&gt;20),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('id, name, surname, mobile, email, invited_by, leader_id', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


	    


		'visitorComments' =&gt; array( self::HAS_MANY, 'visitor_comment', 'visitor_id'),


					//'order'=&gt;'VisitorComments.comment DESC'),


		'visitorEntries' =&gt; array(self::HAS_MANY, 'visitor_entry', 'visitor_id'),


		'leader' =&gt; array(self::BELONGS_TO, 'leader_master', 'leader_id'),


		'visitorStatuses' =&gt; array(self::HAS_MANY, 'visitor_status', 'visitor_id'),


		


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'name' =&gt; 'Name',


		'surname' =&gt; 'Surname',


		'mobile' =&gt; 'Mobile',


		'email' =&gt; 'Email',


		'invited_by' =&gt; 'Invited By',


		'leader_id' =&gt; 'Leader',


	);


}





/**


 * Retrieves a list of models based on the current search/filter conditions.


 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.


 */


public function search()


{


	// Warning: Please modify the following code to remove attributes that


	// should not be searched.


    &#036;action = &#036;_GET['id'];


	


	if (&#33;isset(&#036;_GET['id'])){


		&#036;action = Yii::app()-&gt;session['min_leader_id'];


	}


	//if (&#036;action = 0){


	//&#036;action = Yii::app()-&gt;session['leader_id'];	


	//}


	


	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id,true);


	&#036;criteria-&gt;compare('name',&#036;this-&gt;name,true);


	&#036;criteria-&gt;compare('surname',&#036;this-&gt;surname,true);


	&#036;criteria-&gt;compare('mobile',&#036;this-&gt;mobile,true);


	&#036;criteria-&gt;compare('email',&#036;this-&gt;email,true);


	&#036;criteria-&gt;compare('invited_by',&#036;this-&gt;invited_by,true);


	&#036;criteria-&gt;compare('leader_id',&#036;this-&gt;leader_id,true);


	


	&#036;criteria-&gt;addCondition(&quot;leader_id = &#036;action&quot;);


	





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}

}

/*************************************************************************************************************/

Controller

public function actionAdmin()

{


	&#036;model=new visitor_master('search');


	&#036;test = new visitor_comment;


	//if(Yii::app()-&gt;request-&gt;isPostRequest)


	//{


	


	&#036;fakeid = 82;


	&#036;date=date('y-m-d');


	


	


			if(isset(&#036;_GET['visitor_master'],&#036;_GET['visitor_comment'] )){


			


					


				    &#036;test-&gt;date=&#036;date;


					&#036;test-&gt;visitor_id=&#036;fakeid;


					&#036;test-&gt;comment='Hello';


					


					&#036;model-&gt;attributes=&#036;_GET['visitor_master'];


					&#036;test-&gt;attributes=&#036;_GET['visitor_comment'];


					


		           // &#036;model-&gt;save();


					&#036;test-&gt;save();


			//} 


	}


	


	


//	&#036;model-&gt;unsetAttributes();  // clear any default values





	&#036;this-&gt;render('admin',array(


		'model'=&gt;&#036;model,


		'test'=&gt;&#036;test,


	));

}

Please if someone can help me, have been stuck on this for age now

Please elaborate, what do you want to do? the problem is not clear. What actions you want to perform and how (button click etc)?

Always paste your code in code block (that is <>), so that it can be read easily.

Yeah sorry first time that I used the forum…

Basically what needs to happen when you click save it must take all the textfields data that has been entered and save them all into the database the same will happen for the status. the function that I created in the model finds the comment that was inserted in the db and displays that in cgridview that bit works fine, but I cannot save any data into the db.

This is in my view file… few thing commented out… as I was trying to figure it out


<h1>Visitor</h1>


<?php //echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>

<div class="search-form" style="display:none">

<?php $this->renderPartial('_search',array(

	'model'=>$model,

	

	

)); ?>

</div><!-- search-form -->

<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'visitor_master-grid',

        'enableAjaxValidation'=>true,

)); ?>


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'visitor_master-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'name',

		'surname',

		'mobile',

		'email',

		'invited_by',

		

			array(

                        'name'=>'comment',

						'type'=>'raw',

                        'value'=>'CHTML::textField($test->comment[$data->id],$data->Comments(),array(\'width\'=>20,\'maxlength\'=>20))',

                        'filter'=>'visitor_comment::model()->findAllAttributes(null, true))',

                ),				

		array(

		'header'=>'comment',

        'value'=>'CHTML::textField($test->comment,$data->Comments(),array(\'width\'=>20,\'maxlength\'=>20))',

        'type'=>'raw',

        'htmlOptions'=>array('width'=>'20px'),

      ), 

	  array(

		'header'=>'Status',

        'value'=>'CHTML::dropdownList($status->status,$data->                           statuses(),array("",Member,Regular,Visited,Contacted,Not_Interested,To-Followup))',

        'type'=>'raw',

        'htmlOptions'=>array('width'=>'20px'),

      ),

	  /*

		array(

                        'header' => 'Comment',

                        'type' => 'text',

                        'value' => ('$data->Comments()'),

         ),

		array(

                        'header' => 'status',

                        'type' => 'text',

                        'value' => ('$data->Statuses()'),

						

         ),*/

		

			array(

			'class'=>'CButtonColumn',

			'template'=>'{update}',

				

		),

	),

)); ?>

<div class="row buttons">

   		<?php echo CHtml::submitButton($model->isNewRecord ? 'Save' : 'Save'); ?>

		<!--<button type="sumbit">Next Step</button>-->

	</div>

    

<?php $this->endWidget(); ?>

<?php // echo CHtml::endForm(); ?>

and my controllers code


public function actionAdmin()

	{

		

		

		//$model=$this->loadModel($id);

		$model=new visitor_master('search');

		$test = new visitor_comment;

		$status = new visitor_status;

		$date=date('y-m-d');

		

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

if(isset($_GET['visitor_master'] )){

		if(isset($_POST['visitor_comment'], $_POST['visitor_status']))

		{

			$test->date=$date;

			//$model->attributes=$_POST['visitor_master'];

			$model->attributes=$_GET['visitor_master'];

			$test->attributes=$_POST['visitor_comment'];

			

			$status->attributes=$_POST['visitor_status'];

			

			

			$status->date=$date;

			

			

			

			

			$model->save();

			$test->save();

			$status->save();

			

			

			

				$this->redirect(array('admin'/*,'id'=>$model->id*/));

		}

}


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

			'model'=>$model,

			'test'=>$test,

			'status'=>$status,

		));

	}

This link should be helpful to solve your problem:-

http://www.yiiframework.com/wiki/353/working-with-cgridview-in-admin-panel/

I did try to follow that example, but I have no idea what to do in my controller file

Hmm, but your code does not show that you followed it correctly. For example,

i) you have used submitButton, wiki article used ajaxSubmitButton

ii) You dont have scripts in you view file, for example reloadgrid and preceeding

iii) You have not used foreach loop to save each row’s data into models, like in the wiki (in the controller action)

try to follow it again, i have used this approach in my project successfully :)