dongxiai3003 2016-11-22 14:38
浏览 6

根据其中的数据显示的动态列中的表

ask help to you programmers more experienced than me for a problem plaguing me for weeks.

<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>

You have probably already figured out what effect I try.

I would like to have a php code that allows me to delete the entire column [Only Seasons Label], if the "entire column" (every single cell) has no value (exclude the first row).

Considerations: If a cell, at any height contains value, the column must not be deleted. Practically this table has a matrix structure which the labels are in the first column and first row.

Developer language: php. I'm junior/intermediate coder.

</div>
  • 写回答

2条回答 默认 最新

  • dongqu1783 2016-11-22 17:36
    关注

    So basically what you need is an array to keep track if a column has had a value after printing all of the rows.

    I'm using just 3 columns to make the code more readable.

    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 
          }
        }
    } 
    

    the line

    if ($count['a']!= "") $count['a']++;
    

    is the same as

    if ($count['a']!= "") {
      $count['a']++;    
    }
    

    but saves space.

    NOTE: This works if the cell is empty or NULL, but not with a space or anything else.

    FINAL NOTE: if you assign an id or a class to the cells it will simplify the process of hiding the cells with css and javascrit

    This approach takes into account performance, it only loops once a simple array of integers to check if the cell should be hidden. It is easier to delete unused columns, than doing 2 full loops of all the data to see which columns should not be printed from the beginning.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值