(谷歌网站优化器的一种)PHP 中的随机化(但智能、快速且精简)

发布于 2024-10-12 08:32:41 字数 618 浏览 1 评论 0原文

每次有人访问我的网站时,我都会显示三个选项之一(A、B、C)。如果用户喜欢该选项,他就会点击它。我想找到一种方法来显示点击次数较少的选项。在 PHP 中执行此操作的最佳方法是什么?

我通过简单地在数组中添加“投票”来保存 MongoDB 中的点击:

$option[]='a';//one click on option A
$option[]='b';//one click on option B
$option[]='b';//another click on option B

try{
 $m=new Mongo();
 $c=$m->db->clicks;
 $c->save($option);
 $m->close();
}
catch(MongoConnectionException $e){ die('Error connecting to MongoDB server. ');}
catch(MongoException $e){ die('Error: '.$e->getMessage());} 

这将打印:

Array
(
    [0] => a
    [1] => b
    [2] => b
)

Every time someone visits my site, I show one of three options (A, B, C). If the user likes the option, he clicks on it. I want to find a way to show the options that receive less clicks less frequently. What's the best way of doing this in PHP?

I am saving the clicks in MongoDB by simply adding a "vote" in an array:

$option[]='a';//one click on option A
$option[]='b';//one click on option B
$option[]='b';//another click on option B

try{
 $m=new Mongo();
 $c=$m->db->clicks;
 $c->save($option);
 $m->close();
}
catch(MongoConnectionException $e){ die('Error connecting to MongoDB server. ');}
catch(MongoException $e){ die('Error: '.$e->getMessage());} 

This prints:

Array
(
    [0] => a
    [1] => b
    [2] => b
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-10-19 08:32:41

不确定我是否正确理解你的问题。但如果我这样做了,那么以下可能是一种相当幼稚,甚至可能是冗长的方式来做我认为你想做的事情:

// assume the following fictional values,
// that is, the amount of clicks each option has received thusfar
$clicks = array(
  'A' => 10,
  'B' => 40,
  'C' => 50
);

// what is the total amount of clicks?
$totalClicks = array_sum( $clicks );

// determine the lower bound percentages of the option clicks
$clickPercentageBounds = array(
  'A' => 0,
  'B' => ( ( $clicks[ 'A' ] + 1 ) / $totalClicks ) * 100,
  'C' => ( ( $clicks[ 'A' ] + $clicks[ 'B' ] + 1 ) / $totalClicks ) * 100
);

// get random percentage
$rand = mt_rand( 0, 100 );

// determine what option to show, based on the percentage
$option = '';
switch( true )
{
    case $rand < $clickPercentageBounds[ 'B' ]:
        $option = 'A';
        break;
    case $rand < $clickPercentageBounds[ 'C' ]:
        $option = 'B';
        break;
    default:
        $option = 'C';
        break;
}

var_dump( $option );

Not sure if I understand your question correctly. But if I did, then the following is perhaps a rather naive, and perhaps even verbose way of doing what I think you want to do:

// assume the following fictional values,
// that is, the amount of clicks each option has received thusfar
$clicks = array(
  'A' => 10,
  'B' => 40,
  'C' => 50
);

// what is the total amount of clicks?
$totalClicks = array_sum( $clicks );

// determine the lower bound percentages of the option clicks
$clickPercentageBounds = array(
  'A' => 0,
  'B' => ( ( $clicks[ 'A' ] + 1 ) / $totalClicks ) * 100,
  'C' => ( ( $clicks[ 'A' ] + $clicks[ 'B' ] + 1 ) / $totalClicks ) * 100
);

// get random percentage
$rand = mt_rand( 0, 100 );

// determine what option to show, based on the percentage
$option = '';
switch( true )
{
    case $rand < $clickPercentageBounds[ 'B' ]:
        $option = 'A';
        break;
    case $rand < $clickPercentageBounds[ 'C' ]:
        $option = 'B';
        break;
    default:
        $option = 'C';
        break;
}

var_dump( $option );
北城半夏 2024-10-19 08:32:41

您可以使用 PHP 和数据库来完成此操作,但您可能更喜欢使用 Google Website Optimizer,我认为它提供了这个选项效果很好。

You CAN do it with PHP and a db, but you might prefer using Google Website Optimizer, I think it offers that option and works quite well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文