0

I am trying to print results in HTML table. Please help me to resolve.

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

Next i want to print $results in HTML table.

Example

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

Thanks for support.

2 Answers 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>
Sign up to request clarification or add additional context in comments.

Comments

0

You can simplify your code a lot by removing the loops and just creating your array like this:

$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;

6 Comments

Thanks,how can i print this in html table?
@Hansa - Do you have data in your database?
Yes,i am trying something like this @ pastebin.com/RRMMvabt But not able to get result help me.
@Hansa - The code you have at pastebin doesn't look anything like what I posted. And how are you going to do a query in pastebin with: $result1=$wpdb->get_results("select name from table1");? You're not even trying to make this work.
All 3 results i took in $results 3 dimensional array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.