【问题标题】:How to randomize multi-page form using SESSION variables?如何使用 SESSION 变量随机化多页表单?
【发布时间】:2018-05-09 14:46:36
【问题描述】:

所以基本上我在 php 中有一个会话登录/注册系统,并将用户重定向到 start.php 文件,该文件将显示从数组读取的导航。

这是我的场景:

用户使用login.htmllogin.php成功登录后,将用户重定向到start.php或允许用户转到start.php。在start.php 中,从sql1.php - sql10.php 中随机选择一个php(具有随机函数)并将其作为链接添加到start.php 中(显示示例,以便用户可以导航到所选页面)。

例如,如果选择了sql3.php,那么用户登录后就可以从start.php点击访问sql3.php

当用户点击下一页时,会从sql1.php-sql10.php中随机选择一个新的页面,不包括已经选择的页面。例如,如果用户点击sql3.phpsql3.php 的页面将显示新信息,例如(sql7.php 并有一个新的下一页)与新选择的页面(假设这次是 sql7.php 并且不应选择 sql3.php)。

每次用户登陆新页面时,允许用户单击上一页以返回上一页或下一页。如果在上述过程中随机选择了所有sql1.php - sql10.php 文件,则在最后 选择的页面,不显示下一页的链接。

代码如下

session_start();

if( !isset($_SESSION['username']) ) {
    header("Location: login.html");
}
$stack = array(); 
array_push($stack, 'sql1.php');
array_push($stack, 'sql2.php');
array_push($stack, 'sql3.php');
array_push($stack, 'sql4.php');
array_push($stack, 'sql5.php');
array_push($stack, 'sql6.php');
array_push($stack, 'sql7.php');
array_push($stack, 'sql8.php');
array_push($stack, 'sql9.php');
array_push($stack, 'sql10.php');

$orderedstack = array();
$unorderedstack = array();

if(isset($_SESSION['stack'], $_SESSION['orderedstack'], $_SESSION['unorderedstack'])){
    $stack = $_SESSION['stack'];
    $orderedstack = $_SESSION['orderedstack'];
    $unorderedstack = $_SESSION['unorderedstack'];
}

$count = count($unorderedstack);

if(!empty($stack)){
    $i = 0;
    while($i < 10){
        $i ++;
        $mixing = $stack[array_rand($stack)];
        /* array_rand randomly returns the the key of $stack. In this scenario it's mixing the keys of the array */
        array_push($unorderedstack, $mixing);
        $coreinfo = array_search($mixing,$stack);
        if($coreinfo!==false){
            unset($stack[$coreinfo]);
        }
        /* Using array_search, once the file is located, the keys of the found file is used for the buttons ($currentpage, $lastpage and $thenextpage)*/
    }
}

if(isset($_GET['next'])){
    if(count($orderedstack) < 10){
        $thenextpage = array_pop($unorderedstack);
        array_push($orderedstack, $thenextpage);
        $coreinfo = array_search($thenextpage,$orderedstack);
        if($coreinfo!==false && count($orderedstack) > 1){
            $currentfile = $orderedstack[$coreinfo-1];
        }
        if($coreinfo!==false && count($orderedstack) > 2){
            $lastpage = $orderedstack[$coreinfo-2];
        }
    }
}    

if(isset($_GET['back'])){
    $prevfile = array_pop($orderedstack);
    array_push($orderedstack, $prevfile);    
    $coreinfo = array_search($prevfile,$orderedstack);
    if($coreinfo!==false  && count($orderedstack) > 2){
        $currentfile = $orderedstack[$coreinfo-2];
    }
    if($coreinfo!==false && count($orderedstack) > 3){
        $lastpage = $orderedstack[$coreinfo-3];
    }
    if($coreinfo!==false && count($orderedstack) > 2){
        $thenextpage = $orderedstack[$coreinfo-1];
    }
    $switch = array_pop($orderedstack);
    array_push($unorderedstack, $prevfile);
}

/*Using echo we created HTML tags and elements to create a form. We added buttons to navigate between the files. The form's method is get, since it's insensitive info, and the button's names is back and next. Using if statements and binary search, we were able to make sure no files were being repeated.*/

?>    
<html>
<head>
<title>Start.php  - Reece, Courtney and Ros</title>    
<style>
.box{
    text-align: center;     
}    
p{
    color: red;
}
h1{
    text-align: center;
}
</style>    
</head>    
<body>    

<?php
if (count($orderedstack) <=1) {
    echo '<h1>start.php</h1>';
    echo '<div class="box">
    <form method="GET">';
        echo '<button name = "next">This button will select a random sql{i}.php file</button>
    </form>
    </div>';
} elseif (count($orderedstack) <=2) {
    echo '<h1><a href=" '. $currentfile . '">' . $currentfile . ' </a></h1>
    <br>';
    echo '<div class="box">
        <form method="GET">
            <button name="back">Go back to start.php starting page</button>';
            echo '<p>' . $currentfile . ' is being displayed</p>';
            echo '<button name = "next">The next sql{i}.php file will be ' . $thenextpage . '</button>
        </form>
    </div>';
} elseif (count($orderedstack) < 10) {
    echo '<h1><a href=" '. $currentfile . '">' . $currentfile . ' </a></h1>
    <br>';
    echo '<div class="box">
    <form method="GET">
        <button name="back">The previous sql{i}.php file was ' . $lastpage . '</button>';
        echo '<p>' . $currentfile . ' is being displayed</p>';
        echo '<button name = "next">The next sql{i}.php file will be ' . $thenextpage . '</button>
    </form>
    </div>';
} elseif (count($orderedstack) == 10) {
    echo '<h1><a href=" '. $currentfile . '">' . $currentfile . ' </a></h1>
    <br>';
    echo '<div class="box">
    <form method="GET">
        <button name="back">You reached the last file, press here to go back</button>';
        echo '<p>' . $currentfile . ' is being displayed</p>
    </form>
    </div>';
}

$_SESSION['stack'] = $stack;
$_SESSION['unorderedstack'] = $unorderedstack;
$_SESSION['orderedstack'] = $orderedstack;
?>

<button type="button"><a href="logout.php" >Logout</a></button>
</body>
</html>

想知道一种缩短的方法,但仍然使用堆栈,或者有没有其他方法可以在没有堆栈的情况下执行它?

如果可能的话,我希望为未来的项目优化它以供网络使用。

【问题讨论】:

  • 如果那是你的数组,为什么不$stack = ['sql1.php', 'sql2.php', ...];,而不是所有这些推送?
  • 建议阅读Standard PHP Library (SPL),特别是迭代器。
  • @Michael 你离开我们了吗?

标签: php arrays random pagination session-variables


【解决方案1】:

start.php 你需要:

// build a list of sql.php pages
$_SESSION[ 'sql_pages' ] = explode( ' ', 'sql'.implode( '.php sql', range(1, 10) ).'.php' );

// randomize those pages
shuffle($_SESSION[ 'sql_pages' ]);

$pop = array_pop($_SESSION[ 'sql_pages' ]);
echo '<a href="'.$pop.'">'.$pop.'</a>';

在您需要的 sql{x}.php 页面上:

if(isset($_SESSION[ 'sql_pages' ]) && count($_SESSION[ 'sql_pages' ]) > 0)
{
    $pop = array_pop($_SESSION[ 'sql_pages' ]);
    echo '<a href="'.$pop.'">'.$pop.'</a>';
}
else
{
    echo 'No more pages!';
}

【讨论】:

  • 我不知道我是否会说你需要使用implode()range()explode()。最好说你可以。我认为如果数组以无函数方式声明,一些开发人员会发现代码更具可读性。
  • @mickmackusa 谢谢,但如果我要“给某人一条鱼”,那么我至少会让他们努力将鱼拉到他们的船上。是的,我对函数的使用太过分了,但是非常欢迎 OP 以他们希望的任何方式定义该数组,因此有用的 cmets。另外,这道题有点家庭作业的味道,所以如果我的代码是逐字使用的,那么我希望导师足够聪明,能够衡量这个学生的能力并询问函数的复杂使用。
  • 我的感觉就像是一位父亲为他的孩子们做的事情。不知道。
【解决方案2】:

为了“优化”,我试图通过将重要组件存储在 SESSION 数组中来减少整个应用程序过程中的总函数调用。

您不需要将 sql#.php 字符串存储在堆栈数组中 - 只需存储整数,因为这是字符串的唯一可变部分。

随着项目的扩展,堆栈数组有一个调整点。此外,如果您想放弃 range() 调用,您甚至可以从目录中提取 sql 文件名。

通过使用include 将 sql 文件的内容放置在页面上,您无需重新配置表单目标。

我已经测试过这个可以在我的本地主机上工作...

代码:

<?php
session_start();
/* if(!isset($_SESSION['username']) ) {
    header("Location: login.html");
} */
?>

<html>
<body>
<form method="GET">
<?php
if (!isset($_SESSION['stack'], $_SESSION['id'])) {
    $_SESSION['stack'] = range(1, 10);                     // single point of adjustment as your project evolves/expands
    shuffle($_SESSION['stack']);                           // no subsequent rand() calls; shuffle once and only once
    $_SESSION['id'] = 0;                                   // init the current id
    $_SESSION['last_id'] = sizeof($_SESSION['stack']) - 1; // store the highest index
    echo "Ready to start this thing?";
    echo "<button>Let's Go!</button>";
} else {
    if (isset($_GET['back'])) {
        --$_SESSION['id'];                                 // decrement
    } elseif (isset($_GET['next'])) {
        ++$_SESSION['id'];                                 // increment
    }

    if ($_SESSION['id'] > $_SESSION['last_id']) {
        echo "<h1>Congratulations, you've finished.</h1>";
        session_destroy();                                     // just for my testing
        echo "<button>Play again?</button>";
    } else {
        echo "<div>DISPLAY sql{$_SESSION['stack'][$_SESSION['id']]}.php as page " . ($_SESSION['id'] + 1) . " of " . ($_SESSION['last_id'] + 1) ."</div>";
        //include("sql{$_SESSION['id']}.php");  // I recommend an absolute path to avoid monkeybusiness

        if ($_SESSION['id'] == 0) {
            echo "<button disabled>No Way</button>";
        } else {
            echo "<button name=\"back\">Back</button>";
        }
        if ($_SESSION['id'] == $_SESSION['last_id']) {
            echo "<button name=\"next\">Finish</button>";
        } else {
            echo "<button name=\"next\">Next</button>";
        }
    }
}
?>
</form>
</body>
</html>

【讨论】:

  • @Michael 这个问题的状态是什么?它似乎被遗弃了。
猜你喜欢
  • 2018-12-04
  • 2013-08-25
  • 2018-04-11
  • 1970-01-01
  • 2020-06-28
  • 2020-09-06
  • 2022-01-23
  • 1970-01-01
  • 2014-05-18
相关资源
最近更新 更多