2015-06-20 58 views
0

控制器[在文章頁面文章正確使用分頁,將'用戶電子郵件ID'存儲在'文章'數據庫中,現在我試圖從用戶表中獲取用戶名和姓,但無法正常工作]通過在codeigniter電子郵件ID獲取數據庫

public function articles() 
    {  
    $data['title'] = "Articles"; 

    $config = array(); 
    $config["base_url"] = base_url() . "sd/articles/"; 
    $config["total_rows"] = $this->model_users->record_count_articles(); 
    $config["per_page"] = 10; 
    $config["uri_segment"] = 3; 


    $this->pagination->initialize($config); 

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 
    $data["results"] = $this->model_users->fetch_result_articles($config["per_page"], $page); 
    $data["links"] = $this->pagination->create_links(); 


    if ($this->session->userdata ('is_logged_in')){ 
     $data['profile']=$this->model_users->profilefetch(); 
     $this->load->view('sd/header',$data); 
     $this->load->view('sd/articles', $data); 
     $this->load->view('sd/footer', $data); 
    } else { 
     $this->load->view('sd/sdheader', $data); 
     $this->load->view('sd/articles', $data); 
     $this->load->view('sd/sdfooter', $data); 
    } 
} 

模型[獲取用戶名在文章頁面]

 public function record_count_articles() { 
      return $this->db->where('status','1')->count_all("articles"); 
     } 

    public function fetch_result_articles($limit, $start) { 
     $this->db->limit($limit, $start); 
     $query = $this->db->where('status','1')->order_by('id', 'DESC')->get("articles"); 

     if ($query->num_rows() > 0) { 
      foreach ($query->result() as $row) { 
       $data[] = $row; 

      } 
      return $data; 
     } 
     return false; 
    } 

添加這些行[但不起作用]

if ($query->num_rows() > 0) { 
      foreach ($query->result() as $row) { 
       $data[] = $row; 
       $query = $this->db->select('firstname')->select('lastname')->where('email',$data[0]->email)->get("users"); 
       $data['name_info']=$query->result_array(); 
      } 
      return $data; 
     } 
     return false; 
+0

改變這個$ row-> email而不是數據[0] - >電子郵件,也評論這一行// $ data [] = $ row; –

+0

只有第一個文章名稱得到,名稱數組不重複 –

回答

0

這裏有2個問題。請看代碼中的評論。

if ($query->num_rows() > 0) { 
      foreach ($query->result() as $row) { 
       //1) $data[0]->email keep repeating same email. 
       // inner $query variable should be different. 
       $innerQuery = $this->db->select('firstname,lastname')->where('email',$row->email)->get("users"); 
       //2) you need to store query result on array. 
       // $data['name_info'] stores only single record. 
       $data[]=$innerQuery ->result_array(); 
      } 
      return $data; 
     } 
     return false; 

應避免在循環查詢,如果你能實現它通過加入

編輯:讓我們試試這個與加盟

public function fetch_result_articles($limit, $start) { 
     $this->db->limit($limit, $start); 
     $query = $this->db 
     ->join('users u','u.email = a.email','left') 
     ->where('a.status','1')->order_by('a.id', 'DESC')->get("articles a"); 

     if ($query->num_rows() > 0) { 
      foreach ($query->result() as $row) { 
       $data[] = $row; 

      } 
      return $data; 
     } 
     return false; 
    } 

我沒有測試的代碼。但它比循環更好。

+0

您的代碼是幫助完整數組正確重複,但問題是我需要文章數據如此生病添加這些行,「」「$ data [] = $ row;」「」 ,我的第一篇文章在[0]中打印,名稱在[1]中打印,第二篇文章在[2]中打印,姓名打印在[3]中 –

+0

您可以告訴我外層'$ query'是如何寫的?而不是'$ innerQuery' – jagad89

+0

謝謝男人,'加入'的方式是完美的工作,謝謝:) –

相關問題