当我学习PHP时,我正在为电视节目制作个人剧集跟踪器。 我现在的问题是,我不知道如何打印出一个漂亮的列表"按季节分开。现在,系列的每一集都会打印出来自mysql查询的循环。还有一个观看""在这里我需要用来检查用户是否在循环中标记了剧集。
我怎样才能一次循环一个季节而不是每一集?我在另一个"系列"中获得了季节和int的数量。表,如果这使它成为任何easyer。
// Get episodes from database
$list_sql = "SELECT * FROM episodes WHERE series_id = '".$_GET['id']."' ORDER BY `episodes`.`season` ASC, `episodes`.`season_episode` ASC LIMIT 2000";
$list_query = mysqli_query($con, $list_sql) or die(mysqli_errno($con));
while ($row = mysqli_fetch_assoc($list_query)) {
// GET INFO ON WHEN EPISODE WAS VIEWED
$when_watched_sql = "SELECT date_watched FROM watched WHERE episode_id = '".$row['id']."'";
$when_watched_query = mysqli_query($con, $when_watched_sql);
$when_watched_result = mysqli_fetch_assoc($when_watched_query);
// IS EPISODE WATCHED? If yes, make CHECKBOX CHECKED
$watched_sql = "SELECT watched FROM watched WHERE episode_id = '".$row['id']."' AND watched = '1'";
$watched = mysqli_query($con, $watched_sql);
if(mysqli_num_rows($watched)>0) {
// One row returned, episode is watched
$watched_box = "checked";
} else {
// No rows returned, episode is not watched
$watched_box = "";
}
print("Watched: <input type='checkbox' name='watched[]' value='".$row['id']."' $watched_box /> Season: ".$row['season']." Episode: ".$row['season_episode']." ".$row['name']." <br>");
}
我可能在这里做了很多错误的做事方式,我确实喜欢一些建设性的提示和技巧,以便以适当的方式学习如何做到这一点。
答案 0 :(得分:0)
OFFTOPIC:
首先,在您的代码中,您会检查该集是否被观看,之后您会检查它是否已被观看(根据您的评论)。哪个对我没有意义?
所以我建议将2个查询组合在一起使用:
$watched_sql = "SELECT watched,date_watched FROM watched WHERE episode_id = '".$row['id']."' AND watched = '1'";
如果出于某种原因这种情况不适用于您的情况,我仍然建议您检查一下您确定观看之后所看到的日期:
if(mysqli_num_rows($watched)>0) {
$when_watched_sql = "SELECT date_watched FROM watched WHERE episode_id = '".$row['id']."'";
$when_watched_query = mysqli_query($con, $when_watched_sql);
$when_watched_result = mysqli_fetch_assoc($when_watched_query);
// One row returned, episode is watched
$watched_box = "checked";
} else {
// No rows returned, episode is not watched
$watched_box = "";
}
ON TOPIC:
我会说你需要把$ watched_box作为一个数组。我似乎完全不理解你的mysql表结构,所以我有点偏。我现在无法测试这些东西,但我说你需要这样做:
$i=0;
$watched_box=array();
while ($row = mysqli_fetch_assoc($list_query)) {
// GET INFO ON WHEN EPISODE WAS VIEWED
$when_watched_sql = "SELECT date_watched FROM watched WHERE episode_id = '".$row['id']."'";
$when_watched_query = mysqli_query($con, $when_watched_sql);
$when_watched_result = mysqli_fetch_assoc($when_watched_query);
// IS EPISODE WATCHED? If yes, make CHECKBOX CHECKED
$watched_sql = "SELECT watched FROM watched WHERE episode_id = '".$row['id']."' AND watched = '1'";
$watched = mysqli_query($con, $watched_sql);
if(mysqli_num_rows($watched)>0) {
// One row returned, episode is watched
$watched_box[$i] = "checked";
} else {
// No rows returned, episode is not watched
$watched_box[$i] = "";
}
$i++;
var_dump($i);
我99%确定这个代码不会立即为您工作,因为我没有完全获得您的数据库结构,我无法测试它。您需要对此进行一些更改,这是我想要的方式。使用计数器和数组。在while循环后尝试var_dump($watched_box);
并查看它将显示的内容
答案 1 :(得分:0)
在@Loko的帮助下,我想到了这一点。 我最初的问题是如何在每个季节打印一个列表,包括该季节的所有剧集,而不是一个大型剧集。
我对此的解决方案是FOR循环,根据MySQL数据库中系列信息中设置的季节数,循环显示X个季节。
// Test new print loop (Thanks Loko)
?><form method="post" action="episode_list.php?id=<?php echo $series_id;?>"><?php
// Make a loop that iterates X amount of SEASONS based on mysql count result
$season = $season_count_array['seasons'];
// LOOP, start with season 1, if below $season (Amount of seasons), continue and add 1 to variable until loop reaches amount of seasons
for($seasons = 1; $seasons <= $season; $seasons++) {
$season_episodes_sql = "SELECT * FROM episodes WHERE season = '".$seasons."' AND series_id = '".$_GET['id']."' ORDER BY season_episode";
$season_episodes_query = mysqli_query($con, $season_episodes_sql);
// Loop through episodes of chosen season
while ($row = mysqli_fetch_assoc($season_episodes_query)) {
// Is episode watched?
$watched_sql = "SELECT watched FROM watched WHERE episode_id = '".$row['id']."' AND watched = '1'";
$watched = mysqli_query($con, $watched_sql);
if(mysqli_num_rows($watched)>0) {
// One row returned, episode is watched
$watched_box = "checked";
} else {
// No rows returned, episode is not watched
$watched_box = "";
}
// Allright, let's print
print("Sett: <input type='checkbox' name='watched[]' value='".$row['id']."' $watched_box /> Sesong: ".$row['season']." Episode: ".$row['season_episode']." ".$row['name']." <br>");
}
// Here I can add what I want as a spacer between printed seasons, just a break, for now.
echo '<br>';
}