首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从动态生成的html-table中删除选定的列?

如何从动态生成的html-table中删除选定的列?
EN

Stack Overflow用户
提问于 2016-10-01 06:28:31
回答 4查看 660关注 0票数 1

我有一个SQL数据库,我在其中读出数据,然后在动态生成的html表中显示这些数据。下面是我的代码,运行得很好:

代码语言:javascript
复制
$sql = "SELECT $selection FROM $tabelle WHERE $masterarray";


$result = mysqli_query($db, $sql) or die("Invalid query");             
$numrows = mysqli_num_rows($result);  
$numcols = mysqli_num_fields($result); 
$field = mysqli_fetch_fields($result); 


if ($numrows > 0) {

echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th>";


for($x=0;$x<$numcols;$x++){
echo "<th>" . $field[$x]->name . "</th>";
}

echo "</tr>";
echo "</thead>";

echo "<tbody>";
echo "<tr>";
$nr = 1;

while ($row = mysqli_fetch_array($result)) {
  echo "<td>" . $nr . "</td>";
  for ($k=0; $k<$numcols; $k++) {    
  echo "<td>" . $row[$k] . "</td>"; //Prints the data
  }

 $nr = $nr + 1;
 echo "</tr>";

 }
 echo "</tbody>";
 echo "</table>";

}
}

mysqli_close($db); 

现在,我想删除特定的列(例如,那些为空的列,或者那些对发出请求的用户不是很感兴趣的列)。

我在unset($field[$variable])上试过了,但是它不起作用。此外,还应该删除这些值(如果有)。

EN

回答 4

Stack Overflow用户

发布于 2016-10-01 06:32:21

始终在打印数组之前对其进行格式化。在回显$field之前,尝试从HTML数组中删除特定的列,然后打印最终的表。一旦HTML代码在PHP中得到回显,如果不使用JavaScript,就无法将其删除。

您可以对照$field[$x]->name变量进行检查,并使用continue跳过该列。

票数 0
EN

Stack Overflow用户

发布于 2016-10-01 06:34:44

可以让mysql为你过滤掉它们,$sql = "SELECT $selection FROM $tabelle WHERE $masterarray AND LENGTH($selection) > 0";

-- http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_length

票数 0
EN

Stack Overflow用户

发布于 2016-10-01 08:13:25

代码语言:javascript
复制
<?php

//  DataBase Config - http://php.net/manual/pt_BR/pdo.construct.php.
$dsn = 'mysql:host=localhost;dbname=test';
$usr = 'root';
$pwd = '';

try {   //  try to connect in database.

    $pdo = new PDO($dsn, $usr, $pwd);

} catch (PDOException $e) { //  if there is error in the connection.

    die('Connection failed: ' . $e->getMessage());

}

//  Prepare Statement and execute - http://php.net/manual/pt_BR/pdo.prepare.php.
$stm = $pdo->prepare('select id, weight, color, name from product');
$stm->execute();

//  Get ALL rows - Object.
$rows = $stm->fetchAll(PDO::FETCH_OBJ);

//  Print Rows.
//echo '<pre>'.print_r(rows, true).'</pre>';

//  Check $row;
if (count($rows)) {

    //  Order and Display Cols.
    $colsDisplay = [
        'id'        => 'ID Product',
        'name'      => 'Name',
        'weight'    => 'Weigth'
    ];

    //  Table.
    $html = '<table border="1">';
    $html .= "\n    <thead>";
    $html .= "\n        <tr>";
    $html .= "\n            <th  bgcolor='#eee'>Row</th>";
    $html .= "\n            <th>". implode("</th>\n         <th>", $colsDisplay) ."</th>";
    $html .= "\n        </tr>";
    $html .= "\n    </thead>";
    $html .= "\n    <tbody>";

    //  Loop ROWS.
    foreach ($rows as $key => $val) {

        $html .= "\n        <tr>";

        $html .= "\n            <td bgcolor='#eee'>". $key ."</td>";

        //  Loop COLS to display.
        foreach ($colsDisplay as $thKey => $thVal) {

            $html .= "\n            <td>". $val->$thKey ."</td>";

        }

        $html .= "\n        </tr>";

    }

    $html .= "\n".' </tbody>';
    $html .= "\n".'</table>';

    echo $html;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39800965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档