2017-03-04 84 views
0

我有一個關於隱藏空列的問題。用Php隱藏列

如何隱藏< th>姓名</th>當我有空列的頭列?

在接下來的日子裏我工作到SWITH到mysqli的所有查詢:-)

QUERY 
$query = " SELECT * FROM users "; 
$result = mysql_query($query) or die(mysql_error()); 
$num = mysql_num_rows($result); 

TABLE 
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> 

<thead> 
    <tr> 
     <th>NAME</th> 
     <th>SURNAME</th> 
    </tr> 
</thead> 

<tbody> 
    <?php 
     $i=0; 
     while ($i < $num) { 
      $id = mysql_result($result,$i,"id"); 
      $name = mysql_result($result,$i,"name"); 
      $surname = mysql_result($result,$i,"surname"); 
    ?> 

    <tr> 
     <td><?php echo $name; ?></td> 
     <td><?php echo $surname; ?></td> 
    </tr> 

    <?php 
     $i++; 
     } 
    ?> 
</tbody> 

</table> 
+0

只是做呼應''之前的檢查(以及相應的'​​'),看看是否有姓氏的結果。我假設你可能不知道只使用'$ num'變量? (即你可以有一個名字,但不是姓?)。如果它不是/或者,您可以使用'$ num'變量進行檢查。此外,不需要將'mysql_result()'調用分配給變量,您可以直接在'​​'內將它們回顯出來(將它們添加到變量中會產生不必要的負載)。 – junkfoodjunkie

+0

http://php.net/manual/en/function.empty.php --- http://php.net/manual/en/control-structures.if.php --- http://php.net /manual/en/language.operators.comparison.php –

回答

1

要隱藏列,您需要同時前檢查。

$array_surname = array(); 
    while($res_head_surname = mysql_fetch_array($result)) { 
     $array_surname[] = $res_head_surname; 
    } 

在此之後,插入條件,只要你喜歡,並加入到頭表:

<?php if ($show_surname == 1) {?><th>SURNAME</th><?php } ?> 
0

試試這個!只要把一個如果檢查姓氏如果存在或不。並在回聲中包含標籤。

QUERY 
$query = " SELECT * FROM users "; 
$result = mysql_query($query) or die(mysql_error()); 
$num = mysql_num_rows($result); 
TABLE 
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> 

<thead> 
    <tr> 
     <th>NAME</th> 
     <th>SURNAME</th> 
    </tr> 
</thead> 

<tbody> 
    <?php 
     $i=0; 
     while ($i < $num) { 
      $id = mysql_result($result,$i,"id"); 
      $name = mysql_result($result,$i,"name"); 
      $surname = mysql_result($result,$i,"surname"); 
    ?> 

    <tr> 
 <?php echo "<td>".$name."</td>"; ?> 
     <td><?php if($surname){echo "<th>".$surname."</th>";} ?></td> 
</tr> 

    <?php 
     $i++; 
     } 
    ?> 
</tbody> 

</table> 
+0

其 SURNAME !! –

+0

我已編輯您可以檢查的代碼。 –