【问题标题】:Creating Movie Survey - PHP questions创建电影调查 - PHP 问题
【发布时间】:2018-09-15 11:22:29
【问题描述】:

最近我们有一个项目,因为我做得不太好。我知道这对我目前的成绩没有帮助,但我认为从长远来看,理解这些概念对我非常有益。任务是创建一个电影调查。

这是我的 formprocessor.php 到目前为止所拥有的内容

<body>
<H1><u>The Vermont Web Designers Movie Survey</u></h1>
<?php
//I want to hold the users information that they entered, so they know that this is their survey
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
print "<p>$firstName $lastName's Movie Review - Spring 2018</p><hr>";

//I have values from 1 to 5 (ratings), I want to create a variable for each of them
$great = 0;
$good = 0;
$ok = 0;
$soso= 0;
$terrible = 0;

//I create an array to hold my selections. One for the movie ratings, another for the actor ratings.
$movieChoice = array($_POST['movie1'], $_POST['movie2'], $_POST['movie3']);
$actorChoice = array($_POST['actors1'], $_POST['actors2'], $_POST['actors3']);


//I use a loop to collect my movie choices.
for($i = 0; $i < 3; $i++){
    if($movieChoice[$i] == 1){
        //I increase the variable by 1 if it was selected.
        $great++;
    }
    else if($movieChoice[$i] == 2){
        $good++;
    } 
    else if($movieChoice[$i] == 3){
        $ok++;
    }
    else if($movieChoice[$i] == 4){
        $soso++;
    }
    else{
        $terrible++;
    }
}

//I use another loop to hold my actor selections.
for($i = 0; $i < 3; $i++){
    if($actorChoice[$i] == 1){
        $great++;
    }
    else if($actorChoice[$i] == 2){
        $good++;
    } 
    else if($actorChoice[$i] == 3){
        $ok++;
    }
    else if($actorChoice[$i] == 4){
        $soso++;
    }
    else{
        $terrible++;
    }
}

 print ('<table align= "center">
    <tr><th colspan= "3">Movie</th>
    <th>Question</th>
    <th>Great</th>
    <th>Good</th>
    <th>Ok</th>
    <th>So-So</th>
    <th>Terrible</th>
    </tr>

    <tr><td colspan= "3">1: The Godfather Part 1</td>
    <td>Quality of Movie</td>
    <td>'.$movieChoice[$great].'</td>
    <td>'.$movieChoice[$good].'</td>
    <td>'.$movieChoice[$ok].'&nbsp;</td>
    <td>'.$movieChoice[$soso].'</td>
    <td>'.$movieChoice[$terrible].'</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>)
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td colspan= "3">2: Men of Honor</td>
    <td>Quality of Movie</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td colspan= "3">3: Three Days of the Condor</td>
    <td>Quality of Movie</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
</table>');

?>

这是我的表格

<H1><u>The Vermont Web Designers Movie Survey</u></h1>
First Name: <input type= "text" size= "10" maxlength= "25" name= "firstName">
Last Name: <input type= "text" size= "10" maxlength= "25" name= "lastName"><br />
<hr>
<p>Please use a scale of 1 (first-rate; great; awesome) through 5 (really terrible) to answer each of these questions</p><br />
<hr>

<table align= "center"> 
    <tr><th colspan= "3">Movie </th>
    <th>Question</th>
    <th>Great</th>
    <th>Good</th>
    <th>Ok</th>
    <th>So-So</th>
    <th>Terrible</th>
    </tr>

    <tr><td colspan= "3">1: The Godfather Part 1</td>
    <td>Quality of Movie</td>
    <td><input type= "radio" name= "movie1" value="1">1</td>
    <td><input type= "radio" name= "movie1" value="2">2</td>
    <td><input type= "radio" name= "movie1" value="3">3</td>
    <td><input type= "radio" name= "movie1" value="4">4</td>
    <td><input type= "radio" name= "movie1" value="5">5</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>
    <td><input type= "radio" name= "actors1" value="1">1</td>
    <td><input type= "radio" name= "actors1" value="2">2</td>
    <td><input type= "radio" name= "actors1" value="3">3</td>
    <td><input type= "radio" name= "actors1" value="4">4</td>
    <td><input type= "radio" name= "actors1" value="5">5</td>
    </tr>

    <tr><td colspan= "3">2: Men of Honor</td>
    <td>Quality of Movie</td>
    <td><input type= "radio" name= "movie2" value="1">1</td>
    <td><input type= "radio" name= "movie2" value="2">2</td>
    <td><input type= "radio" name= "movie2" value="3">3</td>
    <td><input type= "radio" name= "movie2" value="4">4</td>
    <td><input type= "radio" name= "movie2" value="5">5</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>
    <td><input type= "radio" name= "actors2" value="1">1</td>
    <td><input type= "radio" name= "actors2" value="2">2</td>
    <td><input type= "radio" name= "actors2" value="3">3</td>
    <td><input type= "radio" name= "actors2" value="4">4</td>
    <td><input type= "radio" name= "actors2" value="5">5</td>
    </tr>

    <tr><td colspan= "3">3: Three Days of the Condor</td>
    <td>Quality of Movie</td>
    <td><input type= "radio" name= "movie3" value="1">1</td>
    <td><input type= "radio" name= "movie3" value="2">2</td>
    <td><input type= "radio" name= "movie3" value="3">3</td>
    <td><input type= "radio" name= "movie3" value="4">4</td>
    <td><input type= "radio" name= "movie3" value="5">5</td>
    </tr>

    <tr><td  colspan= "3">&nbsp;</td>
    <td>Quality of Actors</td>
    <td><input type= "radio" name= "actors3" value="1">1</td>
    <td><input type= "radio" name= "actors3" value="2">2</td>
    <td><input type= "radio" name= "actors3" value="3">3</td>
    <td><input type= "radio" name= "actors3" value="4">4</td>
    <td><input type= "radio" name= "actors3" value="5">5</td>
    </tr>
</table>

    <hr>
    <input type= "submit" value= "submit">
</form>

我遇到的真正问题是填充我的电影选择和演员选择数组,然后为用户打印这些数据。

This is what I get when I run my code:

This is what I should be getting

【问题讨论】:

  • 你的问题太宽泛了。首先缩小问题范围并一一解决。如果您无法找出其中一个,请回来,删除所有对手头问题不必要的代码,向我们展示预期结果以及您当前得到的结果。
  • 我试图将我的代码范围缩小到更确切的问题。我添加了两张图片:一张是我得到的,另一张是我应该得到的。
  • 很难回答这个@Melody。你有更多可以简写的代码吗?你有一个数据库调用来获取你已经收到的结果吗?
  • 我还没有为这个项目设置数据库,只是因为我想先下载 PHP。我将不得不创建一个数据库,我假设为每个用户表单保存选择。老师一直特别不擅长解释这个与php MySQL的联系。我想我可以制作一个数组来保存他们给出的答案,然后打印出来。然后我会将答案存储在我的数据库中。
  • 好的...让我快速为您写一些东西,这将帮助您完成这部分工作。拥有更多数据会有所帮助。 :)

标签: php mysql arrays database


【解决方案1】:

数据库设置:

电影表 字段:movie_id、movie_title

movie_ratings 表 字段:movie_ratings_id、movie_id、user_id、评分

actor_ratings 表 字段:actor_ratings_id、movie_id、user_id、评分

这样,您可以单独存储每个单独的评分,并在您的 sql 语句上执行 COUNT 和 GROUP BY。这可能会有点紧张,因为您正在进行基本的数据库操作。您始终可以执行 SELECT *,然后根据以下内容提高您的电影评分。

不过,这不在回答问题的范围内,如果您想给我发消息/聊天,我可以帮助您。

原始答案如下:

首先从数据库中获取结果

$sql = "SELECT * FROM ratings";
// fetch results

现在你已经有了结果,你可以正确地构建它 我会做这样的事情。我选择这样做的原因是因为你有一个带有命名键的关联数组,你可以稍后在 foreach 循环或类似的东西中调用它们。

$movieChoices = array(
    array('title'=>"The Godfather Part 1",
        'movieRatings' => array(
            "great" => 0,
            "good" => 0,
            "ok" => 0,
            "soso"=> 0,
            "terrible" => 0,
        ),
        'actorRatings' => array(
            "great" => 0,
            "good" => 0,
            "ok" => 0,
            "soso"=> 0,
            "terrible" => 0,
        )
    ),
    array('title'=> "Men Of Honor",
        'movieRatings' => array(
            "great" => 0,
            "good" => 0,
            "ok" => 0,
            "soso"=> 0,
            "terrible" => 0,
        ),
        'actorRatings' => array(
            "great" => 0,
            "good" => 0,
            "ok" => 0,
            "soso"=> 0,
            "terrible" => 0,
        )
    ),
    array('title'=>"Three Days of Condor",
        'movieRatings' => array(
            "great" => 0,
            "good" => 0,
            "ok" => 0,
            "soso"=> 0,
            "terrible" => 0,
        ),
        'actorRatings' => array(
            "great" => 0,
            "good" => 0,
            "ok" => 0,
            "soso"=> 0,
            "terrible" => 0,
        )
    )
);

这会给你这样的东西:

Array
(
    [0] => Array
        (
            [title] => The Godfather Part 1
            [movieRatings] => Array
                (
                    [great] => 0
                    [good] => 0
                    [ok] => 0
                    [soso] => 0
                    [terrible] => 0
                )

            [actorRatings] => Array
                (
                    [great] => 0
                    [good] => 0
                    [ok] => 0
                    [soso] => 0
                    [terrible] => 0
                )

        )

    [1] => Array
        (
            [title] => Men Of Honor
            [movieRatings] => Array
                (
                    [great] => 0
                    [good] => 0
                    [ok] => 0
                    [soso] => 0
                    [terrible] => 0
                )

            [actorRatings] => Array
                (
                    [great] => 0
                    [good] => 0
                    [ok] => 0
                    [soso] => 0
                    [terrible] => 0
                )

        )

    [2] => Array
        (
            [title] => Three Days of Condor
            [movieRatings] => Array
                (
                    [great] => 0
                    [good] => 0
                    [ok] => 0
                    [soso] => 0
                    [terrible] => 0
                )

            [actorRatings] => Array
                (
                    [great] => 0
                    [good] => 0
                    [ok] => 0
                    [soso] => 0
                    [terrible] => 0
                )

        )

)

以相同的方式存储您的帖子变量以方便代码使用。

$movieChoice = array($_POST['movie1'], $_POST['movie2'], $_POST['movie3']);
$actorChoice = array($_POST['actors1'], $_POST['actors2'], $_POST['actors3']);

使用循环来存储这两个项目。 循环遍历同一件事两次是没有意义的。重新使用代码。 我使用 += 1,因为你对变量所做的事情更明显。 和 ++ 一样,只是我的喜好。

此外,当您在这里时,您可以编写一条 SQL 语句来更新数据库中的行。

$sql = "UPDATE movie_ratings SET RATING = RATING + 1 WHERE movie_id = MOVIE_ID";

但是,那是以后的事了。所以继续更新你已经拥有的当前值。

for ($i = 0; $i < 3; $i++) {
    if ($movieChoice[$i] == 1) {
        //I increase the variable by 1 if it was selected.
        $movieChoices[$i]['movieRatings']["great"] += 1;
    } else if ($movieChoice[$i] == 2) {
        $movieChoices[$i]['movieRatings']['good'] += 1;
    } else if ($movieChoice[$i] == 3) {
        $movieChoices[$i]['movieRatings']['ok'] += 1;
    } else if ($movieChoice[$i] == 4) {
        $movieChoices[$i]['movieRatings']['soso'] += 1;
    } else {
        $movieChoices[$i]['movieRatings']['terrible'] += 1;
    }

    if ($actorChoice[$i] == 1) {
        //I increase the variable by 1 if it was selected.
        $movieChoices[$i]['actorRatings']["great"] += 1;
    } else if ($actorChoice[$i] == 2) {
        $movieChoices[$i]['actorRatings']['good'] += 1;
    } else if ($actorChoice[$i] == 3) {
        $movieChoices[$i]['actorRatings']['ok'] += 1;
    } else if ($actorChoice[$i] == 4) {
        $movieChoices[$i]['actorRatings']['soso'] += 1;
    } else {
        $movieChoices[$i]['actorRatings']['terrible'] += 1;
    }
}

现在,当您想要打印结果时,这样做会更容易。您可以使用foreach() 循环遍历每个电影结果,并打印出它们各自的值。比你做的要容易得多,代码更少。

print '
<table align="center">
    <tr>
        <th colspan="3">Movie</th>
        <th>Question</th>
        <th>Great</th>
        <th>Good</th>
        <th>Ok</th>
        <th>So-So</th>
        <th>Terrible</th>
    </tr>';

foreach($movieChoices as $choice) {
    print '
    <tr>
        <td colspan="3">'.$choice['title'].'</td>
        <td>Quality of Movie</td>
        <td>'.$choice['movieRatings']['great'].'</td>
        <td>'.$choice['movieRatings']['good'].'</td>
        <td>'.$choice['movieRatings']['ok'].'&nbsp;</td>
        <td>'.$choice['movieRatings']['soso'].'</td>
        <td>'.$choice['movieRatings']['terrible'].'</td>
    </tr>

    <tr>
        <td colspan="3">&nbsp;</td>
        <td>Quality of Actors</td>
        <td>'.$choice['actorRatings']['great'].'</td>
        <td>'.$choice['actorRatings']['good'].'</td>
        <td>'.$choice['actorRatings']['ok'].'&nbsp;</td>
        <td>'.$choice['actorRatings']['soso'].'</td>
        <td>'.$choice['actorRatings']['terrible'].'</td>
    </tr>
    ';
}
print '</table>';

老实说,对于这段代码来说,这是一个非常长的结果。如果我有更多的工作要做,我可以把它分解成更简单的给你。但是,这将有助于您尝试做的事情。

【讨论】:

  • 该项目是为了添加基本的数据库操作。他希望: * 当我们为不同的人汇总表格时,整体评分表中的计数会增加 * 生成电影评分表(我主要从事的工作) * 报告底部添加了一个清除按钮,即单击时,清除整个数据库。 * 单击清除按钮时会出现一个屏幕,要求您确认是否要清除整个数据库,或取消您的请求。 *在底部(表格后)生成一个包含用户列表及其个人评分的表格
  • 明白了,希望我的设置能帮助你。如果您需要更多帮助,请告诉我。 @旋律
  • 谢谢,我会的。我添加了我之前取出的表格,以及我输入名字和姓氏的最高代码。您将如何为这个特定项目设置数据库?那将是多少个字段? ID、Fname、Lname、电影 1、电影 2、电影 3?
  • @Melody - 我会在数据库的帮助下更新我的答案
  • 你帮了大忙!您提到我们可以聊天/发消息,我将非常感激! ...我只是不知道如何在这里留言,哈哈哈。
【解决方案2】:

我有一些空闲时间,决定尝试一下你的想法。您应该能够将其复制/粘贴到单个文件中,并且它应该可以工作。

我并不打算将我的答案作为最终解决方案,但我希望它能给您一些关于构建程序的新想法。随意挑选你会从中选择什么。

这里的主要思想是将用户的选择存储到会话中,以便我们可以增加它并在最后一个表中显示运行总计。我试着记录代码,但请随时提出问题。

我还尝试让它相对动态,以便您可以添加电影、问题、选择,并且只要您保持数据结构相同,表单和表单处理器应该可以处理它。存储电影、选择和问题的数组也使用关联键(字符串,例如“1”、“2”等),这稍微模仿了关系数据库的使用方式。向这些数组添加/删除元素以查看表单如何更改。

最后,有一些语言结构(??、方形数组 [])需要 PHP 7 才能运行。还有一些代码行......丑陋。我没有试图找到更好的方法来编码它们。 前任。 list($movieId, $questionId) = array_pad(explode('_', $key, 2), 2, null);。

如果您遇到问题,请告诉我。

<?php
session_start();

// The following 3 arrays just store movie, choices, and questions
// which we will use in form processing and also to display the form to the user.
// Could come from DB, file, or hard-coded like this.
//
// Play around with these 3 arrays to see how the application changes.

// A list of movies.
$movies = [
    [
        'id' => '1',
        'title' => 'Taxi Driver',
        'director' => 'Mr. Scor-say-zee',
    ],
    [
        'id' => '2',
        'title' => 'The Mask',
        'director' => 'I do not know',
    ],
];

// A list of choices that a user can select.
$choices = [
    '1' => 'Terrible',
    '2' => 'So-So',
    '3' => 'OK',
    '4' => 'Good',
    '5' => 'Great',
];

// A list if rating categories.
$questions = [
    '1' => 'Quality of Movie',
    '2' => 'Quality of Actors',
    '3' => 'Quality of Producers',
];


// The reset button was clicked. Clear the session and redirect back to self.
if (isset($_POST['reset'])) {
    session_destroy();
    header('Location: ' . $_SERVER['PHP_SELF']);
    die;
}

// The form was submitted. Store the user's selection in to the session.
// Storing it in the session for simplicity but could store it to a file or DB or some other persistent storage.
if (isset($_POST['submit'])) {

    // Loop over the POST data and extract the question ID and movie ID for each submitted answer.
    foreach ($_POST as $key => $value) {

        // The incoming POST data looks like: 1_1, 2_1, 2_2 etc.
        // Split the string on the underscore which will give us the movie ID and question ID respectively.
        // Once we know which movie and question is submitted store them, along with the actual rating, in the session.
        // If it doesn't exist in the session, set it to 1.
        // If it does exist in the session, just increment the value.
        list($movieId, $questionId) = array_pad(explode('_', $key, 2), 2, null);

        if (is_numeric($questionId) && is_numeric($movieId)) {

            // Check if it is in the session already.
            if (!isset($_SESSION['ratings'][$movieId][$questionId][$value])) {
                $_SESSION['ratings'][$movieId][$questionId][$value] = 1;
            } else {
                $_SESSION['ratings'][$movieId][$questionId][$value] = $_SESSION['ratings'][$movieId][$questionId][$value] + 1;
            }
        }
    }
}

?>

<!-- Display the form -->
<form method="post">
    <?php
    foreach ($movies as $movie) {

        echo '<h2>' . $movie['title'] . '</h2>';

        foreach ($questions as $questionId => $questionDescription) {
            echo '<h3>' . $questionDescription . '</h3>';
            foreach ($choices as $choiceValue => $choiceDescription) {
                // Each row will have a name like: 1_1, 1_2, 2_1 etc.
                // These represent the movie ID and question ID respectively.
                // During form processing (above) we split the name on the underscore which give use the movie ID and question ID that the user submitted.
                // There is a probably a better way to do this.
                echo '<label>';
                echo '<input type= "radio" name= "' . $movie['id'] . '_' . $questionId . '" value="' . $choiceValue . '">';
                echo $choiceDescription;
                echo '</label>';
            }
        }

        echo '<hr />';
    }
    ?>

    <br>
    <br>

    <input type="submit" name="submit" value="Submit">
    <input type="submit" name="reset" value="Reset">

</form>

<!-- Display the results from the session -->
<table border="1">
    <caption>Favorite and Least Favorite Things</caption>
    <tr>
        <th>Movie</th>
        <th>Question</th>
        <?php
        echo "<th>" . implode('</th> <th>', $choices) . "</th>";
        ?>
    </tr>

    <?php
    foreach ($movies as $movie) {
        echo '<tr>';

        // Notice the rowspan grows with each question added.
        echo '<th rowspan="' . count($questions) . '">' . $movie['title'] . '</th>';

        foreach ($questions as $questionId => $questionDescription) {

            // Open a new row on the 2nd and subsequent iterations.
            if ((int)$questionId > 1) {
                echo '<tr>';
            }

            echo '<th>' . $questionDescription . '</th>';

            // Output the actual ratings from the session.
            foreach ($choices as $choiceId => $choiceDescription) {
                echo '<th>';
                echo $_SESSION['ratings'][$movie['id']][$questionId][$choiceId] ?? 0;
                echo '</th>';
            }
        }
        echo '</tr>';
    }
    ?>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2013-10-28
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多