如果我的问题听起来很愚蠢,那么我很擅长编程。我试图从MySQL数据库中列出行。我的要求是为部门下的每个指定连续处理每个项目的计数器。 例如:我的每一行都有部门名称和名称,对应MySQL DB中的部门。我试图在下面演示MyMSQL表的结构。我想要获得的输出是以下格式
Department Desig1 Desig2 Desig3
========== ====== ====== ======
IT Director Manager Asst. Mgr
Finance Finance Contr Finance Mgr Chartd Acct.
Admin CEO Gen. Mgr. Mgr
这就是我期待的输出方式。
IT
1 Director
2 Manager
3 Asst. Manager
Finance
1 Finance Controller
2 Finance Manager
3 Charted Accountant
Administration
1 CEO
2 General Manager
3 Manager
我尝试使用以下代码从MySQL检索数据,无法以我正在寻找的格式获得结果。尝试在同一个循环中添加一个增量,认为它可能会有所帮助,但我错了。请指教。
注意:我正在使用bootstrap进行背景设计。
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Dept, Desig1, Desig2, Desig3 FROM comdept";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
i=0;
echo "<div class='panel-heading'><div class='panel-title'><strong>".$row["Dept"]."</strong></div></div><table class='table table-bordered table-hover'><thead><tr><th class='col-sm-1'>SL</th><th>Designations</th></tr></thead><tbody><tr><td>".i++."</td><td>".$row["Desig1"]."</td></tr><tr><td>".i++."</td><td>".$row["Desig2"]."</td></tr><tr><td>".i++.".</td><td>".$row["Desig3"]."</td></tr></tbody></table>";
}
}
else
{
echo "0 results";
}
?>
答案 0 :(得分:2)
请尝试使用此格式:
$i=0;
echo "<table class='table table-bordered table-hover'>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td colspan=2>".$row["Dept"]."</td></tr>";
echo "<tr><td>".$i++."</td><td>".$row["Desig1"]."</td></tr>";
echo "<tr><td>".$i++."</td><td>".$row["Desig2"]."</td></tr>";
echo "<tr><td>".$i++."</td><td>".$row["Desig3"]."</td></tr>";
echo "<tr><td colspan=2></td></tr>";
}
echo "</table>";
霍普这有帮助。
答案 1 :(得分:0)
您查询时出现错误&#34; SELECT Dept,Desig1,Desig2,Desig3 FROM comdept&#34;列标题是&#34;部门&#34;而不是&#34;部门&#34; 这是相关的 `
echo $sql = "SELECT Department, Desig1, Desig2, Desig3 FROM comdept";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table class='table table-bordered table-hover'>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td colspan=2>".$row["Department"]."</td></tr>";
echo "<tr><td>1</td><td>".$row["Desig1"]."</td></tr>";
echo "<tr><td>2</td><td>".$row["Desig2"]."</td></tr>";
echo "<tr><td>3</td><td>".$row["Desig3"]."</td></tr>";
echo "<tr><td colspan=2></td></tr>";
//$i=0;
//echo "<div class='panel-heading'><div class='panel-title'><strong>".$row["Dept"]."</strong></div></div><table class='table table-bordered table-hover'><thead><tr><th class='col-sm-1'>SL</th><th>Designations</th></tr></thead><tbody><tr><td>".i++."</td><td>".$row["Desig1"]."</td></tr><tr><td>".i++."</td><td>".$row["Desig2"]."</td></tr><tr><td>".i++.".</td><td>".$row["Desig3"]."</td></tr></tbody></table>";
}echo "</table>"; else{echo "0 results";}
`