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>

(process.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コードを追加する必要がありますか?

このコードをリンクに追加するにはどうすればよいですか。

エラーがエコーバックされるのを防ぐために、次の記事と前の記事のリンクが特定のチャプターに存在しない配列にアクセスしなくなったため、最後の記事または最初の記事がエコーされたときにページに何をすべきか。

ありがとうございました!

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 値にプラス 1 とマイナス 1 を使用するだけです。例えば:

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

これが最後の記事である場合は、チェックを作成する必要があることに注意してください。その後、次のグループ/次の記事に進みます。

于 2013-02-15T15:33:43.073 に答える