0

我想知道如何在使用它从 process.php 上的 $laws 数组中检索特定文章之前验证来自 form.php 的数据

(form.php)

<form action="process.php" method="get">
Group:
<select name="group">
    <option value="group1">Group1</option>
    <option value="group2">Group2</option>
</select>

Chapter:
<input type="text" name="chapter">

Article:
<input type="text" name="article">

<input type="submit" value="Go to Article">

</form>

(进程.php)

<?php
session_start();
$laws=array(
       "group1"=>array(
                      "1"=>array(
                                "1"=>"This is article (1) in chapter (1) of (group1)",
                                "2"=>"This is article (2) in chapter (1) of (group1)",
                                "3"=>"This is article (3) in chapter (1) of (group1)",
                                ),
                      "2"=>array(
                                "1"=>"This is article (1) in chapter (2) of (group1)",
                                "2"=>"This is article (2) in chapter (2) of (group1)",
                                "3"=>"This is article (3) in chapter (2) of (group1)",
                                ),
                       ),
       "group2"=>array(
                      "1"=>array(
                                "1"=>"This is article (1) in chapter (1) of (group2)",
                                "2"=>"This is article (2) in chapter (1) of (group2)",
                                "3"=>"This is article (3) in chapter (1) of (group2)",
                                ),
                      "2"=>array(
                                "1"=>"This is article (1) in chapter (2) of (group2)",
                                "2"=>"This is article (2) in chapter (2) of (group2)",
                                "3"=>"This is article (3) in chapter (2) of (group2)",
                                ),

       )
       );


$_SESSION['group'] = $_GET['group'];
$_SESSION['chapter'] = $_GET['chapter'];
$_SESSION['article'] = $_GET['article'];

$group = $_SESSION['group'];
$chapter = $_SESSION['chapter'];
$article = $_SESSION['article'];



// Echo Article from Laws Array

echo $group . " chapter" . $chapter . " article" . $article . "<br/>";
echo $laws[$group][$chapter][$article];

?>

<br/>

<a href="sample.php">PREVIOUS Articles</a> <a href="sample.php">NEXT Articles</a>

问题 1

在使用来自 $_GET 变量的数据设置 $_SESSION 值之前,如何使用以下方案验证表单中的 $_GET 值?

Data in CHAPTER and ARTICLE input forms:
    are not empty;
    are numbers;
    are not less than or greater than the existing array Chapters and Article numbers;
    do not have spaces;
    do not have decimals;
    do not have letters;
    do not have negative, positive and; or other signs;

问题2

我应该在下一篇文章和上一篇文章链接中添加什么 PHP 代码,以在当前回显的文章之后回显下一篇文章?

我应该如何将此代码添加到链接中;

由于 NEXT Article 和 PREVIOUS Article 链接将不再访问特定章节中不存在的数组,以防止 ERRORS 被回显,因此当最后一篇文章或第一篇文章被回显时,应该对页面执行什么操作。

谢谢!

4

1 回答 1

0

问题1:你试过intval吗?http://php.net/manual/en/function.intval.php 所以,按照你想要的顺序,

if(isset($_GET['group']) && !empty($_GET['group']) && is_int($_GET['group'])){
    $groupval = intval($_GET['group']);
    $_SESSION['group'] = ($groupval > 0)? $groupval : 1; //A group cannot be 0
}

您也可以对章节和文章使用类似的语句。

问题 2:您只需使用返回给您的 get 值,加一和减一。例如:

echo '<a href="process.php?group=' . $group . '&article=' . ($article+1) . '">Next Article</a>'

请注意,如果这是最后一篇文章,您必须编写检查,然后继续下一组/接下来的内容。

于 2013-02-15T15:33:43.073 回答