请求帮助程序员比我更有经验困扰我几周的问题。
<table class="table-bordered table hover table-striped" id="grades_degree">
<tr>
<td >COURSES TAKEN</td>
<td rel='a'>FALL 2013</td>
<td rel='b'>WINTER 2014</td>
<td rel='c'>SPRING 2014</td>
<td rel='d'>SUMMER 2014</td>
<td rel='e'>FALL 2014</td>
<td rel='f'>WINTER 2015</td>
<td rel='g'>SPRING 2015</td>
<td rel='h'>SUMMER 2015</td>
<td rel='i'>FALL 2015</td>
<td rel='j'>WINTER 2016</td>
<td rel='k'>SPRING 2016</td>
<td rel='l'>FALL 2016</td>
<td rel='m'>GREEN 2014</td>
<td rel='n'>ORANGE 2015</td>
<td rel='o'>RED 2015</td>
<td rel='p'>YELLOW 2015</td>
<td rel='q'>BLUE 2015</td>
<td rel='r'>GREEN 2015</td>
<td rel='s'>ORANGE 2016</td>
<td rel='t'>RED 2016</td>
<td rel='u'>WHITE 2016</td>
<td>GPA</td>
<td>CREDITS</td>
</tr>
<?php
$sql = "SELECT * FROM grades WHERE student_id=$id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row["course"]."</td>";
echo "<td rel='a'>".$row["fall2013"] ."</td>";
echo "<td rel='b'>".$row["winter2014"] ."</td>";
echo "<td rel='c'>".$row["spring2014"] ."</td>";
echo "<td rel='d'>".$row["summer2014"] ."</td>";
echo "<td rel='e'>".$row["fall2014"] ."</td>";
echo "<td rel='f'>".$row["winter2015"] ."</td>";
echo "<td rel='g'>".$row["spring2015"] ."</td>";
echo "<td rel='h'>".$row["summer2015"] ."</td>";
echo "<td rel='i'>".$row["fall2015"] ."</td>";
echo "<td rel='j'>".$row["winter2016"] ."</td>";
echo "<td rel='k'>".$row["spring2016"] ."</td>";
echo "<td rel='l'>".$row["fall2016"] ."</td>";
echo "<td rel='m'>".$row["green2014"] ."</td>";
echo "<td rel='n'>".$row["orange2015"] ."</td>";
echo "<td rel='o'>".$row["red2015"] ."</td>";
echo "<td rel='p'>".$row["yellow2015"] ."</td>";
echo "<td rel='q'>".$row["blue2015"] ."</td>";
echo "<td rel='r'>".$row["green2015"] ."</td>";
echo "<td rel='s'>".$row["orange2015"] ."</td>";
echo "<td rel='t'>".$row["red2016"] ."</td>";
echo "<td rel='u'>".$row["white2016"] ."</td>";
echo "<td>".str_replace(',','.',$row["gpa"]) ."</td>";
echo "<td>".$row["credits"] ."</td>";
echo "</tr>";
}
}
?>
</table>
你可能已经知道我尝试了什么效果。
我想有一个PHP代码,允许我删除整个列[Only Seasons Label],如果“整个列”(每个单元格)没有值(排除第一行)。
考虑: 如果某个单元格在任何高度都包含值,则不得删除该列。实际上,这个表有一个矩阵结构,标签位于第一列和第一行。
开发人员语言:php。我是初级/中级编码员。
答案 0 :(得分:0)
所以基本上你需要的是一个数组,以便在打印完所有行后列中有一个值时跟踪。
我只使用3列来使代码更具可读性。
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row["course"]."</td>";
echo "<td rel='a'>".$row["fall2013"] ."</td>";
echo "<td rel='b'>".$row["winter2014"] ."</td>";
echo "<td rel='c'>".$row["spring2014"] ."</td>";
echo "<td>".str_replace(',','.',$row["gpa"]) ."</td>";
echo "<td>".$row["credits"] ."</td>";
echo "</tr>";
// the following lines check if the cell of a specific column has a value
if ($count['a']!= "") $count['a']++; // If it is not empty it adds 1 to the count of that column
if ($count['b']!= "") $count['b']++;
if ($count['c']!= "") $count['c']++;
}
foreach ($count as $rel => $total) {
if ($total == 0){ // if Total count equals zero, it means that the $rel column never had a value in any of the rows
echo "the column $rel has no values and should not appear" // for testing only
// you now have the identity of the column to hide --> $rel
// here you hide the column with jquery or javascript
}
}
}
行
if ($count['a']!= "") $count['a']++;
与
相同if ($count['a']!= "") {
$count['a']++;
}
但节省空间。
注意:如果单元格为空或NULL,但没有空格或其他任何内容,则此方法有效。
最后注意:如果你为单元格分配一个id或一个类,它将简化用css和javascrit隐藏单元格的过程
这种方法考虑了性能,它只循环一次简单的整数数组,以检查是否应该隐藏单元格。 删除未使用的列比删除所有数据的2个完整循环更容易,以查看从一开始就不应打印哪些列。
答案 1 :(得分:0)
<?php
$sql = "SELECT * FROM grades WHERE student_id=$id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$results_grades[] = $row;
}
}
echo "<table class='table-bordered table hover table-striped' id='grades_degree'>";
foreach($results_grades as $value) {
echo "<tr>";
foreach($value as $key => $val) {
if ($val!="") {
echo "<td>".$key."</td><td> ".$val."</td>";
}
}
echo "</tr>";
}
echo "</table>";
?>
反思解决方案我设法获得了一个多维数组,它允许我使用非常脏的代码,甚至可以获得所需的效果。 向前迈出一小步。