Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP |随机顺序包含文件_Php - Fatal编程技术网

PHP |随机顺序包含文件

PHP |随机顺序包含文件,php,Php,我有一个代码中包含的文件 逻辑是这些文件包含用户必须查看的信息(通信),这20个文件的目标是以随机顺序向用户显示它们 现在,代码计算用户注册日期,并根据过去的天数显示用户必须查看的文件 <?php // Declare and define two dates $date1 = strtotime($data_registrazione); $date2 = strtotime(date('Y-m-d')); // Formulate the Difference between two

我有一个代码中包含的文件

逻辑是这些文件包含用户必须查看的信息(通信),这20个文件的目标是以随机顺序向用户显示它们

现在,代码计算用户注册日期,并根据过去的天数显示用户必须查看的文件

<?php
// Declare and define two dates
$date1 = strtotime($data_registrazione);
$date2 = strtotime(date('Y-m-d'));

// Formulate the Difference between two dates
$diff = abs($date2 - $date1);


// To get the year divide the resultant date into
// total seconds in a year (365*60*60*24)
$years = floor($diff / (365*60*60*24));


// To get the month, subtract it with years and
// divide the resultant date into
// total seconds in a month (30*60*60*24)
$months = floor(($diff - $years * 365*60*60*24)
    / (30*60*60*24));


// To get the day, subtract it with years and
// months and divide the resultant date into
// total seconds in a days (60*60*24)
$days = floor(($diff - $years * 365*60*60*24 -
        $months*30*60*60*24)/ (60*60*24));

?>
<?php if ($days == 0) :?>
    <?php include 'referrer_pro/giorno_zero.php'; ?>
<?php elseif ($days == 1) :?>
    <?php include 'referrer_pro/giorno_uno.php'; ?>
<?php elseif($days == 2) : ?>
    <?php include 'referrer_pro/giorno_due.php'; ?>
<?php elseif($days == 3) : ?>
    <?php include 'referrer_pro/giorno_tre.php'; ?>
<?php elseif($days == 4) : ?>
    <?php include 'referrer_pro/giorno_quattro.php'; ?>
<?php elseif($days == 5) : ?>
    <?php include 'referrer_pro/giorno_cinque.php'; ?>
<?php elseif($days == 6) : ?>
    <?php include 'referrer_pro/giorno_sei.php'; ?>
<?php elseif($days == 7) : ?>
    <?php include 'referrer_pro/giorno_sette.php'; ?>
<?php elseif($days == 8) : ?>
    <?php include 'referrer_pro/giorno_otto.php'; ?>
<?php elseif($days == 9) : ?>
    <?php include 'referrer_pro/giorno_nove.php'; ?>
<?php elseif($days == 10) : ?>
    <?php include 'referrer_pro/giorno_dieci.php'; ?>
<?php elseif($days == 11) : ?>
    <?php include 'referrer_pro/giorno_undici.php'; ?>
<?php elseif($days == 12) : ?>
    <?php include 'referrer_pro/giorno_dodici.php'; ?>
<?php elseif($days == 13) : ?>
    <?php include 'referrer_pro/giorno_tredici.php'; ?>
<?php elseif($days == 14) : ?>
    <?php include 'referrer_pro/giorno_quattoridic.php'; ?>
<?php elseif($days == 15) : ?>
    <?php include 'referrer_pro/giorno_quindici.php'; ?>
<?php elseif($days == 16) : ?>
    <?php include 'referrer_pro/giorno_sedici.php'; ?>
<?php elseif($days == 17) : ?>
    <?php include 'referrer_pro/giorno_diciassette.php'; ?>
<?php elseif($days == 18) : ?>
    <?php include 'referrer_pro/giorno_diciotto.php'; ?>
<?php elseif($days == 19) : ?>
    <?php include 'referrer_pro/giorno_diciannove.php'; ?>
<?php elseif($days == 20) : ?>
    <?php include 'referrer_pro/giorno_venti.php'; ?>
<?php endif; ?>

如何更改结构以不再显示基于过去几天的不同文件

但是,它是否会以随机顺序显示文件,而不考虑已过的天数?


<?php
$input = array('referrer_pro/giorno_zero.php', 'referrer_pro/giorno_uno.php', 'referrer_pro/giorno_due.php', 'referrer_pro/giorno_tre.php', 'referrer_pro/giorno_quattro.php');
$rand_keys = array_rand($input, 2);
include $input[$rand_keys[0]] . "\n";
include $input[$rand_keys[1]] . "\n";
?>

这是如何将所有文件路径放在一个数组中并随机拾取它们。如果用数组长度替换数组随机代码中的2,则将随机获得所有值。PHP在一次代码执行中不会选择同一元素两次。

要计算您所希望的大致随机数,但这将在注册日期起的任意天数内工作,您需要做的是获得天数差的模20

差异计算也可以简化为这样

# change the format to match how your date is being presented to this code
$regDate = (new DateTime())->createFromFormat('Y-m-d', '2000-11-16');
$diff = $regDate->diff(new DateTime());
$days = $diff->days % 20;
echo "Random_ish number less than or equal to 20 = $days";

因此,您可以像当前一样使用
$days

这是一种非常无组织的。。。尝试使用switch语句将所有文件路径放在一个数组中,并选择一个随机元素。我假设您考虑过使用
giorno_0.php
而不是
giorno_zero.php
重命名文件,然后这将是Ezzie peezy,而且绝对没有必要在每行连续代码周围放置
!!使代码几乎不可读,并且unmaintainable@El_Vanja如何排列文件并选择随机顺序?Lelio,谢谢你的回答,我想知道我是如何在回答评论中写道的:如果我这样做的话,通过删除结构:“计算哪一天过去了”,这意味着每次刷新页面时,用户会看到不同的随机变量,对吗?我在想,怎么说,从注册之日算起,无限天,然后放入随机文件,因为现在它达到了20天。为什么你应该删除“计算已经过去了多少天”的部分?我想说的是,一旦你用你的代码得到数组,你就可以以随机顺序显示20个文件。然后,您可以根据自己的喜好调整代码以提取更少或更多的文件。顺便说一句,您的实际代码在功能方面没有什么意义,但这只是我的拙见。我不想完全删除它,相反,添加一段代码,从注册日期算起,我最多不超过20天,但是,从注册之日起无限天,每天随机选择一个文件,这使我无法做到:(应用你答案的代码,甚至不会向我显示文件的内容,而只显示文件的名称。)