Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:表结构_Php_Mysql_Database_Html Table - Fatal编程技术网

PHP:表结构

PHP:表结构,php,mysql,database,html-table,Php,Mysql,Database,Html Table,我正在开发一个网站,有一些音频课程,每门课程可以有多个课程。我想在自己的表格中显示每门课程的不同课程 这是我的SQL语句: 表格:课程 身份证 表格:课程 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 les

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

这是我的SQL语句:

表格:课程

身份证

表格:课程

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>";
    }
    ?>
mysql\u select\u db($database\u config,$config);
mysql_查询(“设置名称utf8”);
$sql=“选择课程。*,courses.title作为课程从课程内部加入课程上的课程。id=lessons.cid按课程分组。id按课程排序。id”;
$result=mysql\u query($sql)或die(mysql\u error());
while($row=mysql\u fetch\u assoc($result)){
回声“”$row['course']。“

”; echo“

已发布到onder,最新更新时间为“.strftime”(%A%d%B%Y%H:%M)”,strotime($row['date']); } 回声“

”; 回声“; 回声“…

”; 回声“; 回声“; 回声“Nr.”; 呼应“莱索”; 呼应“日期”; 回声“下载”; 回声“; 回声“; “回声”$第['nr'行]。""; “回声”$行['title']。""; “回声”。strftime(“%d/%m/%Y”,strotime($row['date']))。""; 回声“; 回声“; 回声“; 回声“
”; } ?>
关闭代码块第8行的while循环。删除第8行的“}”

而且HTML元素不存在

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

}

}
?>

我想到的一件事是,你开始学习
课程
,然后把
课程
的详细信息带过去。这意味着您将在每节课上有一个新的行,其中包含一个连接的课程。您可能希望按课程进行排序(因此它们是分组的),然后(在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)

请更详细地描述您所遇到的问题代码是否存在行为不符合预期的问题?我们这里并没有真正的开放式代码审查。但我确实有一个建议,那就是将代码与标记分开。使用
echo
输出HTML代码通常是糟糕的形式。为什么要执行
$row=mysql\u fetch\u array($result)
,然后立即执行
$row=mysql\u fetch\u assoc($result)
?非常感谢!这个答案似乎可以。