bonnie
(Africanbusinesszone)
1
Hi, All any help asap will be highly appreciated.
here my issue is converting this code to yii way especially the mysql_num_rows
public function votedBefore($item_id){
$ip = $_SERVER['REMOTE_ADDR'];
$query = "SELECT * FROM {{item_votes}} WHERE ip = '$ip' AND classified = '$item_id'";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){ // already voted
return true;
} elseif(mysql_num_rows($result)==0){ // haven't voted
return false;
}
}
}
How can this be converted to yii way again - mysql_fetch_assoc.
public function countUpVotes($item_id) {
$query = "SELECT * FROM {{votes}} WHERE item_id= '$item_id' AND `vote_value`>0";
$result = mysql_query($query);
$votes = 0;
while($row = mysql_fetch_assoc($result)){
$votes+=$row['vote_value'];
}
return (int) $votes;
}
jacmoe
(Jacob Moen)
2
[color="#006400"]/* moved to general Yii 1.1 help forum */[/color]
phtamas
(Phtamas)
3
First, use MySQL aggregate functions to retrieve data you really need:
SELECT COUNT(*) FROM item_votes WHERE id=...
SELECT SUM(vote_value) FROM votes WHERE item_id=...
then read the DAO section of guide.
bonnie
(Africanbusinesszone)
4
Thanks I got it working by doing some research. And here is the final code.
I don’t know if this is ok.
Now i need to get the results via Ajax.
public function voteUp($vote)
{
$ip = $_SERVER['REMOTE_ADDR'];
$criteria = new CDbCriteria;
$criteria->condition='itme_id = :itemId AND ip_address = :Ip';
$criteria->params=array(':itemId'=>$this->id, ':Ip' => $ip);
$votes = Votes::model()->count($criteria);
if($votes == 0){
$vote->classified_id = $this->id;
$vote->ip_address = $ip;
$vote->save();
}else{
return false;
}
}