【发布时间】:2016-11-26 09:23:00
【问题描述】:
我有一个 SQLite 数据库并使用 PHP 来提供数据并与之交互。
我正在尝试为每个 SQLite 表列构建一个 HTML 表作为 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';
}
【问题讨论】:
-
问题和问题。问题是每个 sql 行数据只呈现到一个 html 表行(使用当前代码)(应该是每一行)问题是我如何将 sql 行数据放入数组中以循环呈现 html 表行。
-
您的 while 循环 (
while($row = $query->fetchArray(SQLITE3_ASSOC))) 应该在注释“//build HTML table”之前结束
标签: php html sqlite html-table