首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将SQLite表格数据导入到HTML表格中

将SQLite表格数据导入到HTML表格中
EN

Stack Overflow用户
提问于 2016-11-26 17:23:01
回答 3查看 6.4K关注 0票数 0

我有一个SQLite数据库,使用PHP来提供数据并与之交互。

我正在尝试构建一个HTML表,将每个SQLite表的列作为HTML table列,然后将SQLite表中的每一行呈现为HTML表中的每一行。

的问题是每个sql行数据只被渲染成一个表格行(带有当前代码)(应该是针对每一行)问题是我如何将SQL行数据放入循环通过以渲染html表格行的数组中。

目前我有;

PHP将SQL列放入HTML表中,并将行数据( SQl )放入HTML表中(foreach)

代码语言:javascript
复制
//get table count
$countTable = $dbConnect->querySingle("SELECT COUNT(*) as count FROM ".$table['name']."");

if($countTable){

//data exists, do work
//new query to get table data
$query = $dbConnect->query("SELECT * FROM ".$table['name']." ORDER BY id DESC");

//init arrays
$dataColumns = array();
$dataRows = array();

while($row = $query->fetchArray(SQLITE3_ASSOC))
{


   //add columns to array, checking if value exists
   foreach($row as $key => $value)
   {

       if(in_array(''.$key.'', $dataColumns)){

          //column already in array, dont add again.

       }else{

            //column not in array, add it.
            $dataColumns[]=array(
                'column'=>$key
            );  

        }

        //while in this foreach do I add the row values to an array or do it again outside this loop?

        //below does not work, only adds to the one array item and renders one HTML Table row with multiple SQL Table row values
        $dataRows[]=array(
            'row_item'=>$row[''.$row.'']
        );


    }

   }

   //build HTML table

   echo '<div class="table-responsive"><table class="table"><thead><tr>';

   //build columns from array... works
   foreach($dataColumns as $dataColumn){
        echo '<th>'.$dataColumn['column'].'</th>';
    }

    //close table column headers 7 start HTML table body...
    echo '</tr></thead><tbody>';

    //Issue is here, how do I get the each row (value is either null or not null) to

    echo '<tr>';
    foreach($dataRows as $dataRow){
        echo '<td>'.$dataRow['row_item'].'</td>';
    }
    echo '</tr>';


    //close table body & table...
    echo '</tbody></table></div>';


}else{

//table has no data
echo 'no data in the selected table';

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

https://stackoverflow.com/questions/40816809

复制
相关文章

相似问题

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