简体   繁体   English

显示链接表中的数据,并使用HTML / PHP / MySQL将其显示为列表

[英]Displaying data from a linked table and displaying it as a list with HTML/PHP/MySQL

I have three tables. 我有三张桌子。

students
studentID | FirstName | LastName | Email | Form  

course
CCode | Title  

courseenrolement
courseenrolementid | studentID | ccode | complete | scode

With the website, I have a page where I can view all the current enrolements and I wish to be able to view the list displaying the first name, surname and course title . 在该网站上,我有一个页面,我可以在其中查看所有当前的enrolements并且希望能够看到显示first name, surname and course title的列表。 I know I could do it with the following SQL (for the names): 我知道我可以使用以下SQL(用于名称)做到这一点:

SELECT FirstName, LastName FROM student, courseenrolement 
WHERE courseenrolement.studentID = student.studentID

But I am unsure how to get this to work using HTML/PHP. 但是我不确定如何使用HTML / PHP使它正常工作。 At present I only know how to display the studentID and CCode from the courseenrolement table using the following code: 目前,我只知道如何使用以下代码从courseenrolement表中显示studentID and CCode

<ul>
  <?php foreach ($courseenrolements as $ce): ?>
    <li>
      <form action="" method="post">
        <div>
          <?php htmlout($ce['studentID']); ?>
          <?php htmlout($ce['CCode']); ?>
          <input type="hidden" name="courseenrolementid" value="<?php
              echo $ce['courseenrolementid']; ?>">
          <input type="submit" name="action" value="Edit">
          <input type="submit" name="action" value="Delete">
        </div>
      </form>
    </li>
  <?php endforeach; ?>
</ul>

which displays this: 显示以下内容:

输出

But I would like the names and course title . 但是我想要names and course title I managed to get it to show the names etc in the dropdown on the 'Add new' form, so I would assume it will be similar, but just unsure on how exactly. 我设法让它在“添加新”表格的下拉列表中显示names等,因此我认为它会相似,但是不确定确切性。 Thanks in advance 提前致谢

It appears you're not using a web development framework. 看来您没有使用Web开发框架。 That being the case, check out PHP's PDO library, which lets you make portable database queries in a structured manner. 既然如此,请查看PHP的PDO库,该库使您能够以结构化方式进行可移植数据库查询。

Regardless of whether you check out the PDO library, what you need here is to expand your query with a JOIN , and select the columns you want. 无论是否签出PDO库,这里都需要使用JOIN扩展查询并选择所需的列。 Google for simple examples of JOIN queries, because your use-case seems pretty simple. Google提供了JOIN查询的简单示例,因为您的用例看起来非常简单。 You can then iterate over the result set resource with a for loop, and output data to HTML as necessary. 然后,您可以使用for循环遍历结果集资源,并根据需要将数据输出到HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM