如何将SQLite表数据放入HTML表?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (25)

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

我试图为每个SQLite表列构建一个HTML表,然后为SQLite表中的每一行生成一个HTML表,并将其呈现为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';

}
提问于
用户回答回答于

我重写了这个来完成这样的一个循环:

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

你可能会觉得读得更清楚?它还避免构建内存中所有数据Row的数组。

用户回答回答于

试着取代

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

带着

$dataRows[]=$row;

将这一行放在while循环中的第一行

你应该找到从数据库查询中选择的所有列的行:

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

扫码关注云+社区

领取腾讯云代金券