我正在尝试在HTML表格中打印结果。请帮我解决。
<?php global $wpdb;
$result1=$wpdb->get_results("select name from table1");
$result2=$wpdb->get_results("select name from table2");
$result3=$wpdb->get_results("select name from table3");
$result_one = array();
$result_two = array();
$result_three = array();
//putting values in array
foreach ($result1 as $result) {
$result_one[] = $result
}
foreach ($result2 as $result) {
$result_two[] = $result
foreach ($result3 as $result) {
$result_three[] = $result
$results = array(
$result_one,
$result_two,
$result_three,
);
$array_length = count($result_one);
?>
接下来,我想在HTML表格中打印$results
。
实施例
------------------------------
Name1 | Name2 | Name3
------------------------------
Row1 Row1 Row1
Row2 Row2 Row2
Row3 Row3 Row3
Row4 Row4 Row4
Row5 Row5 Row5
感谢您的支持。
答案 0 :(得分:1)
<?php
$result1=array('ROW1','ROW1','ROW1');
$result2=array('ROW2','ROW2','ROW2');
$result3=array('ROW3','ROW3','ROW3');
$result_one = array();
$result_two = array();
$result_three = array();
//putting values in array
foreach ($result1 as $result) {
$result_one[] = $result;
}
foreach ($result2 as $result)
{
$result_two[] = $result;
}
foreach ($result3 as $result) {
$result_three[] = $result;
}
$results = array( $result_one, $result_two, $result_three);
?>
<table border=1>
<tr><td>Name1</td><td>Name2</td><td>Name3</td></tr>
<?php foreach($results as $row){ ?>
<tr>
<?php foreach($row as $val){?>
<td><?php echo $val;?> </td>
<?php } ?>
</tr>
<?php }?>
</table>
答案 1 :(得分:0)
你可以通过删除循环来简化代码,只需像这样创建数组:
$result1=$wpdb->get_results("select name from table1");
$result2=$wpdb->get_results("select name from table2");
$result3=$wpdb->get_results("select name from table3");
$results = array(NULL, $result1, $result2, $result3);
$results = call_user_func_array('array_map', $results);
$table = "<table cellpadding='0' cellspacing='0' border='0'>\n";
$table .= "\t<tr><td>Name1</td><td>Name2</td><td>Name3</td></tr>\n";
foreach ($results as $key => $value){
$table .= "\t<tr>\n\t\t";
foreach ($value as $key2 => $value2){
$table .= "<td>".$value2."</td>";
}
$table .= "\n\t</tr>\n";
}
$table .= "</table>\n";
echo $table;