dtcpvz8162 2018-07-19 12:52
浏览 50
已采纳

权重逻辑实现

Let's assume i have 4 advertisement banner:

  • Banner A - Price 10$
  • Banner B - Price 5$
  • Banner C - Price 3$
  • Banner D - Price 1$

Now the requirement is to display ads based on the weightage. like Banner A will appear more time compare to Banner B. Banner B will appear more time compare to Banner C and so on.

There is no limit like how many times ad banner can display or how long the banner can display. When ever user refresh the screen they will get one banner, but based on above weightage.

Now i am not sure what kind of logic or mathematical formula is required for that. I want to build that logic in PHP hence adding the php tag.

what i am think is i will have one banner table and based on that we will generate one queue based on the Weightage logic and will send banner one by one.

  • 写回答

2条回答 默认 最新

  • dongyong1897 2018-07-20 05:51
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器