我正在开发一个网站,其中有一些音频课程,每门课程可以有多个教训。我想用不同的课程在自己的桌子上展示每一门课程。
这是我的SQL语句:
表:课程
id,标题
表:经验教训
id,cid (课程id),标题,日期,文件
$sql = "SELECT lessons.*, courses.title AS course FROM lessons INNER JOIN courses ON courses.id = lessons.cid GROUP BY lessons.id ORDER BY lessons.id" ;有人能帮我处理PHP代码吗?
这是我编写的I代码:
mysql_select_db($database_config, $config);
mysql_query("set names utf8");
$sql = "SELECT lessons.*, courses.title AS course FROM lessons INNER JOIN courses ON courses.id = lessons.cid GROUP BY lessons.id ORDER BY lessons.id" ;
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "<p><span class='heading1'>" . $row['course'] . "</span> </p> ";
echo "<p class='datum'>Posted onder <a href='*'>*</a>, latest update on " . strftime("%A %d %B %Y %H:%M", strtotime($row['date']));
}
echo "</p>";
echo "<class id='text'>";
echo "<p>...</p>";
echo "<table border: none cellpadding='1' cellspacing='1'>";
echo "<tr>";
echo "<th>Nr.</th>";
echo "<th width='450'>Lesso</th>";
echo "<th>Date</th>";
echo "<th>Download</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['nr'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . strftime("%d/%m/%Y", strtotime($row['date'])) . "</td>";
echo "<td><a href='audio/" . rawurlencode($row['file']) . "'>MP3</a></td>";
echo "</tr>";
echo "</table>";
echo "<br>";
}
?>发布于 2010-12-28 17:41:38
脑海中浮现的一件事是,您从lessons开始,并将course的详细信息从它中删除。这意味着每节课你将有一个新的行,并加入一个课程。您可能希望按课程进行排序(因此它们是分组的),然后(在PHP中)保存“当前课程”的记录。当课程改变时,切换到新的标题段落、表格等。
伪码:
$currentCourse = null; // intitialize the course
$query = your select sorted by course;
while ($row in $query)
{
if ($currentCourse != $row['course'])
{
if (!is_null($currentCourse))
{
// there was a course before it, close the current one
}
// begin setting up heading1, table beginning, etc.
$currentCourse = $row['course']; // set this as the active course
}
// dump the current row as a table entry
}
// close the table (same code as in the second if statement)发布于 2010-12-28 17:39:36
关闭代码块第8行上的while循环。删除第8行中的“}”。
此外,HTML元素不存在!
我想我知道你有什么问题了。您需要一个while循环,在这个循环中,您执行第二个查询,其中选择course_id等于您正在循环的当前课程id的课程。给你一个小密码。
<?php
while($row = mysql_fetch_assoc(mysql_query("SELECT * FROM courses"))) {
//display the course
while($row2 = mysql_fetch_assoc(mysql_query("SELECT * FROM lessons WHERE course_id=" . $row['id']))) {
//display the lessons of that course
}
}
?>https://stackoverflow.com/questions/4540565
复制相似问题