【问题标题】:Display SQLITE output in column rather than row在列而不是行中显示 SQLITE 输出
【发布时间】:2014-05-20 18:03:49
【问题描述】:

目前我有以下脚本,它通过查看 sqlite 表自动生成表名和行数据。因此,无论您有 2 列还是 10 列,此脚本都有效。

目前脚本输出结果如下:

输出当前显示为行

我已尝试更改脚本,使其输出如下所示的结果。有人可以帮助或指导我朝着正确的方向实现这一目标吗?

是否可以按以下格式输出查询结果:在列中向下而不是在行中?

输出应显示为列

<?
    $ED = $_GET['ED'];
    $ID = $_GET['ID'];
    $table_name = $_GET['table'];
?>
<table border="1">
  <tr>
    <td>
    
    <table>
    <?php // Display all sqlite column names for chosen table

    $tablesquery = $db->query("PRAGMA table_info($table_name)");

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {

if ($table['name'] == "ID") {

        echo "<tr><td>" . $table['name'] . "</td></tr>";
    
} else {
    
        $table_name_header = ucwords(strtolower(str_replace('_', ' ', $table['name'])));
        echo "<tr><td>" . $table_name_header . "</td></tr>";
        } 

        
}
?>

    </table>
    
    </td>
    <td>
<table>
<?

    
// Display all sqlite data for chosen table

    $tablesquery = $db->query("PRAGMA table_info($table_name)");

    $columns = array();

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
        $columns[] = $table['name'];
    }

    // Display * from USERS

    
//    $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
    $results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
    while ($row = $results->fetchArray()) {
//          echo "<tr>";
            $test = $row[0];
        foreach ($columns as $col) 

            echo "<tr><td>" . $row[$col] . "</td></tr>";
            
    }
//          echo "</tr>";
?>
</table>
    
    </td>
  </tr>
</table>

【问题讨论】:

  • 先手动构建html。这将告诉您如何构建 PHP 以生成该 HTML。
  • 您的代码也容易受到 SQL 注入的影响。并且您不应该在 Stackoverflow 上使用简短的开放标签作为示例代码。而且您不应该发布实时代码示例,而是从头开始重新创建一个(较小的)示例来演示您的问题并且是自包含的(例如,它很可能不是因为数据库,而只是因为循环,所以一个示例可以包含显示问题的数据的工作)。
  • 提示:您需要先获取所有数据并将其临时存储到变量中,以从行(如在数据库中)更改为列(如在 HTML 中)。

标签: php sql sqlite


【解决方案1】:

修改代码如下,将数据放入组合数组,然后通过循环将其拉回,它将根据需要显示:

<?


// Display all sqlite data for chosen table

    $tablesquery = $db->query("PRAGMA table_info($table_name)");

    $columns = array();

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
        $columns[] = $table['name'];

    }

    // Display * from USERS


//    $results = $db->query('SELECT * FROM ADMIN_LOGIN WHERE ID = "57"');
    $results = $db->query('SELECT * FROM ' . $table_name . ' WHERE ID = "' . $ID . '"');
    while ($row = $results->fetchArray()) {
//          echo "<tr>";
            $test = $row[0];
        foreach ($columns as $col) 
        $column_data[] = $row[$col];

//            echo "<tr><td>" . $row[$col] . "</td></tr>";

    }
//          echo "</tr>";
?>

<?
$array = $columns;
$array2 = $column_data;

$result = array_combine($array, $array2);
//print_r($result);
?><br><br>
<center>
<table border="0" cellpadding="2" cellspacing="2" color="#4B708D">
<thead>
<?
foreach($result as $key => $value) {
  echo "<tr><td bgcolor='#c6d5e1'>$key</td><td bgcolor='#FFFFFF'>$value</td></tr>";
}
?>
</thead>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 2023-01-31
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多