2012-06-14 128 views
0

試圖弄清楚如何正確執行此操作。從HTML表格類創建表格

的print_r的是這樣的:

Array ([0] => stdClass Object ([quote] => "Now that's wonderful!")) 

創建表:

$tmpl = array ('table_open' => '<table class="table" id="quotes-table">'); 
$this->table->set_template($tmpl); 
print_r($quotes); 
$data = array(
    array(form_checkbox('quotes'), 'Quote'), 
    array(form_checkbox('quotes'), 'Testing Quote Here'), 
    array(form_checkbox('quotes'), $quotes['quote']) 
); 
echo $this->table->generate($data); 
?> 

UPDATE:

問題,但我比1行添加更多的T它只返回一個行。所以基本上我想要一個可以從查詢返回的對象,以便在視圖中我可以簡單地執行$ quote-> quote。這樣它將顯示用戶的所有報價。

<?php 
$tmpl = array ('table_open' => '<table class="table" id="quotes-table">'); 
$this->table->set_template($tmpl); 
$checkbox_data = array(
    'name'  => 'quote', 
    'id'   => $quotes->id 
); 
$data = array(
    array(form_checkbox($checkbox_data), 'Quote'), 
    array(form_checkbox($checkbox_data), $quotes->quote) 
); 
echo $this->table->generate($data); 
+0

什麼你有問題嗎?錯誤? – animuson

+0

未定義的指數:沒有意義的報價 –

回答

1

在回答您的更新,嘗試這樣的事情:

<?php 

// Your query 
$results = $this->db->get('quotes')->result(); 

$this->table->set_heading('Name', 'Quote'); 

foreach ($results as $result) 
{ 
    // The name is the user's name, not sure if that's what you were going for. 
    $this->table->add_row($result->name, $result->quote); 
} 

echo $this->table->generate(); 

這一切都在這裏:http://codeigniter.com/user_guide/libraries/table.html

2

你沒有說是什麼問題,但如果你看不到自己的報價,嘗試改變這一點:

array(form_checkbox('quotes'), $quotes['quote']) 

進入這個:

array(form_checkbox('quotes'), $quotes[0]->quote) 

編輯: 一些額外的澄清。

Array ([0] => stdClass Object ([quote] => "Now that's wonderful!")) 

你有一個1的數組,這是一個對象。對於使用$ array ['key']作爲對象$ object-> key的數組。如果你不能在控制器中改變它,你可以做這樣的事情

$quotes = $quotes[0]; 
$quotes->quote; 
+0

我想知道如何將它變成$報價 - >報價。這意味着我必須在控制器中以不同的方式做到這一點? –

+0

如果您正在使用數據庫查詢並執行 - > result();最後,嘗試將其更改爲 - > row(); – Robert

+0

看看我的更新。 –