English 中文(简体)
我如何选择与特定百分比机会相关的购买力平价变量?
原标题:How can I select a PHP variable that relates to a specific percentage chance?

我不想把我的头部 around在我在这里做的事情上,但有一些困难。 任何帮助或方向都将受到高度赞赏。

因此,在表格一中,我有一些价值,根据一阵列(精明名为1美元)预先界定。 这是我是如何做到的:

foreach ($array as $value) {

$query = "SELECT * FROM b_table WHERE levelname= $value ";
$result = runquery($query);
$num = mysql_numrows($result);

$i=0;
while ($i < 1) {
$evochance=@mysql_result($result,$i,"evochance");  // These values are integers that will add up to 100. So in one example, $evochance would be 60, 15 and 25 if there were 3 values for the 3 $value that were returned.
$i++;
}

Now I can t figure out where to go from here. $evochance represent percentage chances that are linked to each $value. Say the the favourable 60% one is selected via some function, it will then insert the $value it s linked with into a different table.

我知道它获得了帮助,但我来的是:

if (mt_rand(1,100)<$evochance) {
$valid = "True";
} 
else {
$valid = "False";
}
echo "$value chance: $evochance ($valid)<br />
"; // Added just to see the output.

这显然不是我所期望的。 而采用这种方法,我实际上可能具有一定数量的活跃百分比。 此外,这有时会在两种情况下产生一种手法。

So, I m an amateur learning the ropes and I ve had a go at it. Any direction is welcome. Thanks =)


** Edit 3(编辑):

页: 1 我对我的软弱解释感到ry惜;我也正在试图抓住这一局面。 希望能使人感到。

我阵列中的内容实例:$array = 阵列(一个、两个、三个)

不用说,<条码>有3项数值。 如同上文(尽管每当该书的操作时,它总是赢得3张奖)。 我从一个表格中抽取所有记录,该表含有特定领域的这些价值(所谓的“级名称”)。 由于这些价值观是每个记录独一无二的,因此,它只会随着价值观的出现而留下尽可能多的记录。 现在,表格中每个记录都有一个称为<条码>的栏目<>。 在该领域中,只有1至100人。 我先前询问的3份记录(在<编码>上)(foreach)()将有100份。 我需要决定哪些职能,我将根据它包含的起作用编号来加以使用。 如果是第99条,那么我就希望使用这一文字的99%。

HOWEVER... 我不想只保留一个加权机。 我想选择哪一个百分比 = 实际数值(正值=阵列的数值)。 这样,回归率与<代码>级名称<>代码/代码”有关,以便我能够选择这一编号(至少是我想要做的事)。

此外,为了澄清: 选定的记录将包含一个领域的独特信息 (这是我前面询问的<代码>$array中的独特数值之一)。 然后,我向民主党提交另一个具有这一价值的表格(表)。

So you see, the only thing I can t wrap my head around is the percentage chance selection function... It s quite complicated to me, and I might be going about it in a really round-about way, so if there s an alternative way, I d surely give it a try. To the answer I ve received: I m giving that a go now and seeing what I can do. Thanks for your input =)**

最佳回答

我认为,我理解你的要求。 如果我正确理解,“百分比机会”是选择记录的次数。 为了确定这一点,你必须实际跟踪记录何时通过在记录使用时增加价值来选定记录。 对于你的工作,没有任何“主人”,因此,“大人”或“大人”的职能不符合规定。

我在这里建议,如果我正确理解的话。 我将简化可读性守则:

<?php

$value =  one ;   // this can be turned into an array and looped through
$query1 = "SELECT sum(times_chosen) FROM `b_table` WHERE `levelname` =  $value ";
$count =  /* result from $query1 */
$count = $count + 1; // increment the value for this selection

// get the list of items by order of percentage chance highest to lowest
$query2 = "SELECT id, percentage_chance, times_chosen, name FROM `b_table` WHERE `levelname` =  $value  ORDER BY percentage_chance DESC";
$records = /* results from query 2 */
// percentage_chance INT (.01 to 1) 10% to 100%
foreach($records as $row) {
    if(ceil($row[ percentage_chance ] * $count) > $row[ times_chosen ]) {
        // chose this record to use and increment times_chosen
        $selected_record = $row[ name ];
        $update_query = "UPDATE `b_table` SET times_chosen = times_chosen + 1 WHERE id = $row[ id ]";
        /* run query against DB */
        // exit foreach (stop processing records because the selection is made)
        break 1;
    }
}

// check here to make sure a record was selected, if not, then take record 1 and use it but
// don t forget to increment times_chosen

?>

这应当说明自己,但简而言之,你正在把例行公事记下来,以便首先以最有可能获得的最高百分比来订购数据库记录。 那么,如果百分比大于总率,则会ski到下一个记录。

<><>UPDATE: 因此,鉴于这套记录:

$records = array(
    1 => array(
         id  => 1001,  percentage_chance  => .67,  name  =>  Function A ,  times_chosen  => 0,
    ),
    2 => array(
         id  => 1002,  percentage_chance  => .21,  name  =>  Function A ,  times_chosen  => 0,
    ),
    3 => array(
         id  => 1003,  percentage_chance  => .12,  name  =>  Function A ,  times_chosen  => 0,
    )
);

记录1将选定67%的时间,记录时间为21%,记录时间为312%。

问题回答
$sum = 0;
$choice = mt_rand(1,100);
foreach ($array as $item) {
    $sum += chance($item); // The weight of choosing this item
    if ($sum >= $choice) {
        // This is the item we have selected
    }
}

如果我读了你的话,你想从阵列中挑选其中一个项目,其中每个项目都可能选择。 这种方法将做到这一点。 确保概率达到100。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签