Thanks to @Rakesha Shastri, @Lawrence Cherone i am able to figured it out.
What i have done is created 2 tables.
CREATE TABLE `ad_banner` (
`banner_id` int(11) NOT NULL AUTO_INCREMENT,
`banner_name` varchar(255) NOT NULL,
`weightage` int(3) NOT NULL,
`price` decimal(5,2) NOT NULL,
PRIMARY KEY (`banner_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
CREATE TABLE `ad_banner_queue` (
`ad_quque_id` int(11) NOT NULL AUTO_INCREMENT,
`banner_name` varchar(255) NOT NULL,
`is_sent` tinyint(1) NOT NULL,
PRIMARY KEY (`ad_quque_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
After that added few entries in ad_banner table.
public function actionGeneratequeue(){
/* First check that there is existing queue, if so don't generate it */
$data_exist = Yii::app()->db->createCommand()
->select('ad_quque_id')
->from('ad_banner_queue')
->where('is_sent=0')
->queryScalar();
if($data_exist===false){
/* Fetch all banner */
$ads = Yii::app()->db->createCommand()
->select('*')
->from('ad_banner')
->queryAll();
if(!empty($ads)){
foreach($ads as $ad){
/* Make entry as per that weightage, example, if weightage is 10 then make entry 10 times */
for($i=1;$i<=$ad['weightage'];$i++){
$command = Yii::app()->db->createCommand();
$command->insert('ad_banner_queue', array(
'banner_name' => $ad['banner_name'],
));
}
}
} else{
echo "No Banner Found!";
exit;
}
} else{
echo "Queue already exist";
exit;
}
}
/* Fetch banner */
public function actionFetchbanner(){
/* Fetch the Random from table */
$ads_queue = Yii::app()->db->createCommand()
->select('*')
->from('ad_banner_queue')
->where('is_sent=0')
->order('RAND()')
->queryRow();
if($ads_queue===false){
$this->actionGeneratequeue();
}
/* Now, marked that one as is_sent */
$command = Yii::app()->db->createCommand();
$command->update('ad_banner_queue', array(
'is_sent'=>1,
), 'ad_quque_id=:ad_quque_id', array(':ad_quque_id'=>$ads_queue['ad_quque_id']));
echo "<pre>";print_r($ads_queue);echo "</pre>";exit();
}
Then simple call the actionFetchbanner
function.