Недавно у нас был проект из-за того, что я не очень хорошо работал. Я знаю, что это не поможет моему текущему классу, но я думаю, что понимание концепций будет очень полезно для меня в долгосрочной перспективе. Задача состояла в том, чтобы создать обзор фильмов.
Это то, что у меня есть до сих пор для моего 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 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].' </td>
<td>'.$movieChoice[$soso].'</td>
<td>'.$movieChoice[$terrible].'</td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>)
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3">2: Men of Honor</td>
<td>Quality of Movie</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3">3: Three Days of the Condor</td>
<td>Quality of Movie</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr><td colspan= "3"> </td>
<td>Quality of Actors</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </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"> </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"> </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"> </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>
Реальная проблема, с которой у меня возникают проблемы, - это заполнить мой выбор фильмов и выбрать актер, а затем распечатать эти данные для пользователя.
У меня было свободное время и я решил поиграть с твоей идеей. Вы должны иметь возможность скопировать/вставить это в один файл, и он должен работать.
Я не хочу, чтобы мой ответ был окончательным решением, но я надеюсь, что это даст вам новые идеи о структурировании вашей программы. Не стесняйтесь выбирать и выбирать из этого.
Основная идея здесь заключается в том, что она хранит пользовательский выбор в сеансе, чтобы мы могли увеличивать его и отображать текущее итоговое значение в последней таблице. Я пытался документировать код, когда я шел, но pls не стесняются задавать вопросы.
Я также попытался сделать его относительно динамичным, чтобы вы могли добавлять фильмы, вопросы, варианты, а обработчик формы и формы должен обрабатывать его в порядке, пока вы сохраняете структуры данных одинаковыми. В массивах, в которых хранятся фильмы, варианты и вопросы, также используются ассоциативные ключи (строки, такие как "1", "2" и т.д.), Которые слегка имитируют то, что будет использовать реляционная БД. Добавьте/удалите элементы в эти массивы, чтобы увидеть, как изменяется форма.
Наконец, существуют некоторые языковые конструкции (??, квадратные массивы []), которые требуют запуска PHP 7. Есть также некоторые строки кода, которые... уродливые. Я не пытался найти более удобный способ их кодирования. Ex. 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 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>
Настройка базы данных:
таблица фильмов. Поля: movie_id, movie_title
таблица movie_ratings Поля: movie_ratings_id, movie_id, user_id, рейтинг
actor_ratings table Поля: actor_ratings_id, movie_id, user_id, рейтинг
Таким образом, вы можете сохранить каждый индивидуальный рейтинг по отдельности и выполнить COUNT и GROUP BY в своем заявлении sql. Это может стать немного интенсивным, так как вы делаете базовые манипуляции с БД. Вы всегда можете сделать 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'].' </td>
<td>'.$choice['movieRatings']['soso'].'</td>
<td>'.$choice['movieRatings']['terrible'].'</td>
</tr>
<tr>
<td colspan="3"> </td>
<td>Quality of Actors</td>
<td>'.$choice['actorRatings']['great'].'</td>
<td>'.$choice['actorRatings']['good'].'</td>
<td>'.$choice['actorRatings']['ok'].' </td>
<td>'.$choice['actorRatings']['soso'].'</td>
<td>'.$choice['actorRatings']['terrible'].'</td>
</tr>
';
}
print '</table>';
Честно говоря, это ДЕЙСТВИТЕЛЬНО длинный результат для этого фрагмента кода. Если бы у меня было больше работы, я мог бы разбить ее намного проще для вас. Однако это поможет с тем, что вы пытаетесь сделать.