Wyświetlanie Img Z Danym Parametrem

Witam.

Posiadam w bazię danych kolumne "piorytet" która odpowiada za piorytet danego stworzonego zadania.

Przy tworzeniu nowych zadań posiadam checkbox w którym zaznaczam wartość piorytetu "1,2 lub 3". Następnie dana wartość zapisuje się w bazie danych.

Przy wyświetlaniu zadań, w kolumnie "piorytet" występują wartośći liczbowe.

W jaką funkcje stworzyć by podczas wyświetlania zadań w kolumnie "piorytet" zamiast wyświetlania się "value" wyświetlał się obrazek z danym piorytetem.

Domyślam się ze muszą stworzyć funkcję w kontrolerze, przesłać ją do widoku i wywołać.

W models\task.php stworzyłem funckje :


    public function valueShow() {

        if ($value === 1) {

            echo "<img src=\"css/p_1.png\">";  

        }elseif ($value === 2){

            echo "<img src=\"css/p_2.png\">";  

        }else{

            echo "<img src\"css/p_3.png\">";  

        }

    }

}



W TaskController.php dopisałem :


public function actionIndex() {

        $model = new Task();

        $dataProvider = new CActiveDataProvider('Task');

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

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

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

        ));

I nie wiem za bardzo jak wyświetlić to w widoku _view.php :


<?php $data->piorytet; ?>

  1. Nadpisujesz sobie zmienną DataProvider, czy to zamierzone ?



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

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

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

        ));



  1. W modelu możesz sobie zrobić np. to:



public function getPriorytet()

{

   if($this->priorytet==1) $image='p_1.png';

   if($this->priorytet==2) $image='p_2.png';

   if($this->priorytet==3) $image='p_3.png';

   return $image;

}



  1. W widoku

[html]<img src="/css/<?php $data->getPriorytet();?>">[/html]

Wyrzuca mi :


Undefined variable: image 

A w TaskController wrzucić funkcje getPriorytet()? Czy nie przejdzie?.

  1. podeślij kod modelu

TASK.PHP :


<?php


/**

 * This is the model class for table "task".

 *

 * The followings are the available columns in table 'task':

 * @property integer $id

 * @property string $content

 * @property string $deadline

 * @property string $created

 * @property string $finished

 * @property integer $author_id

 * @property integer $user_id

 * @property integer $project_id

 *

 * The followings are the available model relations:

 * @property Message[] $messages

 * @property Project $project

 * @property User $user

 */

class Task extends CActiveRecord {


    public $user_name;

    public $image;

    public $file;


    /**

     * Returns the static model of the specified AR class.

     * @param string $className active record class name.

     * @return Task 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 'task';

    }


    /**

     * @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('created, finished, author_id, user_id, project_id', 'required'),

            array('priorytet, author_id, deadline, user_id, project_id', 'required'),

            array('author_id, project_id', 'numerical', 'integerOnly' => true),

            array('content', 'length', 'max' => 400),

            array('deadline, created, finished', 'length', 'max' => 45),

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

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

            array('[b]user_name[/b], id, content, deadline, created, finished, author_id, user_id, project_id', 'safe', 'on' => 'search'),

            array('image', 'file', 'types' => 'jpg, gif, png, jpeg', 'allowEmpty' => TRUE),

            array('file', 'file', 'types' => 'zip, rar', 'allowEmpty' => true),

        );

    }


    /**

     * @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(

            'messages' => array(self::HAS_MANY, 'Message', 'task_id'),

            'project' => array(self::BELONGS_TO, 'Project', 'project_id'),

            'user' => array(self::BELONGS_TO, 'User', 'user_id'),

            'author' => array(self::BELONGS_TO, 'User', 'author_id'),

        );

    }


    /**

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

     */

    public function attributeLabels() {

        return array(

            'id' => 'ID',

            'content' => 'Nazwa',

            'deadline' => 'Czas realizacji',

            'created' => 'Stworzony',

            'finished' => 'Zakończony',

            'author_id' => 'Autor',

            'user_id' => 'Użytkownik',

            'project_id' => 'Projekt',

        );

    }


    /**

     * 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('content', $this->content, true);

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

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

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

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

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

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


        return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

        ));

    }


//    public function getProjectTasks() {

//

//        $select = Yii::app()->db->createCommand()

//                ->select(array('task.*', 'u.name as user_name', 'a.name as author_name'))

//                ->from('task')

//                ->join('user u', 'u.id=task.user_id')

//                ->join('user a', 'a.id=task.author_id');

//

//

//

//        return $select->queryAll();

//    }


    public function searchWithUser() {

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

        // should not be searched.


        $criteria = new CDbCriteria;

        $criteria->with = 'user'; //dla jednej tabeli

        $criteria->with = array('user', 'author', 'project'); //dla kilku tabel


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

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

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

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

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

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

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

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


        return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

        ));

    }


    public function getPriorytet() {

        if ($this->priorytet == 2)

            echo '<div id="priorytet" style="background-position:-50px 0;"></div>';

        if ($this->priorytet == 3)

            echo '<div id="priorytet" style="background-position:-25px 0;"></div>';

        if ($this->priorytet == 4)

            echo '<div id="priorytet" style="background-position:1px 0;"></div>';

        return $this;

    }


}



  1. No nie widzę w nim funkcji, którą ci podałem wcześniej.

  2. No i pytanie co ma robić twoja funkcja getPriorytet() ?

wszystko fajnie w Twojej funkcji tylko ona w jakis sposob musi sie dowiedziec jaka ta value jest wiec to powinien byc parametr i wtedy to ladnie pieknie sobie wyswietlasz w bardzo prosty sposob:


$model->valueShow($value)

(jezeli na przyklad w cgridview wyswietlasz to $model zamieniasz na $data)

tylko pamietaj ze jeszcze musisz przekazac model do widoku w kontrolerze

czyli dodac :


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

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

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

            'model'=>$model, 

        ));

Usunąłem w ogole $model->ValueShow(), w kontrolerze, i smiga, poprzez funkcje, getPriorytet(), ale jest problem z checkBoxlist w którym każdy z trzech checkboxsów daje mi value 1 do bazy a potrzebuje , 1 ,2 ,3.

O to kod:


 <?php echo $form->labelEx($model, 'priorytet'); ?><br>

        <?php echo $form->checkBoxList($model, 'priorytet', array(1=>'<div id="p1"></div>',2=>'<div id="p2"></div>',3=>'<div id="p3"></div>')); ?><br>