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如表標題
我真的糊塗了現在做什麼和怎麼做......請幫助
您是否嘗試給邊界等表格,'echo「
回答
你不必擔心太多。這可能是一個解決方案。只要做到像這樣在你的控制器:
,你也需要從你的模型返回
$query->result()
。 現在嘗試打印$table
使用echo $table;
它會打印結果,如果你想,現在它也被設置在一個單一的變量,所以你可以,你設置爲$data['table'] = $table
並結束它來查看,如果你需要。來源
2016-07-18 06:04:14
謝謝!但如果我還希望表標題如id,seller_id,brand_name如上所示 – Pardeep
返回'$ query-> result();'從模型中,你不需要循環那裏 –
及以上,你可以看到編輯後的頭 –
來源
2016-07-18 06:18:58 Joga
你是做正確的,但需要一些修改 你需要做的是在你第二foreach()循環,你需要將對象轉換成數組,並採取了數組鍵
array_keys($array)
在一個變量和打印此鍵變量。如下所示。來源
2016-07-18 06:37:25
你可以通過數據從數據庫,並因爲每一行的數組循環是標準的PHP對象的數組(其中包含列名鍵),然後你可以提取從第一列名行,然後用它來構造表頭。之後,您可以繼續構建包含所需實際數據的行。下面的代碼演示瞭如何:
乾杯&好運...
測試一下自己HERE。
來源
2016-07-18 06:40:14 Poiz
試試這種方法。
我想HTML標籤,身體標籤丟失。這就是爲什麼沒有按照你想要的方式顯示錶格的原因。
我的建議是,對於html部分,最好是放在視圖中。
我使用引導創建了查看頁面
cron_job_seller.php
。來源
2016-07-18 09:39:46
相關問題