【问题标题】:Get SQLite Table Data into HTML Table将 SQLite 表数据获取到 HTML 表中
【发布时间】: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-&gt;fetchArray(SQLITE3_ASSOC))) 应该在注释“//build HTML table”之前结束

标签: php html sqlite html-table


【解决方案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>';

您可能会发现阅读起来更清晰?它还避免了在内存中构建所有数据行的数组。

【讨论】:

    【解决方案2】:

    尝试替换

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

    【讨论】:

    • 基本上有效,但只显示一行,即使我知道表中有多行。还想在不使用(假设)的情况下回显每一列的行值我知道列名 $dataRow['the column name'] ...
    • 你需要第二次 foreach:foreach($dataRow as $key =&gt; $value){echo '&lt;td&gt;'.$value.'&lt;/td&gt;';}
    • 删除了 $dataRows = array();允许我获取每一行数据,但仍然需要在不知道 $dataRow['column1'] 中的列名的情况下获取行列值;
    • 嗯,这没有意义。现在您应该得到通知,在尝试添加数据库行时未定义 $dataRows。您是否将 $dataRows[]=$row; 放在了 while 循环的开头?
    【解决方案3】:

    感谢 akrys

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

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

    并在 HTML Table 的构建中;

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

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 2012-05-17
      • 2017-06-10
      • 2018-06-05
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      相关资源
      最近更新 更多