我想在PHP / HTML表格中显示这样的MySQL结果。如果以后容易,可以在每个工厂添加鼠标信息。
+-----------------------------------------------------------+ |category1 ¦ category2 ¦ category3 ¦ category4 ¦ category5 ¦ +-----------+-----------+-----------+-----------+-----------+ | plantName ¦ plantName ¦ plantName ¦ plantName ¦ plantName ¦ | plantName ¦ plantName ¦ plantName ¦ plantName ¦ plantName ¦ | plantName ¦ plantName ¦ ¦ ¦ ¦ | ¦ plantName ¦ ¦ ¦ ¦ | ¦ plantName ¦ ¦ ¦ ¦ +-----------+-----------+-----------+-----------+-----------+
首先,我根据海拔和降雨值选择植物。
$sql = mysql_query("SELECT * FROM `plants_tb`
WHERE $elevate>= elevationLOW && $elevate<= elevationHI &&
$rainfall>= rainfallLOW && $rainfall<= rainfallHI ORDER BY commonNames ASC");
$plant = 'commonNames';
$elevationH = 'elevationHI';
$elevationL ='elevationLOW';
$rainfallL ='rainfallLOW';
$rainfallH ='rainfallHI';
$species ='species';
echo "<table border='1'><tr><th>Name</th><th>Category</th></tr>";
while($row = mysql_fetch_array($sql)){
echo "<tr><td>" . $row[$plant] . "</td><td>" . $row['heightHI'] . "</td></tr>";
}
echo "</table>";
现在我需要按高度类别在列中显示它们。也许我应该制作所选植物的临时表,然后对它们进行分类,然后将它们显示在列中? 这是我的分类想法。我知道这在代码之间是不正确的,但它得到了我的观点。
$sql="SELECT tree_height FROM $Elevation_Rainfall_list;
WHERE tree_height
BETWEEN 1 AND 7 = $Category1
BETWEEN 7 AND 15 = $Category2
BETWEEN 15 AND 30 = $Category3
BETWEEN 30 AND 9999 = $Category4
if not = $Category5
mahalo的支持!
答案 0 :(得分:3)
看起来你想创建类似直方图的东西。
您应该在PHP中使用以下内容对SQL查询中的数据进行分类,而不是尝试对数据进行分类:
$histogram = array();
# classify the data into histogram bins
while($row = mysql_fetch_array($sql)) {
$h = $row['tree_height'];
$cat = 3;
if ($h <= 7) {
$cat = 0;
} elseif ($h <= 15) {
$cat = 1;
} elseif ($h <= 30) {
$cat = 2;
}
$histogram[$cat][] = $row['commonNames'];
}
# determine the number of rows in the table
$rows = 0;
foreach ($histogram as $ar) { $rows = max($rows, count($ar)); }
# write a table with the data
echo "<table border='1'>\n" .
" <tr>\n <td>Category 1</td>\n <td>Category 2</td>\n" .
" <td>Category 3</td>\n <td>Category 4</td>\n</tr>\n";
for ($i = 0; $i < $rows; ++$i) {
echo " <tr>\n";
for ($cat = 0; $cat <= 3; ++$cat) {
echo " <td>" . $histogram[$cat][$i] . "</td>\n";
}
echo " </tr>\n";
}
echo "</table>\n";