简体   繁体   中英

php multiple foreach loop

Hello i am creating a php script in wordpress to generate a dynamic html table which data is fetch from database,

The function is something like this:

1) Theme 2) Topics 3) items

For each [theme] there are [topics] and for each [topics] there are multiple [items]

I want to display each [topic] As a new row and under which [items] will be displayed

here is my function which is not working help me to correct my code.

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');

And here is unexpected result Screenshot

Firstly I would select from your database

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

Now everything is in the $data array with your table names as key.

hold any other data in other arrays

$another_array = ['example', 'example']; <- just an example could be another database retrieval like above, whatever just another array of data!

Then for each loop the one of the arrays but access other arrays within that loop..

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

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

Result will look like..

one - a two - b three - c

Sorry if this is not clear enough!

in one of my instance i did like this

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM