0

我正在尝试在 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);
   ?>

接下来我想$results在 HTML 表格中打印。

例子

------------------------------
   Name1  |  Name2   |  Name3
------------------------------
   Row1      Row1       Row1       
   Row2      Row2       Row2
   Row3      Row3       Row3
   Row4      Row4       Row4
   Row5      Row5       Row5

谢谢你的支持。

4

2 回答 2

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>
于 2013-08-13T05:23:19.050 回答
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;
于 2013-08-13T05:08:07.220 回答