繁体   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