php - 创建电影调查 - PHP 问题

标签 php mysql arrays database

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

这是我到目前为止的 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>

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

This is what I get when I run my code:

This is what I should be getting

最佳答案

数据库设置:

电影表 字段:movie_id、movie_title

电影分级表 字段:movie_ ratings_id、movie_id、user_id、评分

Actor 评级表 字段: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";

但是,那是以后的事了。因此,请继续更新您当前已有的值(value)观。

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>';

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

关于php - 创建电影调查 - PHP 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675714/

相关文章:

PHP 无法从 mysqli 语句读取第二个结果集

ios - 显示数组值的每周本地通知 - Swift

arrays - 组合框和文本框单击事件的 VBA 动态数组

php - 如何在 ->get() 使用 laravel5 中的查询构建器之前进行查询选择

php - php 文件的问题

mysql - 从 Magento 数据库中的序列化字段获取数据

php - 优化随机记录查询

javascript - Jquery:用数组中的值替换字符串

javascript - 如何在Mysql中生成月报表和周报表?

PHP - 从不同的文件格式 Word/Excel/Powerpoint/PDF/RTF 中提取文本