Yii Framework Forum: Wyświetlanie Img Z Danym Parametrem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Wyświetlanie Img Z Danym Parametrem Jak wyświetlić zdjecię z danym parametrem value? Rate Topic: -----

#1 User is offline   kopecldz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 20-February 13

Posted 26 February 2013 - 03:40 AM

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; ?>

0

#2 User is offline   .viktor. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 25-August 10
  • Location:Sokołów Podlaski, Poland

Posted 26 February 2013 - 03:57 AM

1. Nadpisujesz sobie zmienną DataProvider, czy to zamierzone ?
        $this->render('index', array(
            'dataProvider' => $model->searchWithUser(),
            'dataProvider' => $model->valueShow(),
        ));

2. 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;
}


3. W widoku

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

0

#3 User is offline   kopecldz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 20-February 13

Posted 26 February 2013 - 04:07 AM

Wyrzuca mi :

Undefined variable: image 


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

#4 User is offline   .viktor. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 25-August 10
  • Location:Sokołów Podlaski, Poland

Posted 26 February 2013 - 08:05 AM

1. podeślij kod modelu
0

#5 User is offline   kopecldz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 20-February 13

Posted 26 February 2013 - 08:25 AM

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;
    }

}

0

#6 User is offline   .viktor. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 25-August 10
  • Location:Sokołów Podlaski, Poland

Posted 26 February 2013 - 08:40 AM

1. No nie widzę w nim funkcji, którą ci podałem wcześniej.
2. No i pytanie co ma robić twoja funkcja getPriorytet() ?
0

#7 User is offline   mirunho 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 19-December 12
  • Location:Gdansk, Poland

Posted 27 February 2013 - 02:38 AM

View Postkopecldz, on 26 February 2013 - 03:40 AM, said:



W models\task.php stworzyłem funckje :
    public function valueShow($value) {
        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\">";  
        }
    }
}


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, 
        ));

0

#8 User is offline   kopecldz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 20-February 13

Posted 27 February 2013 - 03:37 AM

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>

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users