Viewing Specific Data In Columns

Hello guys. I just want to ask for help.

I’m working on a game registration system, which players/guest can register online.

I have a table named players, and under it it have a column name player_status there are two options in the player status; paid and not paid. (Only admin can change/modify the player_status)

Here is my problem, I want to View only the Players who are already paid in the index.php.

Thank you guys I’m just new here in yii and our school project is to create a system. thanks you again :D

If we assume that you have Active Record model class Player, then this could be in this way:





$players = Player::model()->findAllByAttributes(

    array(

        'player_status' => 'paid'

    )

);




Later in the view file youc could iterate over $players and do some things:





foreach($players as $player)

{

    echo $player->name;

}




Thank you Sir Lastday for your help, I’ll show you the codes that I have. I tried the scope but it shows no record.

What shall I do here in order to fix my problem? also about the code that you gave.

Players Model


<?php


class Players extends CActiveRecord

	

{

public $verifyCode;

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Players the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'players';

	}

	public function scopes()

    {

        return array(

            'onuser'=>array(

                'condition'=>'players_status= 1'.Yii::app()->user->id,

            ),

            

        );

    }


	/**

	 * @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('players_lastname, players_firstname, players_birthday, players_contactno, players_age', 'required'),

			array('players_contactno, players_age', 'numerical', 'integerOnly'=>true),

			array('players_lastname, players_middlename, players_firstname', 'length', 'max'=>45),

			array('players_status', 'length', 'max'=>15),

		 array('verifyCode','captcha','allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'insert'), 




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

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

			array('id, players_lastname, players_middlename, players_firstname, players_birthday, players_contactno, players_age, players_status', 'safe', 'on'=>'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(

		

			'teamPlayers' => array(self::HAS_MANY, 'TeamPlayers', 'players_id'),

		

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'players_lastname' => 'Lastname',

			'players_middlename' => 'Middlename',

			'players_firstname' => 'Firstname',

			'players_birthday' => 'Birthday',

			'players_contactno' => 'Contact Number',

			'players_age' => 'Age',

			'players_status' => 'Players Status',

			'verifyCode'=>'Verification Code',

		);

	}

	

	

	

	/**

	 * 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.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('players_lastname',$this->players_lastname,true);

		$criteria->compare('players_middlename',$this->players_middlename,true);

		$criteria->compare('players_firstname',$this->players_firstname,true);

		$criteria->compare('players_birthday',$this->players_birthday,true);

		$criteria->compare('players_contactno',$this->players_contactno);

		$criteria->compare('players_age',$this->players_age);




		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

		public function getfullName(){

			return $this->players_lastname.', '.$this->players_firstname;

			}

	}




PlayersController





<?php


class PlayersController extends Controller

{

	

	public $layout='//layouts/column2';


	

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

			'postOnly + delete', // we only allow deletion via POST request

		);

	}


		public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view','indexUser'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update', 'captcha'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('register'),

				'users'=>array('*'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('@'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

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

			'model'=>$this->loadModel($id),

			'Players_status'=>$Paid

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Players;


		// Uncomment the following line if AJAX validation is needed

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


		if(isset($_POST['Players']))

		{

			$model->attributes=$_POST['Players'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}

	

	public function actionRegister()

	{

		$model=new Players;


		// Uncomment the following line if AJAX validation is needed

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


		if(isset($_POST['Players']))

		{

			$model->attributes=$_POST['Players'];

			if($model->save()){

				Yii::app()->user->setFlash('success',"Great! Please pay the specified registration fee at the PAF- BAdminton Court to join a tournament.");

				$this->redirect(array('register','id'=>$model->id));

			}else{

				Yii::app()->user->setFlash('error',"Sorry!");

			}

		}


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

			'model'=>$model,

		));

	}


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

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


		// Uncomment the following line if AJAX validation is needed

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


		if(isset($_POST['Players']))

		{

			$model->attributes=$_POST['Players'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'admin' page.

	 * @param integer $id the ID of the model to be deleted

	 */public function actions()

    {

        return array(

            // captcha action renders the CAPTCHA image displayed on the contact page

            'captcha'=>array(

                'class'=>'CCaptchaAction',

                'backColor'=>0xFFFFFF,

            ),

        );

    } 

	

	public function actionDelete($id)

	{

		$this->loadModel($id)->delete();


		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

		if(!isset($_GET['ajax']))

			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Players');

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

			'dataProvider'=>$dataProvider,

		));

	}

	public function actionIndexUser()

	{

		

		

		  $dataProvider=new CActiveDataProvider(Players::model()->onuser());

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

                        'dataProvider'=>$dataProvider,

                ));


	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new Players('search');

		$model->unsetAttributes();  // clear any default values

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

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


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

			'model'=>$model,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer the ID of the model to be loaded

	 */

	public function loadModel($id)

	{

		$model=Players::model()->findByPk($id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param CModel the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='players-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

}




While this is my view.php




<?php

/* @var $this PlayersController */

/* @var $model Players */


$this->breadcrumbs=array(

	'Players'=>array('index'),

	$model->id,

);


$this->menu=array(

	array('label'=>'List Players', 'url'=>array('index')),

	array('label'=>'Create Players', 'url'=>array('create')),

	array('label'=>'Update Players', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Players', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Players', 'url'=>array('admin')),

);

?>


<h1>View Players #<?php echo $model->id; ?></h1>


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

	'data'=>$model,

	'attributes'=>array(

		'id',

		'players_lastname',

		'players_middlename',

		'players_firstname',

		'players_birthday',

		'players_contactno',

		'players_age',

		'players_status',

	),

)); ?>



Thank you, sorry about this I’m really confused on what will I do right now.

First of all, this code looks strange for me:




public function scopes()

    {

        return array(

            'onuser'=>array(

                'condition'=>'players_status= 1'.Yii::app()->user->id,

            ),

            

        );

    }



For example if current user id is 23, then ‘condition’ key would be ‘players_status = 123’. Is it what you mean?

Seems to me you were going to write something like this:





public function scopes()

    {

        return array(

            'onuser' => array(

                'condition' => 'players_status = 1 AND user_id = ' . Yii::app()->user->id,

            ),

            

        );

    }




Where ‘user_id’ is the database table column identifying player.

Next, this code also looks strange:





public function actionView($id)

        {

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

                        'model'=>$this->loadModel($id),

                        'Players_status'=>$Paid

                ));

        }






$Paid variable here is not-defined and you should get notice. So initialize it with needed value (I assume it’s 1 but not sure).

Also if you are planning using ‘players_status’ as 0/1 value you could define it as ‘boolean’ in your Active Record validators list. This could be helpful to assign true / false for this value.




public function rules()

{

    return array(


       ....


       array('player_status', 'boolean'),


       ....


    );

}