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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题