【发布时间】:2015-06-30 22:02:53
【问题描述】:
我正在创建一个广告系统,它会更频繁地显示出价最高者的广告。
这是我正在使用的表结构的示例,但经过简化...
+----+----------+------------------------+----------------------+-----+
| id | name | image | destination | bid |
+----+----------+------------------------+----------------------+-----+
| 1 | abc, co | htt.../blah | htt...djkd.com/ | 3 |
+----+----------+------------------------+----------------------+-----+
| 2 | facebook | htt.../blah | htt...djkd.com/ | 200 |
+----+----------+------------------------+----------------------+-----+
| 3 | google | htt.../blah | htt...djkd.com/ | 78 |
+----+----------+------------------------+----------------------+-----+
现在,我正在从数据库中选择值,然后将它们插入到一个数组中,然后随机选择一个,类似于以下内容:
$ads_array = [];
$ads = Ad::where("active", "=", 1)->orderBy("price", "DESC");
if ($ads->count() > 0) {
$current = 0;
foreach ($ads->get() as $ad) {
for ($i = 0; $i <= ($ad->price == 0 ? 1 : $ad->price); $i++) {
$ads_array[$current] = $ad->id;
$current++;
}
}
$random = rand(0,$current-1);
$ad = Ad::where("id", "=", $ads_array[$random])->first();
...
}
所以,本质上这是在将广告的 ID 插入数组1*$bid 次。遗憾的是(出于显而易见的原因),这是非常低效的,但这是我能想到的最佳方式。
有没有更好的方法从我的数据库中挑选出随机广告?同时仍然让出价更高的人有更高的展示机会?
【问题讨论】:
-
@b.enoit.be 说不要使用答案......不过谢谢。 :)
标签: php sql laravel probability ads