我试图用php创建一个表,根据用户选中的复选框显示mysql数据库上的数据。
正如您在这个屏幕快照中看到的,如果您没有在最后一个复选框之前选中一个复选框,它将出现问题:
http://www.mypicx.com/04282010/1/
这是我的代码:
if($_POST['general'] == 'ADDRESS'){
$result2 = mysql_query("SELECT * FROM student WHERE ADDRESS='$saddress'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<?php if ( $ShowLastName )
echo "<th>LASTNAME</th>" ?>
<?php if ( $ShowFirstName )
echo "<th>FIRSTNAME</th>" ?>
<?php if ( $ShowMidName )
echo "<th>MIDNAME</th>" ?>
<?php if ( $ShowAddress )
echo "<th>ADDRESS</th>" ?>
<?php if ( $ShowGender )
echo "<th>GENDER</th>" ?>
<?php if ( $ShowReligion )
echo "<th>RELIGION</th>" ?>
<?php if ( $ShowBday )
echo "<th>BIRTHDAY</th>" ?>
<?php if ( $ShowContact )
echo "<th>CONTACT</th>" ?>
</tr>
<?php
while($row = mysql_fetch_array($result2))
{?>
<tr>
<td><?php echo $row['IDNO']?> </td>
<td><?php echo $row['YEAR'] ?> </td>
<td><?php echo $row['SECTION'] ?></td>
<td><?php
if ( $ShowLastName )
echo $row['LASTNAME'] ?></td>
<td><?php
if ( $ShowFirstName )
echo $row['FIRSTNAME'] ?></td>
<td><?php
if ( $ShowMidName )
echo $row['MI'] ?></td>
<td><?php
if ( $ShowAddress )
echo $row['ADDRESS'] ?></td>
<td><?php
if ( $ShowGender )
echo $row['GENDER'] ?></td>
<td><?php
if ( $ShowReligion )
echo $row['RELIGION'] ?></td>
<td><?php
if ( $ShowBday )
echo $row['BIRTHDAY'] ?></td>
<td><?php
if ( $ShowContact )
echo $row['S_CONTACTNUM'] ?></td>
</tr>
<?PHP } ?>
</table>
<?PHP }
mysql_close($con);
?>
当您在未单击复选框之前选中一个复选框时,您可以推荐什么以使输出不会像这样:http://www.mypicx.com/04282010/2/
最佳答案
好的,首先,让我们来清理代码,因为以当前格式阅读太难了:
<?php
if($_POST['general'] == 'ADDRESS'){
$result2 = mysql_query("SELECT * FROM student WHERE ADDRESS='$saddress'");
?>
<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>SECTION</th>
<?php if ( $ShowLastName ) { ?><th>LASTNAME</th><?php } ?>
<?php if ( $ShowFirstName ) { ?><th>FIRSTNAME</th><?php } ?>
<?php if ( $ShowMidName ) { ?><th>MIDNAME</th><?php } ?>
<?php if ( $ShowAddress ) { ?><th>ADDRESS</th><?php } ?>
<?php if ( $ShowGender ) { ?><th>GENDER</th><?php } ?>
<?php if ( $ShowReligion ) { ?><th>RELIGION</th><?php } ?>
<?php if ( $ShowBday ) { ?><th>BIRTHDAY</th><?php } ?>
<?php if ( $ShowContact ) { ?><th>CONTACT</th><?php } ?>
</tr>
<?php while($row = mysql_fetch_array($result2)) {?>
<tr>
<td><?php echo $row['IDNO']?> </td>
<td><?php echo $row['YEAR'] ?> </td>
<td><?php echo $row['SECTION'] ?></td>
<?php if ( $ShowLastName ) { echo('<td>'.$row['LASTNAME'].'</td>'); } ?></td>
<?php if ( $ShowFirstName ) { echo('<td>'.$row['FIRSTNAME'].'</td>'); } ?>
<?php if ( $ShowMidName ) { echo('<td>'.$row['MI'].'</td>'); } ?>
<?php if ( $ShowAddress ) { echo('<td>'.$row['ADDRESS'].'</td>'); } ?>
<?php if ( $ShowGender ) { echo('<td>'.$row['GENDER'].'</td>'); } ?>
<?php if ( $ShowReligion ) { echo('<td>'.$row['RELIGION'].'</td>'); }?>
<?php if ( $ShowBday ) { echo('<td>'.$row['BIRTHDAY'].'</td>'); }?>
<?php if ( $ShowContact ) { echo('<td>'.$row['S_CONTACTNUM'].'</td>'); }?>
</tr>
<?php } ?>
</table>
<?php }
mysql_close($con);
?>
你最好的办法就是试着把这些代码放进去,告诉我们这些代码是否能改善情况?
编辑
啊,正如其他人所说,您的
<td>
标记不在您的条件之内,但是,上面的代码更容易阅读,并且有助于将来的调试:-)