-1

こんにちは、ワードプレスでphpスクリプトを作成して、データベースからデータを取得する動的なhtmlテーブルを生成しています。

関数は次のようなものです。

1) テーマ 2) トピックス 3) 項目

[テーマ] ごとに [トピック] があり、[トピック] ごとに複数の [項目] があります

各 [トピック] を新しい行として表示し、その下に [項目] を表示したい

これが私のコードを修正するのに役立つ機能していない私の機能です。

function dynamic_table($attr) {

global $wpdb;

$ThemeCode = $attr['theme'];
$Themes = getThemeinfo($ThemeCode);
$sr = 1;

$dt_list = $wpdb->get_results("SELECT * FROM wp_mtresources WHERE ThemeCode= '$ThemeCode'");
if(!empty($dt_list)) {
    echo '<h2>' . $Themes['Desc'] . '</h2>
        <table style="margin: auto; margin-bottom: 30px;" border="0" cellspacing="0" cellpadding="0">
        <tbody>
            <tr>
                <th>Topic</th>
                <th>Presenation</th>
                <th>Worksheets</th>
                <th>Other Resources</th>
            </tr>
            ';
    foreach ($dt_list as $dt) {
        $tp_list = $wpdb->get_results("SELECT * FROM wp_mtresources WHERE TopicCode= '$dt->TopicCode'");

        $Topics = getTopicsinfo($dt->ThemeCode, $dt->TopicCode);    
            echo '  
                <tr>
                    <td colspan="4" style="text-align:center;"><a href=""> ' . $Topics['TopicDesc'] . ' </a></td>
                </tr>';
            foreach ($tp_list as $tp) {

                echo '
                    <tr>
                        <td>' . $tp->ResourceLocation.'</td>    
                        <td></td>   
                        <td></td>   
                        <td></td>   
                    </tr>       
                ';

            }
    }   
        echo '</tbody>
              </table>';    
} else {
    return 'No Theme or Topic found.';
}   


 }
 add_shortcode('dt', 'dynamic_table');

そして、ここに予期しない結果があります スクリーンショット

4

2 に答える 2

2

まず、データベースから選択します

PDO example..
while($rows = $query->fetch(PDO:FETCH_ASSOC)){
  $data = $rows;
}

これで、テーブル名をキーとしてすべてが $data 配列に含まれます。

他の配列に他のデータを保持する

$another_array = ['example', 'example']; <- 単なる例として、上記のような別のデータベース検索が考えられます。別のデータ配列が何であれ!

次に、ループごとに配列の1つをループしますが、そのループ内の他の配列にアクセスします..

$array1 = ['one', 'two', 'three'];
$array2 = ['a', 'b', 'c'];

foreach($array1 as $k => $v){
     echo $v.' - '.$array2[$k];
}

結果は次のようになります。

1 - a 2 - b 3 - c

これが十分に明確でない場合は申し訳ありません!

于 2012-10-24T11:18:37.117 に答える