2011-03-26 36 views
0

所以,我一直在使用笨了一會兒,我從來沒有運行在我的模型查詢並返回到我的控制器,然後將其傳遞問題到我的視圖,其中我訪問它作爲一個對象,像這樣:笨2不從模型返回的對象只爲結果數組

MODEL(somemodel)

function getdata() 
{ 
    $query = $this->db->get('sometable'); 
    return $query; 
} 

控制器(somecontroller)

function controldata() 
{ 
    $this->load->model('somemodel'); 
    $data['dbdata'] = $this->somemodel->getdata(); 
    $this->load->view('someview',$data); 
} 

VIEW(someview)

<?php 
foreach($dbdata->result() as $row) { 
    echo $row->id; 
    echo $row->name; 
    echo $row->whatever; 
    echo "<br />"; 
} 
?> 

但現在無論什麼原因在2.0它使用$ dbdata-> result_array()作爲$行代替。

基本上它將結果作爲數組返回。

它沒有任何意義,已經在一些新版本的變化?

感謝,

因此,這裏是我的代碼:

MODEL(admin_model)

function show_allproducts($sort,$order,$limit,$offset) 
{ 
    $this->db->select('products.id,productcategories.categoryname,products.name,products.internetspecial,products.added,products.modified'); 
    $this->db->from('products'); 
    $this->db->join('productcategories','products.productcategories_id = productcategories.id'); 
    $this->db->order_by($sort,$order); 
    $this->db->limit($limit,$offset); 

    $query = $this->db->get();   

    if($query->num_rows() > 0) 
    { 
     return $query; 
    } 
    else 
    { 
     return FALSE; 
    } 
} 

控制器(管理員)

function show_allproducts() 
{ 
    $data['title'] = 'All Products'; 

    $this->load->library('pagination'); 
    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/admin/show_allproducts'; 
    $config['total_rows'] = $this->db->get('products')->num_rows(); 
    $config['per_page'] = 10; 
    $config['num_links'] = 5; 
    $config['full_tag_open'] = '<div class="paggination right">'; 
    $config['full_tag_close'] = '</div>'; 
    $config['prev_link'] = '&lt;&nbsp;prev'; 
    $config['next_link'] = 'next&nbsp;&gt;'; 
    $config['cur_tag_open'] = '<a class="active">'; 
    $config['cur_tag_close'] = '</a>'; 
    $this->pagination->initialize($config); 

    $this->load->model('admin_model'); 
    $data['tabledata'] = $this->admin_model->show_allproducts('name','ASC',$config['per_page'],$this->uri->segment(3)); 

    $data['nav'] = 'admin/pagesections/nav'; 
    $data['maincontent'] = 'admin/pages/show_allproducts'; 

    $this->load->view('admin/template',$data); 
} 

VIEW(show_all_products)

<table border="0" cellpadding="0" cellspacing="0" width="100%"> 
    <?php if($tabledata) { ?>    

     <tr> 
      <th>ID</th> 
      <th>CATEGORY</th> 
      <th>NAME</th> 
      <th>ADDED</th> 
     </tr> 

     <?php foreach($tabledata->result() as $row) { ?> 

      <tr> 
       <td><?php echo $row->id; ?></td> 
       <td><?php echo $row->categoryname; ?></td> 
       <td><?php echo $row->name; ?></td> 
       <td><?php echo $row->added;) ?></td> 
      </tr> 

     <?php } ?> 
    <?php } else { ?> 
     <tr> 
      <td>NO DATA</td> 
     </tr> 
    <?php } ?> 

</table> 

回答

2

好的人,沒有謊言,我發現這個該死的問題。

觀察第四TD其中$按行>補充;是在,它有一個接近尾音,它導致4天的混亂。

最親切的問候所有那些誰試圖幫助。有時候,我猜想有多少隻眼睛都不重要。

+0

也許你應該接受這個答案? :d – 2013-03-07 12:22:34

2

除非指定$ dbdata-> result_array()作爲$行,它不會返回結果爲數組。您發佈的相同代碼必須在CI 2.0中工作。

如果不是,可能是$ this-> db-> get('sometable')的問題。

試用$ this-> db-> query('select * from sometable')。但據我所知,都是一樣的...

+2

這是正確的。該代碼應該在CI2.0中工作,沒有問題。您需要使用result_array來獲取數組數組。否則,結果是一個對象數組。更多信息:http://codeigniter.com/user_guide/database/results.html – Rahul 2011-03-26 03:28:47

+0

你正在向合唱團傳道。我無法弄清楚爲什麼它的行事方式如此。如果使用$ dbdata-> result_array()作爲$ row,它就可以工作。如果我使用$ dbdata-> result作爲$ row,它不起作用。我將編輯上面的文本以包含實際的代碼。 – dv310p3r 2011-03-26 04:09:08

+0

查看我的代碼 – dv310p3r 2011-03-26 04:27:57

相關問題