2013-11-24 47 views
1

我有一個名爲表uploaded_files在我的數據庫,在那裏我有以下字段:活動記錄問題返回陣列(笨)

ID,FILE_NAME,upload_id,filetype_id

這最後一個字段(filetype_id)是一個外鍵的表稱爲文件類型在那裏我有這些字段:

ID,filetype_desc

我想在我的模型來創建一個方法返回的所有字段表uploaded_files,陣列中的以下這樣的結構:

array(
      'file_type_1' => array(
       'id'   => 1, 
       'file_name' => "lalala.txt", 
       'upload_id'  => $uploadId, 
       'filetype_id' => 1 
      ), 
      'file_type_2' => array(
       'id'   => 1, 
       'file_name' => "lelele.txt", 
       'upload_id'  => $uploadId, 
       'filetype_id' => 2 
      ), 
      'file_type_3' => array(
       'id'   => 1, 
       'file_name' => "blabla.txt", 
       'upload_id'  => $uploadId, 
       'filetype_id' => 3 
      ), 
    ); 
} 

到現在爲止,我來到這個:

public function get_UploadedFilesFromUploadId($uploadId){ 
     //$query = "select * from uploaded_files where upload_id =".$uploadId; 
     $query = $this->db->get_where('uploaded_files', array(
                  'upload_id' => $uploadId 
                 ) 
            ); 
     return $this->db->query($query); 
} 
+0

什麼似乎是問題?你有什麼失敗的嘗試? – DevlshOne

回答

1
$query = $this->db->get_where('uploaded_files', array(
                  'upload_id' => $uploadId 
                 ) 
            ); 
$res = $this->db->query($query); 

$result = array(); 
foreach ($res->result_array() as $row) 
{ 
    $key = 'file_type_'.$row['filetype_id']; 
    $result[$key] = $row; 
} 
+0

我仍然有一個問題:消息:preg_match()期望參數2是字符串,對象給出 文件名:mysql/mysql_driver.php – Periback

+0

你認爲我建立查詢的方式有問題嗎? – Periback

0
$query = $this->db->get_where('uploaded_files',array('upload_id' => $uploadId)); 
foreach($query->result() as $row) 
{ 
echo $row->filetype_id; 
} 

我認爲這對你有所幫助。