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
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..
Result will look like..
Sorry if this is not clear enough!