如何清理PHP代码

时间:2015-10-29 13:35:38

标签: php

所以昨天我很无聊并创建了一个闪存卡网络应用程序。我基本上使用了一个HTML登陆页面,它有10个不同的文本区域,可以将从文本区域输入的内容推送到PHP变量中。每个文本区域都有一个名称" questionone" " questiontwo"等等。但是,有更清洁,更简单的方法吗?

<?php

$questionone = $_POST['questionone'];
$questiontwo = $_POST['questiontwo'];
$questionthree = $_POST['questionthree'];
$questionfour = $_POST['questionfour'];
$questionfive = $_POST['questionfive'];
$questionsix = $_POST['questionsix'];
$questionseven = $_POST['questionseven'];
$questioneight = $_POST['questioneight'];
$questionnine = $_POST['questionnine'];
$questionten = $_POST['questionten'];
$answerone = $_POST['answerone'];
$answertwo = $_POST['answertwo'];
$answerthree = $_POST['answerthree'];
$answerfour = $_POST['answerfour'];
$answerfive = $_POST['answerfive'];
$answersix = $_POST['answersix'];
$answerseven = $_POST['answerseven'];
$answereight = $_POST['answereight'];
$answernine = $_POST['answernine'];
$answerten = $_POST['answerten'];

?>
<div class="two"><p><?php echo "$questionone"; ?></p></div>
<div class="three"><p><?php echo "$questiontwo"; ?></p></div>
<div class="two"><p><?php echo "$questionthree"; ?></p></div>
<div class="three"><p><?php echo "$questionfour"; ?></p></div>
<div class="two"><p><?php echo "$questionfive"; ?></p></div>
<div class="three"><p><?php echo "$questionsix"; ?></p></div>
<div class="two"><p><?php echo "$questionseven"; ?></p></div>
<div class="three"><p><?php echo "$questioneight"; ?></p></div>
<div class="two"><p><?php echo "$questionnine"; ?></p></div>
<div class="three"><p><?php echo "$questionten"; ?></p></div>
</div>

5 个答案:

答案 0 :(得分:2)

你可以在html中使用这样的数组:

<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"/>

并在php中处理这些数据:

<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />

答案 1 :(得分:2)

虽然其他答案似乎是正确的,但大多数答案都没有使用htmlspecialchars或issets来防止警告。尝试这样的事情:

<?php $trusted_post = array( 'questionone', 'questiontwo', 'questionthree', 'questionfour', 'questionfive', 'questionsix', 'questionseven', 'questioneight', 'questionnine', 'questionten', 'answerone', 'answertwo', 'answerthree', 'answerfour', 'answerfive', 'answersix', 'answerseven', 'answereight', 'answernine', 'answerten'); ?>
<?php foreach($trusted_post as $loopKey => $postKey): ?>
    <?php if(isset($_POST[$postKey])): ?>
        <div class="<?php echo ($loopKey%2===0)?'two':'three'; ?>"><p><?php echo htmlspecialchars( $_POST[ $postKey ] ); ?></p></div>
    <?php endif; ?>
<?php endforeach; ?>

答案 2 :(得分:0)

<?php
foreach ($_POST as $key => $post) {
    ${$key} = $post;
}
?>

<div class="two"><p><?php echo "$questionone"; ?></p></div>
<div class="three"><p><?php echo "$questiontwo"; ?></p></div>
<div class="two"><p><?php echo "$questionthree"; ?></p></div>
<div class="three"><p><?php echo "$questionfour"; ?></p></div>
<div class="two"><p><?php echo "$questionfive"; ?></p></div>
<div class="three"><p><?php echo "$questionsix"; ?></p></div>
<div class="two"><p><?php echo "$questionseven"; ?></p></div>
<div class="three"><p><?php echo "$questioneight"; ?></p></div>
<div class="two"><p><?php echo "$questionnine"; ?></p></div>
<div class="three"><p><?php echo "$questionten"; ?></p></div>
</div>

OR

<?php
$i = 0;
foreach ($_POST as $post) {
    if ($i%2) {
        echo '<div class="two"><p>' . $post . '</p></div>';
    } else {
        echo '<div class="three"><p>' . $post . '</p></div>';
    }
    $i++;
}
?>

答案 3 :(得分:0)

我建议您使用一个名称提出所有问题,例如question[] 并且,所有答案都有一个名称,即answer[]

<form method='POST' action='submitPage.php'>
    <textarea name='question[]'></textarea> <br>
    <textarea name='question[]'></textarea> <br>
    <textarea name='question[]'></textarea> <br>
    .
    .

    <textarea name='answer[]'></textarea> <br>
    <textarea name='answer[]'></textarea> <br>
    <textarea name='answer[]'></textarea> <br>
    .
    .
    .
</form>

<强> submitPage.php

<?
$question=$_POST['question'];
$answer=$_POST['answer'];
$TotalQuestion=sizeof($question);

for($i=0;$i<$TotalQuestion;$i++)
{
    $Question=$question[$i];
    $Answer=$answer[$i];
?>

    <div class="Question"><p><?php echo $Question;?></p></div>
    <div class="Answer"><p><?php echo $Answer;?></p></div>

<?}?>

答案 4 :(得分:0)

如果您已经知道问题是什么(并且只有少数问题)并且不需要依赖发布的值,则可以使用数组。

$questions = array('question1', 'question2', 'question3');
$i = 1;
foreach($questions as $question) {
  echo "Question " . htmlspecialchars($i) . ":" . " " . htmlspecialchars($question) . '<br>';
  $i++;
}

或者,如果您将来需要从大量问题中提取,请使用数据库:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, questiontext FROM tbleQuestions"); 
$stmt->execute();

// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
    echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}