如何将foreach结果以表格格式

时间:2016-07-18 05:55:45

标签: php codeigniter

我希望以表格格式获取foreach的结果,并将整个表格作为变量。 这是我的模特:

        public function cron_job_seller(){
        $this->db->select('*');
        $this->db->from('wc_seller_products');
        $query = $this->db->get();
        $result = array();
        foreach ($query->result() as $row){
            $result[] = $row;
         }
         return $result; 
      }

我的控制器是

public function cron_job(){
$this->load->model('home/Home_model');
$buyer = $this->Home_model->cron_job_buyer();
$this->load->library('email', array('mailtype'=>'html'));
$seller = $this->Home_model->cron_job_seller();


echo "<table>";
foreach($seller as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
    echo "<td>" . $row2 . "</td>"; 
}
echo "</tr>";
}
 echo "</table>";

以表格格式

给出这样的o / p
 19 102 Rolex   65  Good    0000-00-00  fh  ghf fgh ghf ghf gfh ghf ghf 56  56  download14.jpg  11/6/2016 19:03 2016-07-15 12:13:35 1   0

当我打印$ seller变量时,它会给出

Array ( [0] => stdClass Object ( [id] => 19 [seller_id] => 102 [brand_name] => Rolex [model_no] => 65 [condition] => Good [date_purchase] => 0000-00-00 [case_size] => fh [case_shape] => ghf [case_material] => fgh [strap_type] => ghf [dial_colour] => ghf [water_resistance] => gfh [local_overseas] => ghf [warranty_period] => ghf [min_price] => 56 [sale_price] => 56 [photo] => download14.jpg [date] => 11/6/2016 19:03 [time] => 2016-07-15 12:13:35 [status] => 1 [login] => 0 [verified] => )

现在我想要两件事: 1.整个表在一个变量中。 2.数组键,如id,seller_id,brand_name作为表标题

我真的很困惑现在要做什么,怎么做......请帮忙

5 个答案:

答案 0 :(得分:1)

public function cron_job() {
    $this->load->model('home/Home_model');
    $buyer = $this->Home_model->cron_job_buyer();
    $this->load->library('email', array('mailtype'=>'html'));
    $seller = $this->Home_model->cron_job_seller();

    $theader = '';
    $tbody = "<tbody>";
    foreach($seller as $key => $row) {
        $tbody .= "<tr>";
        foreach($row as $key2 => $row2){
            if (!$theader) {
                $theader = array_keys($row2);
            }
            $tbody .= "<td>" . $row2 . "</td>"; 
        }
        $tbody .=  "</tr>";
    }

    $tbody .= "</tbody>";
    if (!empty($theader)) {
        $theader = '<thead><th>' . implode('</th><th>', $theader) . '</th></thead>';
    }
    $table = '<table>'.$theader.$tbody.'</table>';
}

答案 1 :(得分:0)

你不需要担心它......这可能是一个解决方案。只需在您的控制器中执行此操作:

$seller = $this->Home_model->cron_job_seller();
$table = "<table border='1'>";
$i=1;
foreach($seller as $key=>$row) { //you don't need to loop twice
     if($i == 1){
         $table.="<tr>";
         $table .= "<th>".$key."</th>";
         $table.="</tr>";
         $i=0; //change value of $i so for next iteration the header will not print
       }
     $table .= "<tr>";
     $table .= "<td>" . $row . "</td>"; 
     $table.= "</tr>";
}
$table .= "</table>";

您还需要从模型中返回$query->result()。 现在尝试打印$table 使用echo $table;它会根据需要打印结果,现在它也可以设置在一个变量中,您可以设置为$data['table'] = $table并在需要时将其结束以查看。

答案 2 :(得分:0)

你做得对,但需要一些修改 您需要做的是在第二个foreach()循环中,您需要将对象转换为数组并取出变量中的数组键array_keys($array)并将此键打印到变量。如下图所示。

$table = "<table border='1'>";
foreach($seller as $key=>$row) { //you don't need to loop twice
    $table .="<tr>";
    // creating a table heading 
    if($key === 0 ){
        $cols = array_keys((array)$row); // Convert the object to array and take out all keys of array and assign to $cols variable
        $table .="<th>".implode("</th><th>", $cols); 
        $table .="</th></tr><tr>";
    }
    // table heading end

    $table .= '<td>'. implode('</td><td>', (array)$row). '</td>';
    $table .= "</tr>";
}
$table .="</table>";
echo $table;

答案 3 :(得分:0)

您可以直接遍历数据库中的数据数组,因为每行都是标准PHP对象(包含列名作为键)的数组,然后您可以从第一行中提取列名,然后使用它构建表头。之后,您可以继续构建包含所需实际数据的行。下面的代码说明了如何:

    <?php

        function cron_job() {
            $strOutput = "<table class='cron-tbl'>";
            $this->load->model('home/Home_model');
            $buyer      = $this->Home_model->cron_job_buyer();
            $this->load->library('email', array('mailtype' => 'html'));
            $seller     = $this->Home_model->cron_job_seller();

            $cue        = 0;
            foreach ($seller as $key => $row) {
                if($cue == 0){
                    // CREATE THE HEADER ROW:
                    $strOutput .= "<tr class='cron-head-row'>" . PHP_EOL;
                    foreach($row as $rowName=>$rowVal){
                        $strOutput .= "<th class='cron-head-cell'>{$rowName}</th>" . PHP_EOL;
                    }
                    $strOutput .= "</tr>" . PHP_EOL;
                    $strOutput .= "<tbody class='cron-body'>" . PHP_EOL;
                }

                // CREATE THE TABLE-BODY CELL
                  $strOutput .= "</tr>" . PHP_EOL;
                foreach ($row as $rowKey => $data) {
                    $strOutput .= "<td class='cron-data-cell'>{$data}</td>" . PHP_EOL;
                }
                $strOutput .= "</tr>" . PHP_EOL;
                $cue++;
            }
            $strOutput .= "</tbody>"  . PHP_EOL;
            $strOutput .=  "</table>" . PHP_EOL;;

            return $strOutput;
        }

        echo (cron_job());

干杯&amp;祝你好运......

自己测试HERE

答案 4 :(得分:0)

试试这种方式。
我猜html标签,身体标签都缺失了。这就是为什么没有按照你想要的方式显示表格的原因。

我的建议是,对于html部分,如果放入视图会更好。
我已经使用bootstrap创建了视图页面cron_job_seller.php

// Model - Change to this
public function cron_job_seller() 
{
    $this->db->select('*');
    $this->db->from('wc_seller_products');

    return $this->db->get();    
}

// Controller
public function cron_job() {

    $this->load->model('home/Home_model');
    $this->load->library('email', array('mailtype'=>'html'));

    $buyer = $this->Home_model->cron_job_buyer();
    $seller = $this->Home_model->cron_job_seller();

    $data['seller'] = $seller // Send data to View

    // Create cron_job_seller.php file in Views Folder.
    $this->load->view('cron_job_seller', $data); 
}

// View cron_job_seller.php 
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Sample Table</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>

    <div class="container">
        <table class="table table-bordered">
            <?php foreach($seller->result() as $row) { ?>
                <tr>
                    <td><?php echo $row->colname_1; ?></td>
                    <td><?php echo $row->colname_2; ?></td>
                    <td><?php echo $row->colname_3; ?></td>
                    <td><?php echo $row->colname_4; ?></td>
                </tr>
            <?php } ?>
        </table>
    </div>

</body>
</html>