2016-07-18 56 views
0

我想將表格格式的foreach結果和變量中的整個表格結合起來。 這裏是我的模型:如何將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 

當我打印$賣家變量它給

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] =>) 

現在我想在這2個東西: 1.整個表在一個單一的變量。 2.陣列鍵,如ID,seller_id,BRAND_NAME如表標題

我真的糊塗了現在做什麼和怎麼做......請幫助

+0

您是否嘗試給邊界等表格,'echo「

」;'? – C2486

回答

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並結束它來查看,如果你需要。

+0

謝謝!但如果我還希望表標題如id,seller_id,brand_name如上所示 – Pardeep

+0

返回'$ query-> result();'從模型中,你不需要循環那裏 –

+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>'; 
} 
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; 
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()); 

乾杯&好運...

測試一下自己HERE

0

試試這種方法。
我想HTML標籤,身體標籤丟失。這就是爲什麼沒有按照你想要的方式顯示錶格的原因。

我的建議是,對於html部分,最好是放在視圖中。
我使用引導創建了查看頁面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>