2011-11-04 99 views
1

我即将实施解析器类到我的codeigniter项目,并希望将数据从模型传递到解析器数组。哪种做法更好,效率更高。Codeigniter将数据从模型传递到控制器分析程序

我的模型获取数据并将其返回给控制器。我怎样才能把它放入控制器的数组中?

型号:

function getSeo($data){ 

    $this->db->select('seo_title, seo_description, seo_keywords'); 
    $this->db->from('content'); 
    $this->db->where($data); 

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

    $data = array(); 

foreach ($query->result() as $row) { 
    $data[] = array(
     'seo_title' => $row->seo_title, 
     'seo_description' => $row->seo_description, 
      'seo_keywords' => $row->seo_keywords 
    ); 
} 

return $data; 

} 

控制器:

 $viewdata['pageseo'] = $this->Content_model->getSeo($data); 

$viewdata = array(
    'seo_title' => 'My seo Title', 
    'seo_description' => 'My seo description', 
    'seo_keywords' => 'My seo keywords', 
    ); 

什么是从我的模型中的数据为 '$可视数据' 数组的最佳方式,它怎么办????

回答

1

由于模型中的getSeo函数返回一个数组,控制器也会将这些信息存储到$ viewdata数组中。

如果您尝试print_r($viewdata),您会看到该结构与预期相符。这是$viewdata['seo_title'] => 'My seo Title'

等等...

1

有一个叫result_array()功能,用于获取结果集为阵列和下面的链接可以帮助你。这是核心库功能。

PLZ参阅,

http://codeigniter.com/user_guide/database/results.html 
0

使用模型动态视图

functiongetData($alias='*',$table,$where='',$join='',$orderBy='',$whereOr='',$groupby='',$limit='') 
{ 

    $this->db->select($alias); 
    $this->db->from($table); 
    if($join!='') 
    { 
     for($i=0;$i<count($join);$i++) 
     { 
      $this->db->join($join[$i]['tableName'],$join[$i]['joinCondition'],$join[$i]['joinType']); 
     } 
    } 

    if($where!='') 
    { 
     foreach($where as $coulmn=>$value){ 
      $this->db->where($coulmn,$value); 
     } 
    } 

    if($whereOr!='') 
    { 
     //print_r($whereOr); 
     foreach($whereOr as $coulmn=>$value){ 
      $this->db->or_where($coulmn,$value); 
     } 
    } 

    if($orderBy!='') 
    { 
     $orderByKey=array_keys($orderBy); 
     $orderByMode=array_values($orderBy); 
     $this->db->order_by($orderByKey[0],$orderByMode[0]); 
    } 

    if($groupby != '') 
    { 
     $this->db->group_by($groupby); 
    } 

if($limit!='') 
    { 
     $limit=explode(',', $limit); 
     if(count($limit)>=2) 
     { 
      $this->db->limit($limit[1],$limit[0]); 
     }else { 
      $this->db->limit($limit); 
     } 
    } 

    $query=$this->db->get(); 
    $result=$query->result(); 
    //echo $this->db->last_query(). "<br>"; 
    return $result; 
} 

To know more click here

获取数据
相关问题