我有一个从数据库中提取信息的表。为了让访问网页的人看起来整洁,如果没有信息,我希望隐藏一些列。
我的表格是全年赛车活动的结果。在今年年初,只有1个活动,但到12月时,将会有20多个结果。因此,我希望隐藏它们,而不是拥有大量空列,直到添加了该事件的标题。
我尝试使用!empty
,但这并不是我想要的。它只是将空行移到左侧,使得标题与正确的结果不匹配。
这是我的代码:
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, (u181 + u182 + u183 + u184 + u185 + u186 + u187 + u188 + u189 + u1810 + u1811 + u1812 + u1813 + u1814) AS total FROM results WHERE u18 = '1'");
$title = $mysqli->query("SELECT * FROM closedevents");
while ($t = $title->fetch_assoc()){
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>
<td>" . $t["u18a"] . "</td>
<td>" . $t["u18b"] . "</td>
<td>" . $t["u18c"] . "</td>
<td>" . $t["u18d"] . "</td>
<td>" . $t["u18e"] . "</td>
<td>" . $t["u18f"] . "</td>
<td>" . $t["u18g"] . "</td>
<td>" . $t["u18h"] . "</td>
<td>" . $t["u18i"] . "</td>
<td>" . $t["u18j"] . "</td>
<td>" . $t["u18k"] . "</td>
<td>" . $t["u18l"] . "</td>
<td>" . $t["u18m"] . "</td>
<td>" . $t["u18n"] . "</td>
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["competitor"].'</td>';
echo '<td>'.$row["u181"].'</td>';
echo '<td>'.$row["u182"].'</td>';
echo '<td>'.$row["u183"].'</td>';
echo '<td>'.$row["u184"].'</td>';
echo '<td>'.$row["u185"].'</td>';
echo '<td>'.$row["u186"].'</td>';
echo '<td>'.$row["u187"].'</td>';
echo '<td>'.$row["u188"].'</td>';
echo '<td>'.$row["u189"].'</td>';
echo '<td>'.$row["u1810"].'</td>';
echo '<td>'.$row["u1811"].'</td>';
echo '<td>'.$row["u1812"].'</td>';
echo '<td>'.$row["u1813"].'</td>';
echo '<td>'.$row["u1814"].'</td>';
echo '<td>'.$row["total"].'</td>';
echo "</tr>";
$counter++; //increment count by 1
}
echo "</table>";
?>
因此,如果u18j,k,l,m,n中没有标题名称,那么它们的列将不会显示在表格中。
答案 0 :(得分:1)
PHP替代方案:
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, (u181 + u182 + u183 + u184 + u185 + u186 + u187 + u188 + u189 + u1810 + u1811 + u1812 + u1813 + u1814) AS total FROM results WHERE u18 = '1'");
$title = $mysqli->query("SELECT * FROM closedevents");
$all_cols=array("u18a" => "u181","u18b"=> "u182","u18c"=> "u183","u18d"=> "u184","u18e"=> "u185","u18f"=> "u186","u18g"=> "u187",
"u18h"=> "u188","u18i"=> "u189","u18j"=> "u1810","u18k"=> "u1811","u18l"=> "u1812","u18m"=> "u1813","u18n"=> "u1814");
while ($t = $title->fetch_assoc()){
// remember empty cols
$empty_cols=array();
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>";
foreach ($all_cols as $col => $value) {
if (!empty($t[$col])) {
echo "<td>" . $t[$col] . "</td>";
} else {
// set this column as empty for later
$empty_cols[]=$col;
}
} unset($col); unset($value);
echo "
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["competitor"].'</td>';
foreach ($all_cols as $col => $value) {
if (!in_array($col, $empty_cols)) {
// echo non-empty values
echo '<td>'.$row[$value].'</td>';
}
} unset($col); unset($value);
$counter++; //increment count by 1
}
echo "</table>";
?>
答案 1 :(得分:0)
好。我在jQuery中帮助你。显然,您必须在代码中包含jquery库。更多信息:http://jquery.com
首先,您必须使用标识符属性更改您的表:
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>
<td rel='a'>" . $t["u18a"] . "</td>
<td rel='b'>" . $t["u18b"] . "</td>
<td rel='c'>" . $t["u18c"] . "</td>
<td rel='d'>" . $t["u18d"] . "</td>
<td rel='e'>" . $t["u18e"] . "</td>
<td>Total</td>
</tr>";
然后,在你的同时,同样的:
echo '<td rel="a">'.$row["u181"].'</td>';
echo '<td rel="b">'.$row["u182"].'</td>';
echo '<td rel="c">'.$row["u183"].'</td>';
echo '<td rel="d">'.$row["u184"].'</td>';
echo '<td rel="e">'.$row["u185"].'</td>';
然后是javascript魔术:
// select the first row and found all td in the first row
$('table.entrywriting tr:first-child').find('td').each(function() {
var text = $(this).text(); // get the inner text
if(!text) { // if text is empty
var position = $(this).attr('rel'); //position like a, b, c...
// iterate all tds in this position
$('table.entrywriting td[rel="'+position+'"]').each(function() {
$(this).hide(); // hide the td with the position
});
}
});
祝你好运!
答案 2 :(得分:0)
我认为你可以测试varibables和reduc到一个这样的echo指令:
<?php
$counter = 1;
$out ='';
while($row = $results->fetch_assoc()) {
$out .= "<tr align=\"center\">";
$out .= "<td>" . $counter . "</td>";
$out .= (isset($row["competitor"]) && !empty(trim($row["competitor"]))?'<td>'.$row["competitor"].'</td>':'';
..... etc
$out .= "</tr>";
$counter++; //increment count by 1
}
echo $out;