【问题标题】:PHP: table structurePHP:表结构
【发布时间】:2017-06-18 17:06:21
【问题描述】:

我正在开发一个包含一些音频课程的网站,每门课程可以有多个课程。我想在自己的表格中显示每门课程以及不同的课程。

这是我的 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 代码吗?

这是我写的代码:

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>";
    }
    ?>

【问题讨论】:

  • 请详细描述您遇到的问题
  • 代码是否存在不符合预期的问题?我们在这里并没有真正进行开放式代码审查。但我确实有一个建议是将代码与标记分开。使用echo 输出HTML 代码的格式一般很差。
  • 你为什么在做$row = mysql_fetch_array($result),紧接着是$row = mysql_fetch_assoc($result)的循环?

标签: php mysql database html-table


【解决方案1】:

想到的一件事是您从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)

【讨论】:

  • 非常感谢!这个答案似乎可以做到。
【解决方案2】:

您在代码块的第 8 行关闭了 while 循环。删除第 8 行的“}”。

HTML 元素也不存在!

我想我知道你的问题是什么。您需要一个循环来循环所有“课程”,并在该循环中执行第二个查询,在其中选择课程 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

}

}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多