2013-06-03 4 views
0

3 개 테이블 1 개가 item 테이블이고, 하나는 note 테이블이고 다른 하나는 note image 테이블입니다. Codeigniter/PHP, 2 Mysql 테이블, 다중 쿼리

사용자가 항목 세부 정보를 볼

은, 모든 음이

노트는 그 다음에 부착 된 여러 개의 이미지를 가질 수는 플랫 파일에 저장됩니다 (메모 테이블의 ITEM_ID 필드가) 해당 항목의 선택됩니다 만 "메모 이미지"표에 의해 참조됩니다.

이제 항목 세부 정보를 표시 할 때 항목에 대한 모든 메모를 가져 오는 쿼리를 실행하면 간단합니다. 이러한 결과는 반복되어 페이지에 출력됩니다.

문제는 이제 노트에 이미지를 추가 한 후 발생, 당신은 어떻게 말하는 항목에 대한 모든 사항을 질의에 대해 갈 것이라고 다음

SELECT * FROM notes WHERE item = 1

방법 것 당신 루프하지만 메모에 대한 모든 노트의 이미지를 얻는 결과 배열 내가 PHP의 결과와 출력을 얻는 방법을 시각화 할 수없는 작은 때문에

SELECT * FROM note_img WHERE note_img_noteid = 27 

그것의 내 머리를 아프게 말한다.

는 --- 편집 ---

SELECT 
d.door_note_id, 
d.door_note_doorid, 
d.door_note_timestamp, 
d.door_note_editedtime, 
d.door_note_text, 
u.user_name AS created_by, 
e.user_name AS edited_by, 
i.door_img_id AS img_id, 
i.door_img_url AS img_url 

FROM 
user u, 
door_note d 

LEFT JOIN 
user e 
ON 
user_id = d.door_note_editeduserid 
LEFT JOIN 
door_img i 
ON 
door_img_noteid = d.door_note_id 
WHERE 
d.door_note_doorid = 214 
AND 
u.user_id = d.door_note_userid 

그럼 내가 이것을 사용 겠어요 월 생각해

foreach ($result->result() as $row){ 
    if(!isset($my_items[$row->door_note_id])){ //the note id becaoms a key 
     //here you set up an array for all the note details 
     $my_items[$row->door_note_id] = array('door_note_id'=>$row->door_note_id, 
     'door_note_doorid'=>$row->door_note_doorid, 
     'door_note_timestamp'=>$row->door_note_timestamp, 
     'door_note_editedtime'=>$row->door_note_editedtime, 
     'door_note_text'=>$row->door_note_text, 
     'created_by'=>$row->created_by, 
     'edited_by'=>$row->edited_by, 
     'images'=>array()); 
    } 
    //if the note has any images add them to the images array for that note. 
    if(isset($row->img_url)){ 
     $my_items[$row->door_note_id]['images'][] = $row->img_url; 
    } 
} 
+0

메모에 대한 사실 이미지가 선택 사항이지만 2 가지 쿼리를 수행해야한다고 생각합니다. 그러나 잘 모르겠습니다. – Sam

답변

1

그것의 아주 열심히 '할 때 당신에게 피난처를 알고 관계를 테이블에 게시하지만 몇 가지 가정을합니다.

$query = "SELECT items.id as item_id, items.name as item_name, notes.id as note_id, 
    notes.description as note_description, note_image.image as note_image from notes 
    LEFT JOIN notes ON items.id = notes.item_id 
    LEFT JOIN note_image ON notes.id = note_image.note_img_noteid"; 

//this wil fetch all you items with description, notes and images, because item can have multiple notes, your result wil have multiple entires of the item. so you have to index correctly to use in views 

$result = $this->db->query($query) 

$my_items = array(); 

foreach ($result->result() as $row){ 

    if(!isset($my_items[$row->item_id])){ //you item it becaoms a key 
     //here you set up an array for all your items 
     $my_items[$row->item_id] = array('item_name'=>$row->item_name, 'notes'=>array()); 
     } 
    //here you stroe all images fro a note 
    if(!isset($my_items[$row->item_id]['notes'][$row->note_id])){ 

      $my_items[$row->item_id]['notes'][$row->note_id] = array('note_description'=>$row->note_description, 'images'=>array()); 

     } 

     $my_items[$row->item_id]['notes'][$row->note_id]['images'][] = $row->note_image; 


    } 
+0

이것이 작동해야하는 것처럼 보입니다. 이름을 조금 바꿔야 할 것입니다.하지만 테스트 할 때 다시 게시 할 것입니다. 이미지 업로드 스크립트에서 찾은 다른 문제를 정렬합니다. – Sam