簡體   English   中英

PHP的多個foreach循環

[英]php multiple foreach loop

您好,我正在wordpress中創建一個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');

這是意外的結果屏幕截圖

首先,我將從您的數據庫中選擇

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

現在,所有內容都在$ data數組中,其中表名為鍵。

保存其他數組中的任何其他數據

$another_array = ['example', 'example']; <-只是一個例子,可以是如上所述的另一次數據庫檢索,而不僅僅是另一組數據!

然后,對於每個循環,使用其中一個陣列,但訪問該循環中的其他陣列。

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

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

結果看起來像..

一-一二-b三-c

對不起,如果還不夠清楚!

在我的一個例子中,我確實是這樣

$i=0;
$count=10;
echo count($_POST['dates']);echo'<br>';
while($i<10 )
{
$date = ($_POST['dates'][$i]) ;
$fee = ($_POST['fee'][$i]);
echo "The value of  is".$date .'-------'.$fee;
echo'<br>';
$i=$i+1;
} 

and it gave answer like 
The value of is2017-02-28-------800
The value of is2017-02-21-------1000
The value of is2017-02-13-------200

暫無
暫無

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

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