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

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

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

| id_class| name  |
-------------------
|    1    |   A   |
|    2    |   B   |
|    3    |   C   |

表\u学生

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

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

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

<!--page-->
<table border="1">
	<thead>
		<tr>
		<th>Class</th>
		<th>Student</th>
		</tr>
	</thead>
	<tbody>
	<?php
	for ($x=0; $x <= (getCountClass()-1) ; $x++) {
	?>
		<tr>
		<td>
			<input type="text" class="form-control" id="bobot" name="" value="<?php echo getClassName($x); ?>" readonly>
		</td>
		<td>				
			<select class="form-control">
				<option><?php echo getStudentName(getClassID($x));?></option>
			</select>
		</td>			
		</tr>
	<?php
	}
	?>
	</tbody>
</table>

<!--function-->
<?php

function getClassID($no_urut) {
	include('config.php');
	$query  = "SELECT id_class FROM class ORDER BY id_class";
	$result = mysqli_query($connect, $query);

	while ($row = mysqli_fetch_array($result)) {
		$listID[] = $row['id_class'];
	}

	return $listID[($no_urut)];
}

function getClassName($no_urut) {
	include('config.php');
	$query  = "SELECT name_class FROM class ORDER BY id_class";
	$result = mysqli_query($connect, $query);

	while ($row = mysqli_fetch_array($result)) {
		$name[] = $row['name_class'];
	}

	return $name[($no_urut)];
}

function getStudentName($id_class) {
	include('config.php');
	$query  = "SELECT name_student FROM student WHERE id_class=$id_class";
	$result = mysqli_query($connect, $query);
	while ($row = mysqli_fetch_array($result)) {
		$name = $row['name_student'];
	}

	return $name;
}

function getCountClass() {
	include('config.php');
	$query  = "SELECT count(*) FROM class";
	$result = mysqli_query($connect, $query);
	while ($row = mysqli_fetch_array($result)) {
		$count = $row[0];
	}

	return $count;
}

?>

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

暂无答案!

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

相关问题