简体   繁体   中英

How To Clean up PHP Code

So yesterday I was bored and created a flash card web application. I basically used an HTML landing page that has 10 different text areas that push the content entered from the text area into a PHP variable. Each text area had a name "questionone" "questiontwo" and so on. However, is there a cleaner and easier way to do this?

<?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>

You can use arrays in html like this:

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

and process this data in php like this:

$name = $_POST['name'];
foreach( $name as $v ) {
    print $v . '<br/>';
}

While the other answers appear to be correct, most of them are not utilizing htmlspecialchars or issets to prevent warnings. Try something like this:

<?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; ?>
<?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++;
}
?>

I will suggest you to give all questions with one name ie question[] and, all answers with one name ie 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>

<?}?>

If you already know what the questions are (and there's only a few) and don't need to rely on posted values, you could use an array.

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

Or if in future you need to pull from a big set of quesitons, use a DB:

$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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM