Yii Framework Forum: Average - Yii Framework Forum

Jump to content

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

Average Rate Topic: -----

#1 User is offline   Foghist 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 31-January 13

Posted 02 February 2013 - 04:56 PM

Hey guys.

I'm wondering: how to get the average of a column in my database table displayed? And furthermore, how to display the average of a column for each row that has a certain integer in an another column?
Basically like a rating system, in which i have a rating column, a video column and a user column. I want to find the average rating for a certain video.

Can anybody help me?

Btw, i've already looked at the Statistical query part of The Guide, but i doesn't understand it fully...
It would help with an example :)
0

#2 User is offline   Luke Jurgs 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 16-October 11
  • Location:Australia

Posted 02 February 2013 - 06:24 PM

class Video extends CActiveRecord {
  public function relations() {
    return array(
      //'relation name' => array(self::STAT, 'table name', 'related column', query options)
      'averageRating' => array(self::STAT, 'video_rating', 'video_id' 'select' => 'AVG(rating)'),
    );
  }
}


Should give you some ideas.

View PostFoghist, on 02 February 2013 - 04:56 PM, said:

Hey guys.

I'm wondering: how to get the average of a column in my database table displayed? And furthermore, how to display the average of a column for each row that has a certain integer in an another column?
Basically like a rating system, in which i have a rating column, a video column and a user column. I want to find the average rating for a certain video.

Can anybody help me?

Btw, i've already looked at the Statistical query part of The Guide, but i doesn't understand it fully...
It would help with an example :)

0

#3 User is offline   Foghist 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 31-January 13

Posted 05 February 2013 - 04:57 PM

Thanks, it worked like a charm :)

Now, just another question: What if i have multiple things of each video i want to be able to rate, but i don't want to have more columns in my table besides an integer column that detects what kind of thing you are rating (like quality=1, acting=2 etc.)? So to display a rating, i'll want to ask the database to give me a rating with a certain id, and a certain type.
0

#4 User is offline   Foghist 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 31-January 13

Posted 06 February 2013 - 06:57 AM

I'm guessing i have to add a condition to the relation, such as 'condition' => 'type=$type' and then somehow get $type on the specific page, but i don't know how to do that.
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