我尝试在以下代码中向表中添加计数器。但我无法成功。我可以帮忙吗?谢谢。像这样:
$counter = 0;
$counter++;
if($counter % 33 == 0)
因此,当添加计数器时,表格将在页面右侧的%33之后继续,并且它将继续这样,而不是沿着页面下移。
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="etc/sumain.css" />
</head>
<body>
<table class="tbresult">
<?php
include ("confige.php");
$query = 'select * from employees';
$result = mysqli_query($link, $query);
if (!$result) {
$message = 'ERROR:' . mysqli_error($link);
return $message;
} else {
$i = 0;
echo '<form name="select" action="" method="GET">';
echo '<select name="mySelect" id="mySelect" onchange="this.form.submit()">';
while ($i < mysqli_field_count($link)) {
$meta =
mysqli_fetch_field_direct($result, $i);
echo '<option>' . $meta->name . '</option>';
$i = $i + 1;
}
echo '</select>';
echo '</form>';
}
if(isset($_GET['mySelect'])) {
$myselect = $_GET['mySelect'];
$sql = "SELECT `$myselect` as mySelect FROM employees"; // add column alias
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
}
echo "</table>";
}
}
mysqli_close($link);
?>
</body>
</html>
答案 0 :(得分:1)
首先,在表格中没有HTML表单,这是无效的,AFIK,并且可能会在不同的浏览器中引起很多麻烦。
您只需打开一次表,然后创建一个counter = 0,并在每个while循环上添加1。然后检查,如果它除以33而不是关闭表并打开一个新表。循环结束后,关闭最后一个表。
可以使用CSS完成并排对齐,例如.tbresult {float: left; width: 200px;}
这样的事情:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="etc/sumain.css" />
</head>
<body>
<?php
include ("confige.php");
$query = 'select * from employees';
$result = mysqli_query($link, $query);
if (!$result) {
$message = 'ERROR:' . mysqli_error($link);
return $message;
} else {
$i = 0;
echo '<form name="select" action="" method="GET">';
echo '<select name="mySelect" id="mySelect" onchange="this.form.submit()">';
while ($i < mysqli_field_count($link)) {
$meta =
mysqli_fetch_field_direct($result, $i);
echo '<option>' . $meta->name . '</option>';
$i = $i + 1;
}
echo '</select>';
echo '</form>';
}
if(isset($_GET['mySelect'])) {
$myselect = $_GET['mySelect'];
$sql = "SELECT `$myselect` as mySelect FROM employees"; // add column alias
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
// output data of each row
$table_row_counter = 0;
echo '<table class="tbresult">';
while($row = $result->fetch_assoc())
{
$table_row_counter++;
if ($table_row_counter % 33 == 0) {
echo '</table>';
echo '<table class="tbresult">';
}
echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
}
}
}
mysqli_close($link);
?>
</body>
</html>