1

条目和图像,并且一直在尝试我能找到的各种方式加入他们以获得以下结果

entries:
entry_id
name

images:
entry_id
image_url

我正在使用 php,并希望能够检索给定 entry_id 的所有关联 image_url 行,并将它们与条目表中的其他信息结合起来。

所以我有类似的东西:

entries:
1, Brian
2, Steve
3, Jane

images:
1, images/brian1.jpg
1, images/brian2.jpg
2, images/steve.jpg
3, images/jane_1.jpg
3, images/jane_2.jpg
3, images/jane_3.jpg

并希望得到一个数组,例如

array(3) {
  [0]=>
    array(3) {
      ["entry_id"]=>
      string(1) "1"
      ["name"]=>
      string(5) "Brian"
      ["images"]=>
        array(2) {
          ["image_url"]=>
          string(17) "images/brian1.jpg"
          ["image_url"]=>
          string(17) "images/brian2.jpg"
        }
      }
  [1]=>
    array(3) {
      ["entry_id"]=>
      string(1) "2"
      ["name"]=>
      string(5) "Steve"
      ["images"]=>
        array(1) {
          ["image_url"]=>
          string(16) "images/steve.jpg"
        }
      }
  [2]=>
    array(3) {
      ["entry_id"]=>
      string(1) "3"
      ["name"]=>
      string(5) "Jane"
      ["images"]=>
        array(3) {
          ["image_url"]=>
          string(18) "images/jane_1.jpg"
          ["image_url"]=>
          string(18) "images/jane_2.jpg"
          ["image_url"]=>
          string(18) "images/jane_3.jpg"
        }
      }
    }

谢谢!

4

4 回答 4

1

没有测试过这些,我假设你想要这样的东西:

SELECT * FROM entries INNER JOIN images ON images.entry_id = entries.entry_id

然后遍历这些结果:

$entries = array();

while ($row = mysql_fetch_assoc($data))
{
    if (!isset($entries[$row['entry_id']]))
    {
        $row['images'] = array();
        $entries[$row['entry_id']] = $row;
    }

    $entries[$row['entry_id']]['images'][] = $row['image_url'];
}

然后你可以循环这个返回:

foreach ($entries as $entry)
{
    foreach ($entry['images'] as $image);
    // do something interesting
}

That should about do it for you, but you may have to modify some things.

于 2012-10-26T01:27:40.827 回答
0

您的数据库连接代码是否已排序?

我相信您正在寻找一个完整的外部连接。查看此资源以获取不同连接的 SQL 示例。

http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

于 2012-10-26T00:30:57.607 回答
0

简单的方法是这样的

 select * from entries, images where images.entry_id = entries.entry_id group by entries.entry_y;

加入是这样的:

SELECT * FROM entries INNER JOIN images ON images.entry_id = entries.entry_id
于 2012-10-26T00:33:21.307 回答
0

For display array like that, you cannot use join, try this :

  1. select all data in table entries
  2. foreach table entries, when foreach include table images

Ex:

$q_e = "SELECT * FROM entries"; $result_entry = mysql_query($q_e);

$entries = array(); $images = array();

while($entry = mysql_fetch_array($result_entry)) {

  $q_i = "SELECT * FROM `images` WHERE `entry_id` = '".$entry['entry_id']."'";
  $result_image = mysql_query($q_i);

  while($image = mysql_fetch_array($result_image)) {
      $images[] = array(
          'image_url' => $image['image_url']
      );
  }

  $entries[] = array (
      'entry_id' => $entry['entry_id'],
      'name' => $entry['name'],
      'images' => $images,
  );

}

echo 'pre>';var_dump($entries);

于 2012-10-26T01:55:01.097 回答