0

目前,我需要在 3 页上随机满足 3 个条件。因此,如果第一页随机获得第二个条件,那么第二页只能随机获得条件 1 或 3,依此类推。为此,我写入数据库(我已经使用数据库来记录活动)并在其他页面上调用它,以查看在以前的页面上已经满足了哪个条件。

我为第一页创建了一个随机函数:<?php $rand = rand(1, 3); ?>

第二页:

    if ($row['manip_1'] == 1){
        $man = rand(2,3);
    } elseif ($row['manip_1'] == 2){    //should do randomly 1 or 3,
        $man_a = rand(1,2);         //but don't know how to skip #2 in a 
            if($man_a == 2){    //random function, so solved it like this
                $man = 3;
            } else {
                $man = 1;
            }
    } else {
        $man = rand(1,2);
    }

第三页:

    if ($row['manip_1'] == 1 && $row['manip_2'] == 2){
        $man = 3;
    } elseif ($row['manip_1'] == 1 && $row['manip_2'] == 3){
        $man = 2;
    } elseif ($row['manip_1'] == 2 && $row['manip_2'] == 1){
        $man = 3;
    } elseif ($row['manip_1'] == 2 && $row['manip_2'] == 3){
        $man = 1;
    } elseif ($row['manip_1'] == 3 && $row['manip_2'] == 1){
        $man = 2;
    } else {
        $man = 1;
    }

现在的问题是我突然需要有第四页。这意味着还应该有第四个条件。这也意味着最后一页上的可能性数量现在是 25。我可以像以前一样再次对其进行编程,但我想知道是否没有更方便的方法来编写依赖于前几页条件的随机条件。在 4 页中,每个条件只能显示一次。

4

1 回答 1

2

Define an array of the conditions, and store them in the session, e.g.

$conditions = array(1,2,3,4);
$_SESSION['conditions'] = $conditions;

Randomization is easy:

array_shuffle($_SESSION['conditions']);

Then on each of the individual pages, you just pop off one of those values from the session:

<?php
# page 1
session_start();

$random_condition = array_pop($_SESSION['conditions']);

and do so for all the other pages:

<?php
#page n
session_start();
$random_condition = array_pop($_SESSION['conditions']);
于 2013-07-18T15:00:35.327 回答