【问题标题】:Make HTML table from array returned from execute_db in PHP - entries duplicated从 PHP 中的 execute_db 返回的数组制作 HTML 表 - 条目重复
【发布时间】:2020-07-18 05:52:29
【问题描述】:

我想从 SQL 查询的结果中填充一个 html 表,包含的字段将根据用户所做的选择而有所不同,因此我想根据查询结果中的内容自动设置标题等。我在https://inkplant.com/code/array-to-table 网站上找到了 array_to_table 函数,它可以执行我想要的操作,只是数据重复:-

预期结果

Formation       Play    
A               SW
S               DL

实际结果

Formation   0   Play    1
A           A   SW      SW
S           S   DL      DL 

我认为这可能与我正在创建的数组类型有关,但我不确定如何更改代码以生成我的预期结果。我不确定是否应该以不同的方式创建数组,或者是否(或如何)更改函数以使用我拥有的数组类型。

#SQL 创建示例表

CREATE TABLE `example` ( `id` INT NOT NULL AUTO_INCREMENT , `form` VARCHAR(10) NOT NULL , `playcall` VARCHAR(10) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;

#样本数据

INSERT INTO `example` (`id`, `form`, `playcall`) VALUES (NULL, 'A', 'SW'), (NULL, 'S', 'DL');

#mycode

$sql = "SELECT `form` as 'Formation',`playcall` as 'Playcall' FROM `example` ORDER BY `form` DESC";       
$res = execute_db($sql3 $conn);
   
$caption = "Example table";
     
echo array_to_table($res,$caption);

#函数

function array_to_table($data,$caption,$args=false) {
    if (!is_array($args)) { $args = array(); }
    foreach (array('class','column_widths','custom_headers','format_functions','nowrap_head','nowrap_body','capitalize_headers') as $key) {
        if (array_key_exists($key,$args)) { $$key = $args[$key]; } else { $$key = false; }
    }
    if ($class) { $class = ' class="'.$class.'"'; } else { $class = ''; }
    if (!is_array($column_widths)) { $column_widths = array(); }

    //get rid of headers row, if it exists (headers should exist as keys)
    if (array_key_exists('headers',$data)) { unset($data['headers']); }

    $t="<table style='width:40%'  class='w3-table w3-striped w3-bordered'>";
    $t .= $caption;
    
    $i = 0;
    foreach ($data as $row) {
        $i++;
        //display headers
        if ($i == 1) { 
            foreach ($row as $key => $value) {
                if (array_key_exists($key,$column_widths)) { $style = ' style="width:'.$column_widths[$key].'px;"'; } else { $style = ''; }
                $t .= '<col'.$style.' />';
            }
            $t .= '<thead><tr>';
            foreach ($row as $key => $value) {
                if (is_array($custom_headers) && array_key_exists($key,$custom_headers) && ($custom_headers[$key])) { $header = $custom_headers[$key]; }
                elseif ($capitalize_headers) { $header = ucwords($key); }
                else { $header = $key; }
                if ($nowrap_head) { $nowrap = ' nowrap'; } else { $nowrap = ''; }
                $t .= '<td'.$nowrap.'>'.$header.'</td>';
            }
            $t .= '</tr></thead>';
        }

        //display values
        if ($i == 1) { $t .= '<tbody>'; }
        $t .= '<tr>';
        foreach ($row as $key => $value) {
            if (is_array($format_functions) && array_key_exists($key,$format_functions) && ($format_functions[$key])) {
                $function = $format_functions[$key];
                if (!function_exists($function)) { custom_die('Data format function does not exist: '.htmlspecialchars($function)); }
                $value = $function($value);
            }
            if ($nowrap_body) { $nowrap = ' nowrap'; } else { $nowrap = ''; }
            $t .= '<td'.$nowrap.'>'.$value.'</td>';
        }
        $t .= '</tr>';
    }
    $t .= '</tbody>';
    $t .= '</table>';
    return $t;
}

编辑:根据https://stackoverflow.com/users/13851118/dhnwebpro 的提示,我转储了数组以查看其中包含的内容:-

数组

(
    [0] => Array
        (
            [Formation] => A
            [0] => A
            [Playcall] => SW
            [1] => Sw
        )

    [1] => Array
        (
            [Formation] => S
            [0] => S
            [Playcall] => DL
            [1] => DL
        )

)

EDIT2:我更改了查询的格式并使其正常工作:-

$res3 = $conn->query($sql3)->fetchAll(PDO::FETCH_ASSOC);

Fetch both确实是默认设置,我可以用上面的语句覆盖它。

【问题讨论】:

  • execute_db() 是否使用 PHP 数据对象? PDO 默认为PDO::FETCH_BOTH,看起来$res 的数组正在从PDO 获取关联值和索引值。您可能需要将其更改为 PDO::FETCH_ASSOCPDO::FETCH_NUM。没有看到来自execute_db 的代码,我不知道如何建议您这样做。
  • 是 PDO 是的,execute_db() 是由框架提供的,所以我需要去挖掘 - 感谢您的指点。
  • 你可以试试这个答案来删除数字键。如果它修复了它,那么你可以去挖掘框架。如果你在正确的树上吠叫,它至少会告诉你。 stackoverflow.com/questions/11042536/…
  • 我认为您应该先重新格式化数组,然后按照 dhnwebpro 的建议将其放入函数array_to_table
  • 嗯,要不要重写上面的函数?

标签: php html arrays


【解决方案1】:

正如@dhnwebpro 所说,PDO 默认为PDO::FETCH_BOTH,所以我通过将执行更改为:-

$res3 = $conn->query($sql3)->fetchAll(PDO::FETCH_ASSOC);

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2019-04-04
    • 2011-12-02
    • 2015-07-23
    • 1970-01-01
    相关资源
    最近更新 更多