我想为每个回显的表数据列显示单独的列标题?
<form>
<?php
$numeroOption= $_POST['numero'];
$roomtype= $_POST['roomtype'];
$selectOption = $_POST['parkname'];
$query = "SELECT * FROM `ROOMS` WHERE `Capacity` < '$numeroOption' AND `Park` LIKE '$selectOption%' AND `dataProjector` LIKE '$proj_check%' AND `Whiteboard` LIKE '$white_check%' AND `OHP` LIKE '$ohp_check%' AND `WheelchairAccess` LIKE '$wheel_check%' AND `lectureCapture` LIKE '$cap_check%' AND `Style` LIKE '$roomtype%'";
$result = mysql_query($query);
if ($result == FALSE) die ("could not execute statement $query<br />");
echo "<form action='' method='post'>";
echo "<table>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['roomCode'] . "</td>";
echo "<td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td>";
echo "<td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td>";
echo "<td>" . $row['wheelchairAccess'] . "</td>";
echo "<td>" . $row['lectureCapture'] . "</td>";
echo "<td><input type='radio' name='radioSelect' value= '". $row['roomCode']."'></td>";
}
echo "<input type='submit' name='ttroom' id='ttroom' name='ttroom'>";
echo "</tr>";
echo "</table>";
echo "</form>";
我不确定我是否理解您的问题,但我认为您的解决方案很简单,在 while 条件之前创建一个标题行。也就是说:
...
echo "<table>";
echo '<tr>';
echo '<th>Title 1</th>';
echo '<th>Title 2</th>';
echo '<th>Title 3</th>';
echo '<th>Title 4</th>';
echo '<th>Title 5</th>';
echo '</tr>';
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['roomCode'] . "</td>";
echo "<td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td>";
echo "<td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td>";
echo "<td>" . $row['wheelchairAccess'] . "</td>";
...
只需在 echo "<table>";
之后插入 <th>
的,如下所示:
echo '<tr>
<th>Title 1</th>
<th>Title 2</th>
<!-- add as many as you need -->
</tr>';
这是一种动态执行此操作的方法。
在while
循环中,使用 if
检查是否存在不存在的变量。然后,在if
内设置该变量。这样,您只能在第一次通过循环进入if
。然后,在 if
中,您可以遍历您的第一个$row
并将其键用作列标题:
<?php
$numeroOption= $_POST['numero'];
$roomtype= $_POST['roomtype'];
$selectOption = $_POST['parkname'];
$query = "SELECT * FROM `ROOMS` WHERE `Capacity` < '$numeroOption' AND `Park` LIKE '$selectOption%' AND `dataProjector` LIKE '$proj_check%' AND `Whiteboard` LIKE '$white_check%' AND `OHP` LIKE '$ohp_check%' AND `WheelchairAccess` LIKE '$wheel_check%' AND `lectureCapture` LIKE '$cap_check%' AND `Style` LIKE '$roomtype%'";
$result = mysql_query($query);
if ($result == FALSE) die ("could not execute statement $query<br />");
?>
<form action="" method="post">
<table>
<?php while ($row = mysql_fetch_array($result)) : ?>
<?php if (!isset($headersDone)) : $headersDone = true; ?>
<tr>
<?php foreach ($row as $key => $value) : ?>
<th><?php echo $key; ?></th>
<?php endforeach; ?>
</tr>
<?php endif; ?>
<tr>
<td><?php echo $row['roomCode']; ?></td>
<td><?php echo $row['Style']; ?></td>
<td><?php echo $row['dataProjector']; ?></td>
<td><?php echo $row['Whiteboard']; ?></td>
<td><?php echo $row['OHP']; ?></td>
<td><?php echo $row['wheelchairAccess']; ?></td>
<td><?php echo $row['lectureCapture']; ?></td>
<td><input type="radio" name="radioSelect" value="<?php echo $row['roomCode']; ?>"></td>
</tr>
<?php endwhile; ?>
<input type="submit" name="ttroom" id="ttroom" name="ttroom">
</table>
</form>
你还会注意到我重新格式化了一堆混合的PHP/HTML代码。只是我的意见,但我认为可以通过打破 <?php ?>
标签而不是一堆 echo
s来使其看起来更干净.PHP控制结构的替代语法非常适合这种情况
在循环访问记录集之前,可以添加包含相关列标题的新行。通常,您可以使用th
标签,但如果您愿意,它可以是标准td
标签。
<?php
$numeroOption= $_POST['numero'];
$roomtype= $_POST['roomtype'];
$selectOption = $_POST['parkname'];
$query = "SELECT * FROM `ROOMS` WHERE `Capacity` < '$numeroOption' AND `Park` LIKE '$selectOption%' AND `dataProjector` LIKE '$proj_check%' AND `Whiteboard` LIKE '$white_check%' AND `OHP` LIKE '$ohp_check%' AND `WheelchairAccess` LIKE '$wheel_check%' AND `lectureCapture` LIKE '$cap_check%' AND `Style` LIKE '$roomtype%'";
$result = mysql_query($query);
if ($result == FALSE) die ("could not execute statement $query<br />");
echo "<form action='' method='post'>";
echo "<table>
<tr>
<th scope='col'>Room Code</th>
<th scope='col'>Style</th>
<th scope='col'>Projector</th>
<th scope='col'>Whiteboard</th>
<th scope='col'>OHP</th>
<th scope='col'>Wheelchair Access</th>
<th scope='col'>Lecture Capture</th>
<th scope='col'> </th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['roomCode'] . "</td>";
echo "<td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td>";
echo "<td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td>";
echo "<td>" . $row['wheelchairAccess'] . "</td>";
echo "<td>" . $row['lectureCapture'] . "</td>";
echo "<td><input type='radio' name='radioSelect' value= '". $row['roomCode']."'></td></tr>";
}
echo "<input type='submit' name='ttroom' id='ttroom' name='ttroom'>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
我没有立即注意到的是循环内缺少闭tr
标签 - 只有一个闭tr
标签,而且它在循环外!要根据实际字段名称动态添加列标题(这是我最初认为您想要做的,您可以使用这样的方法:-
<?php
$numeroOption= $_POST['numero'];
$roomtype= $_POST['roomtype'];
$selectOption = $_POST['parkname'];
$query = "SELECT * FROM `ROOMS` WHERE `Capacity` < '$numeroOption' AND `Park` LIKE '$selectOption%' AND `dataProjector` LIKE '$proj_check%' AND `Whiteboard` LIKE '$white_check%' AND `OHP` LIKE '$ohp_check%' AND `WheelchairAccess` LIKE '$wheel_check%' AND `lectureCapture` LIKE '$cap_check%' AND `Style` LIKE '$roomtype%'";
$result = mysql_query( $query );
if ( $result == FALSE ) die ("could not execute statement $query<br />");
/* an array to store column names */
$colheaders=array();
/* find how many fields in each row there are */
$iCols=mysql_num_fields( $result );
/* Get the column names */
for( $i=0; $i < $iCols; $i++ ) $colheaders[]=mysql_field_name( $result, $i );
/* Add an item for the radio buttons */
$colheaders[]='Options';
/* ensure all of recordset is available to iterate through after getting field names*/
mysql_data_seek( $result, 0 );
echo "<form action='' method='post'>";
echo "<table>
<tr>";
foreach( $colheaders as $colname ) echo "<th scope='col'>{$colname}</th>";
echo "
</tr>";
while( $row = mysql_fetch_array($result) ){
echo "
<tr>
<td>" . $row['roomCode'] . "</td>
<td>" . $row['Style'] . "</td>
<td>" . $row['dataProjector'] . "</td>
<td>" . $row['Whiteboard'] . "</td>
<td>" . $row['OHP'] . "</td>
<td>" . $row['wheelchairAccess'] . "</td>
<td>" . $row['lectureCapture'] . "</td>
<td><input type='radio' name='radioSelect' value= '". $row['roomCode']."'></td>
</tr>";
}
echo "
<tr>
<td colspan='".count($colheaders)."'><input type='submit' name='ttroom' id='ttroom' name='ttroom'></td>
</tr>
</table>
</form>";
?>