将SQLite表数据导入HTML表

时间:2016-11-26 09:23:01

标签: php html sqlite html-table

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

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

问题是每个 sql行数据只呈现为一个html表行(带有当前代码)(应该是每行)问题是我如何将sql行数据转换成用于循环以呈现html表行的数组。

目前我有;

PHP将SQL列添加到HTML表格中SQl将数据行(foreach)放入HTML表行(foreach)

//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';

}

3 个答案:

答案 0 :(得分:1)

尝试替换

$dataRows[]=array(
    'row_item'=>$row[''.$row.'']
);

$dataRows[]=$row;

将此行放在while循环中的第一行(或者在您的列上的foreach循环外部的一般行),因为没有连接分析列的行。

然后,在输出foreach中,您应该找到从$dataRow内的数据库查询中选择的所有列的行(此处用column1,column2,...表示):

echo '<tr>';
foreach($dataRows as $dataRow){
    echo '<td>'.$dataRow['column1'].'</td>';
    echo '<td>'.$dataRow['column2'].'</td>';
    echo '<td>'.$dataRow['column3'].'</td>';
    echo '<td>'.$dataRow['column4'].'</td>';
    echo '<td>'.$dataRow['column5'].'</td>';
    echo '<td>'.$dataRow['column6'].'</td>';
    echo '<td>'.$dataRow['column7'].'</td>';
    // etc.
}
echo '</tr>';

毕竟你的代码看起来像这样(有点简化):

$query = $dbConnect->query("SELECT * FROM ".$table['name']." ORDER BY id DESC");

$dataColumns = array();
$dataRows = array();

while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
    $dataRows[] = $row;

    foreach ($row as $key => $value) {
        //Bilding $dataColumns, see Question
    }
}

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

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

echo '</tr></thead><tbody>';

echo '<tr>';
foreach ($dataRows as $dataRow) {
    foreach ($dataRow as $columnName => $columnValue) {
        echo '<td>'.$columnValue.'</td>';
    }
}
echo '</tr>';

echo '</tbody></table></div>';

答案 1 :(得分:1)

我重写了此代码以使其在一个循环中完成所有操作。

$firstRow = true;
echo '<div class="table-responsive"><table class="table">';
while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
    if ($firstRow) {
        echo '<thead><tr>';
        foreach ($row as $key => $value) {
            echo '<th>'.$key.'</th>';
        }
        echo '</tr></thead>';
        echo '<tbody>';
        $firstRow = false;
    }

    echo '<tr>';
    foreach ($row as $value) {
        echo '<td>'.$value.'</td>';
    }
    echo '</tr>';
}
echo '</tbody>';
echo '</table></div>';

您可能会更清楚地阅读?它还避免了在内存中构建所有dataRows的数组。

答案 2 :(得分:0)

感谢akrys

在while循环中,但在foreach列循环之外;

 //$dataRows = array();
 $dataRows[]=$row;

并构建HTML表格;

 foreach($dataRows as $dataRow){
    echo '<tr>';
    foreach($dataRow as $key => $value){echo '<td>'.$value.'</td>';}
    echo '</tr>';
}