我在SQL中创建了一个数据库,我正在尝试回显HTML表中的数据。我创建了一个单独的PHP文件,它将引用特定条件(在本例中为行业),用于确定表中显示的记录。
我的代码提取所有匹配的条件,但只显示表格中的第一个结果。所有其他结果只是在没有空格的情况下编写。知道如何解决这个问题吗?谢谢!
mysql_select_db("project") or die(mysql_error());
print ("<h1>Database</h1>");
print("</br>");
print("</br>");
if($industry == "Media")
{
$strSQL = "SELECT * FROM db WHERE industry = 'Media' ";
$data = count($id);
} elseif ($industry == "Finance")
{
$strSQL = "SELECT * FROM db WHERE industry = 'Finance' ";
} elseif ($industry == "Technology")
{
$strSQL = "SELECT * FROM db WHERE industry = 'Technology' ";
} elseif ($industry == "Health")
{
$strSQL = "SELECT * FROM db WHERE industry = 'Health care/biotechnology' ";
} elseif ($industry == "RealEstate")
{
$strSQL = "SELECT * FROM db WHERE industry = 'Real Estate' ";
} elseif ($industry == "Retail")
{
$strSQL = "SELECT * FROM db WHERE industry = 'Retail' ";
} elseif ($industry == "Other")
{
$strSQL = "SELECT * FROM db WHERE industry = 'Other' ";
} elseif ($industry == "Technology")
{
$strSQL = "SELECT * FROM db WHERE industry = 'Technology' ";
}
?>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Column 3</th>
<th>Column 4</th>
<th>Column 5</th>
<th>Column 6</th>
</tr>
<?php
$id = $row['id'];
$firstName = $row['firstName'];
$lastName = $row['lastName'];
$company = $row['company'];
$totaldollar = $row['totaldollar'];
$formatted_dollar = number_format($totaldollar);
$dollar2 = $row['dollar2'];
$formatted_dollar2 = number_format($dollar2);
$year = $row['year'];
$industry = $row['industry'];
?>
<?php
for($i = 0; $i < count($id); $i++)
{
echo "<tr><td>" . $firstName . "</td><td>" . $lastName . "</td><td>" . $company . "</td><td>" . "$" . $formatted_dollar . "</td><td>" . "$" . $formatted_dollar2 . "</td><td>" . $industry . "</td></tr>" ;
}
?>
</table>
<?php
}
mysql_close($link);
?>
</body>
</html>
答案 0 :(得分:0)
不确定你在哪里煽动$row
。但是,既然您将$id
视为数组 - $i < count($id)
,那么您可能希望将它们设置为数组 -
<?php
$id[] = $row['id'];
$firstName[] = $row['firstName'];
$lastName[] = $row['lastName'];
$company[] = $row['company'];
$totaldollar[] = $row['totaldollar'];
$formatted_dollar[] = number_format($totaldollar);
$dollar2[] = $row['dollar2'];
$formatted_dollar2[] = number_format($dollar2);
$year[] = $row['year'];
$industry[] = $row['industry'];
?>
然后在循环中,您需要使用$i
来设置数组键
<?php
for($i = 0; $i < count($id); $i++)
{
echo "<tr><td>" . $firstName[$i] . "</td><td>" . $lastName[$i] . "</td><td>" . $company[$i] . "</td><td>" . "$" . $formatted_dollar[$i] . "</td><td>" . "$" . $formatted_dollar2[$i] . "</td><td>" . $industry[$i] . "</td></tr>" ;
}
?>