php - MySQL使用foreach将多个图像连接到相册并在页面上显示

标签 php mysql foreach

我正在尝试将多个图像加入相册并使用 foreach 将它们显示在页面上。

+----+----------+----------------------------------+-----------+----------+
| id | album_id | name                             | url       | theme_id |
+----+----------+----------------------------------+-----------+----------+
|  1 |        1 | 60b0603f40993bfe86d54756505416cb | project_1 | 1        |
|  2 |        1 | 5efd6e08fed71ac6ad18bdde997e97bd | project_1 | 1        |
|  3 |        2 | fe4ac6806722e7cd74a60476da9ec4ea | project_2 | 1        |
|  4 |        2 | 87028bcd233b5ca5133fd0be335ff4b7 | project_2 | 1        |
|  5 |        2 | 9326d956c5cd30a75ff8b90d59e18273 | project_2 | 1        |
|  6 |        3 | 325925af3c04d936aa0e292b007b19e9 | project_3 | 1        |
|  7 |        3 | 0e087fb5e2038a746398d8dbe362032d | project_3 | 1        |
|  8 |        3 | 8be793e3e619771bb81e55c805d23b7d | project_3 | 1        |
+----+----------+----------------------------------+-----------+----------+

当我运行以下代码时,我在第一个 album_id 中获得了正确的图像名称和 url,但例如在 album_id=2 中,我仍然会获得 project_1 url,在 album_id=3 中,我将获得 project_2 url。我完全迷失了。

       $album = find_album($_GET['id']);
       $albums = find_albums_by_theme($album['theme_id']);
       $image = find_image($_GET['id']);
       $images = find_images_by_album($image['album_id']);
       $themes = find_themes();

<ul>
        <?php foreach($images as $image): ?>
            <li><img src="photos/<?php echo $image['url']; ?>/large/<?php echo $image['name']; ?>.jpg" alt="<?php echo safe_output($album['title']); ?>" /></li>
   <?php endforeach; ?>
               </ul>

我的功能:

function find_image($id)
{
  db_connect();

  $query = sprintf("SELECT
                         images.id,
                         images.album_id,
                         images.name,
                         images.url,
                         albums.id
                             FROM
                               images, albums
                             WHERE
                               images.album_id = albums.id and
                               images.id = '%s'
                           ",
                         mysql_real_escape_string($id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $row = mysql_fetch_array($result);

  return $row; 

    function find_images_by_album($album_id)
    {
      db_connect();

      $query = sprintf("SELECT images.id,
                               images.name,
                               images.url,
                               images.album_id,
                               albums.id,
                               albums.theme_id,
                               themes.name as theme,
                               themes.id
                      FROM
                             images, themes, albums
                      WHERE
                             images.album_id = albums.id and albums.theme_id = themes.id and
                             albums.id = '%d'

                             ORDER BY images.id;

                             ",
                             mysql_real_escape_string($album_id));

    $result = mysql_query($query);
    if(!$result)
      {
        return false;
      }

      $result = db_result_to_array($result);

      return $result;

    } 

function find_albums_by_theme($theme_id)
{
  db_connect();

  $query = sprintf("SELECT albums.id,
                         albums.title,
                         albums.theme_id,
                         albums.created_at,
                         albums.fullname,
                         albums.vote_cache,
                         themes.name as theme
                  FROM
                         albums, themes
                  WHERE
                         themes.id = '%s' and albums.theme_id = themes.id
                  ORDER by vote_cache DESC
                         ",
                         mysql_real_escape_string($theme_id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $result = db_result_to_array($result);

  return $result;

}


function find_themes()
{
  db_connect();

  $query = "SELECT * from themes order by id DESC";

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $result = db_result_to_array($result);

  return $result;

}


function find_theme($id)
{
  db_connect();

  $query = sprintf("SELECT * FROM themes
                             WHERE id = '%s'
                           ",
                         mysql_real_escape_string($id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $row = mysql_fetch_array($result);

  return $row;

}


function find_album($id)
{
  db_connect();

  $query = sprintf("SELECT
                         albums.id,
                         albums.title,
                         albums.theme_id,
                         albums.created_at,
                         albums.fullname,
                         albums.vote_cache,
                         albums.discuss_url,
                         albums.description,
                         themes.name as theme

                             FROM
                               albums, themes
                             WHERE
                               albums.theme_id = themes.id and
                               albums.id = '%s'
                           ",
                         mysql_real_escape_string($id));

$result = mysql_query($query);
if(!$result)
  {
    return false;
  }

  $row = mysql_fetch_array($result);

  return $row;

} 

最佳答案

在这两行中,您将 id 用于 album.id 和 image.id。我想这会引起问题......

   $album = find_album($_GET['id']);
   $image = find_image($_GET['id']);

关于php - MySQL使用foreach将多个图像连接到相册并在页面上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5273365/

相关文章:

php - 如何使用sql结果中的值作为其子查询的表名?

.net - 使用 VB.NET 更新 MySQL

php - 连接两个数组

perl - 在 Perl 中修改然后切片未知大小的二维数组

javascript - 将变量传递给 forEach 循环内的请求 promise 函数

php - 如何长期积累红利?

php - 使用 Paypal 的信用卡授权?

javascript - PHP 限制输入字段的字符数

php - 如何在 MySQL 数据库中存储嵌套/多维数组中的值?

php - 三个插入语句插入三个表