dongwu5743 2012-06-16 00:08
浏览 14
已采纳

从查询中返回NULL以获取数据表

I'm wondering what would be best to do. Right now I have running a query to see if I have any results returned and if none are returned I return NULL.

On my controller I send that resultset whether it be an object or NULL to my table and it echos the rows on the view page.

For my tables I am using the jquery datatables plugin. I'm trying to figure out how I can have it handle the data when the sent value is NULL that way it doesn't show me an error when it hits my foreach loop.

Controller:

$news_articles = $this->news_model->get_news_articles();
$this->data['news_articles'] = $news_articles;

Model:

/**
 * Get news articles
 *
 * @return  object
 */
function get_news_articles()
{   
    $query = $this->db->get($this->news_articles_table);
    if ($query->num_rows() > 0) return $query->result();
    return NULL;
}

View:

$tmpl = array ( 'table_open'  => '<table class="table" id="newsarticles-table">' );
$data = array('name' => 'newsarticles', 'class' => 'selectall');
$this->table->set_heading(form_checkbox($data), 'ID', 'Date', 'Title');            
$this->table->set_template($tmpl);             
foreach ($news_articles as $row)
{
    $checkbox_data = array(
        'name'        => 'newsarticles',
        'id'          => $row->id
    );
    $this->table->add_row(form_checkbox($checkbox_data), $row->id, $row->date_posted, $row->article_title);
}
echo $this->table->generate(); 
  • 写回答

3条回答 默认 最新

  • duanci19881003 2012-06-16 00:18
    关注

    Just return an empty array from the model, if there are not results. That way your foreach won't break. It just won't loop over anything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料