首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP:表结构

PHP:表结构
EN

Stack Overflow用户
提问于 2010-12-27 17:38:19
回答 2查看 775关注 0票数 0

我正在开发一个网站,其中有一些音频课程,每门课程可以有多个教训。我想用不同的课程在自己的桌子上展示每一门课程。

这是我的SQL语句:

:课程

id,标题

:经验教训

id,cid (课程id),标题,日期,文件

代码语言:javascript
复制
        $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代码:

代码语言:javascript
复制
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>";
    }
    ?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-28 17:41:38

脑海中浮现的一件事是,您从lessons开始,并将course的详细信息从它中删除。这意味着每节课你将有一个新的行,并加入一个课程。您可能希望按课程进行排序(因此它们是分组的),然后(在PHP中)保存“当前课程”的记录。当课程改变时,切换到新的标题段落、表格等。

伪码:

代码语言:javascript
复制
$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)
票数 0
EN

Stack Overflow用户

发布于 2010-12-28 17:39:36

关闭代码块第8行上的while循环。删除第8行中的“}”。

此外,HTML元素不存在!

我想我知道你有什么问题了。您需要一个while循环,在这个循环中,您执行第二个查询,其中选择course_id等于您正在循环的当前课程id的课程。给你一个小密码。

代码语言:javascript
复制
<?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

}

}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4540565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档