2012-02-08 22 views
2

の内側に、私は3つのテーブルを持って、選択PHPのmysqlの、配列結果配列結果

tbl_photo    tbl_photo_comments  tbl_photo_likers 
___________    ____________    _____________ 
| photo_id |   | comment_id |   | like_id  | 
| photo_url |   | photo_id FK|   | user_id FK | 
| user_id FK|   | user_id FK |   | photo_id FK | 
         | comment | 

私の目標は、それぞれのコメントデータとlikersデータとともにtbl_photoから写真データを取得することです。私は

oneResultArray = 
{ 
    photo_url = "www.url.com/photo.png"; 
    photoID = 1; 
    user_id = 2 
    commentData = (
    { 
     comment = "comment 1"; 
     userid = 1 
    }, 
    { 
     comment = "comment 2"; 
     userid = 2 
    }, 
    { 
     comment = "comment 3"; 
     userid = 3}); 

    likersData = (
    { 
     userid = 2; 
     username = liker1; 
    }, 
    { 
     userid = 3; 
     username = liker2; 
    }); 

    }, 
{ 
    photo_url = "www.url.com/photo.png"; 
    photoID = 1; 
    user_id = 2 
    commentData = (
    { 
     comment = "comment 1"; 
     userid = 1 
    }, 
    { 
     comment = "comment 2"; 
     userid = 2 
    }, 
    { 
     comment = "comment 3"; 
     userid = 3}); 

    likersData = (
    { 
     userid = 2; 
     username = liker1; 
    }, 
    { 
     userid = 3; 
     username = liker2; 
    }); 
    } 

私の質問があり、そのデータ上の要素として2つの以上のアレイを持っているONE結果配列を持っているでどこ以下のように私はしたい配列の構造があり、それは、MySQL上で1つのクエリを使用して、これを達成することは可能ですか?もしそうでなければ、これを行う他の方法はありますか?君たちありがとう!

答えて

4

davidethellは、あなたがそれらのテーブルを結合する必要はありません指摘したように。したがって、1つのクエリでデータを選択することはできません。 Garry Weldingのアプローチは、あなたが持っている写真のレコードごとに後続のクエリを実行するように解釈されるかもしれません。これはであり、どちらでもしないでください。 3枚の写真が実行されると、7つのクエリが実行されます。それは必要以上に4倍です。 10枚の写真は21の質問につながります。あなたは写真を手に入れている。

<?php 

// build hierarchical result 
$result = array(); 

// getting the photos 
$query = $pdo->query('SELECT photo_id, photo_url, user_id FROM tbl_photo WHERE user_id = 5'); 
foreach ($query as $row) { 
    $result[$row['photo_id']] = $row; 
    $result[$row['photo_id']]['comments'] = array(); 
    $result[$row['photo_id']]['likes'] = array(); 
} 

if ($result) { 
    // comments and likes only for the photos we've selected 
    $photos = join(',', array_keys($result)); 

    // getting the comments 
    $query = $pdo->query('SELECT comment_id, photo_id, user_id, comment FROM tbl_photo_comments WHERE photo_id IN (' . $photos . ')'); 
    foreach ($query as $row) { 
     $result[$row['photo_id']]['comments'][$row['comment_id']] = $row; 
    } 

    // getting the likes 
    $query = $pdo->query('SELECT like_id, user_id, photo_id FROM tbl_photo_likers WHERE photo_id IN (' . $photos . ')'); 
    foreach ($query as $row) { 
     $result[$row['photo_id']]['likes'][$row['like_id']] = $row; 
    } 
} 

var_dump($result); 
+0

はい、これが最適です。 – deed02392

+0

こんにちは!ありがとう!大きな助け男! – janusbalatbat

1

各行に多数の重複データがない場合、1つのクエリでこれを実行することはできません。 3つのクエリでこれを実行すると、結果がより効率的に簡単に表示されます。あなたは、写真のテーブルを照会し、それをループし、ループの各反復で、コメントと好きなものの2つのクエリを実行します。

-1
$photos = {code to get the array of photos}; 

foreach($photos as &$photo) { 
    $photo['comments'] = {code to get photo comments using the photo id}; 
    $photo['likes'] = {code to get photo likes using the photo id} 
} 

return $photos; 

これは私がこれを行うために知っている唯一の方法です。 $ photの前の&は、配列を参照渡しすることです。参照渡し配列は、PHP5 +、http://php.net/manual/en/language.types.array.phpでのみ利用可能です。上記PHP.netリンクから

// PHP 5 
foreach ($colors as &$color) { 
    $color = strtoupper($color); 
} 
unset($color); /* ensure that following writes to 
$color will not modify the last array element */ 

// Workaround for older versions 
foreach ($colors as $key => $color) { 
    $colors[$key] = strtoupper($color); 
} 

print_r($colors); 
+0

こんにちは@ガリー溶接、その1つを試してみましょう!ありがとう! – janusbalatbat

関連する問題