mysql制作带有组合框的动态表单表

watbbzwu  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(335)

我想尝试用combobox创建动态表单表。但组合框不能显示指定id的所有记录。
我的数据库包含2个表:
表\u类

  1. | id_class| name |
  2. -------------------
  3. | 1 | A |
  4. | 2 | B |
  5. | 3 | C |

表\u学生

  1. | id_student | name | id_class |
  2. --------------------------------
  3. | 1 | Sarah | 1 |
  4. | 2 | Joe | 1 |
  5. | 3 | Yuri | 2 |
  6. | 4 | Andy | 2 |
  7. | 5 | Sam | 3 |
  8. | 6 | Oliv | 3 |
  • id\类是外键

我想要的结果是一个表单界面,如图所示:

但我的代码结果不是我想要的,combobox不能显示指定id的所有记录。

  1. <!--page-->
  2. <table border="1">
  3. <thead>
  4. <tr>
  5. <th>Class</th>
  6. <th>Student</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <?php
  11. for ($x=0; $x <= (getCountClass()-1) ; $x++) {
  12. ?>
  13. <tr>
  14. <td>
  15. <input type="text" class="form-control" id="bobot" name="" value="<?php echo getClassName($x); ?>" readonly>
  16. </td>
  17. <td>
  18. <select class="form-control">
  19. <option><?php echo getStudentName(getClassID($x));?></option>
  20. </select>
  21. </td>
  22. </tr>
  23. <?php
  24. }
  25. ?>
  26. </tbody>
  27. </table>
  28. <!--function-->
  29. <?php
  30. function getClassID($no_urut) {
  31. include('config.php');
  32. $query = "SELECT id_class FROM class ORDER BY id_class";
  33. $result = mysqli_query($connect, $query);
  34. while ($row = mysqli_fetch_array($result)) {
  35. $listID[] = $row['id_class'];
  36. }
  37. return $listID[($no_urut)];
  38. }
  39. function getClassName($no_urut) {
  40. include('config.php');
  41. $query = "SELECT name_class FROM class ORDER BY id_class";
  42. $result = mysqli_query($connect, $query);
  43. while ($row = mysqli_fetch_array($result)) {
  44. $name[] = $row['name_class'];
  45. }
  46. return $name[($no_urut)];
  47. }
  48. function getStudentName($id_class) {
  49. include('config.php');
  50. $query = "SELECT name_student FROM student WHERE id_class=$id_class";
  51. $result = mysqli_query($connect, $query);
  52. while ($row = mysqli_fetch_array($result)) {
  53. $name = $row['name_student'];
  54. }
  55. return $name;
  56. }
  57. function getCountClass() {
  58. include('config.php');
  59. $query = "SELECT count(*) FROM class";
  60. $result = mysqli_query($connect, $query);
  61. while ($row = mysqli_fetch_array($result)) {
  62. $count = $row[0];
  63. }
  64. return $count;
  65. }
  66. ?>

对不起,如果我的语言太差,请帮我解决这个问题

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题