簡體   English   中英

如何制作用戶友好的列標題

[英]How to make user friendly column headings

我正在遍歷 wordpress 數據庫表,它根據選定的ID輸出所有表列和數據。 它按預期工作,但缺點是它在前端顯示了一個不太用戶友好的列標題。 我想將表格列標題更改為對用戶更友好的內容。

我曾考慮在查詢中使用別名,但我無法找到一種方法來使其與變量一起工作。 另一個想法是制作一個單獨的表,該表具有相同的列名,其中 1 行數據包含對用戶更友好的列名,並在創建 html 時嘗試將它們換掉。

但我不確定如何讓它在循環內工作。 這會起作用,還是有更好的方法來制作更用戶友好的列標題? 由於列的數量將來會增加,我無法在 php 中對列名進行硬編碼,這是我目前所擁有的:

$myquery = $mydb->get_results($mydb->prepare( "SELECT * FROM {$mydb->prefix}`mytable` WHERE id=%d", $myChosenID));

$output = "<div>";
    foreach($myquery as $key => $var) {
        if($key==0) {

            foreach($var as $col => $val) {
                $output .= '<div>';
                $output .= "<div>" . $col . '</div>';
                $output .= '<div><input name="' . $col . '" type="text" value="' . $val . '"></div>';
                $output .= '</div>';
            }
        }
        else {
            // output different html
        }
    }
    $output .= '</div>'; 
    echo $output;

任何為我指明正確方向的幫助表示贊賞。

最適合我的解決方案是使用具有相同列名的不同表和包含新名稱的行,然后在 for each 循環中獲取新名稱。

$my_table = $mydb->prefix . '`my-table`';
$myquery = $mydb->get_results($mydb->prepare( "SELECT * FROM {$my_table} WHERE id=%d", $myChosenID));

$output = "<div>";
    foreach($myquery as $key => $var) {
        if($key==0) {

            foreach($var as $col => $val) {
                $table_name = $mydb->prefix . '`table-headers`';
                $field_name = $col;
                $getNewNames = $mydb->get_var($mydb->prepare( "SELECT {$field_name} FROM {$table_name} WHERE id=%d", 0));
                $output .= '<div>';
                $output .= "<div>" . $getNewNames . '</div>';
                $output .= '<div><input name="' . $col . '" type="text" value="' . esc_attr( $val ). '"></div>';
                $output .= '</div>';
            }
        }
        else {
            // output different html
        }
    }
    $output .= '</div>'; 
    echo $output;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM