2011-03-11 51 views
0

我试图将多个图像合并到相册并使用foreach在页面上显示它们。MySQL将多个图像合并到相册并使用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图像正确的名称和网址,但例如在album_id = 2我仍然会得到PROJECT_1网址,并在album_id = 3,我会得到project_2网址。我完全迷失了。

 $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; 

} 

回答

1

在这两条线,你使用id两人都有album.id和image.id。我想这是造成问题...

$album = find_album($_GET['id']); 
    $image = find_image($_GET['id']); 
相关问题