cycero
cycero

Reputation: 4761

while inside of another while in PHP using MySQL

I need to do the following - get the list of all artists from the database and print them on a page. But besides of that, I need to make another query to the "albums" table and get the albums of each artist and print the images under each artist description. The code I've written is as follows:

require('db.php');

$query = "SELECT * FROM artists";
$result = mysql_query($query);

$artist_id = $result[id];
$artist_name = $result[name];
$artist_surname = $result[surname];
$artist_email = $result[email];
$artist_about = $result[about];
$artist_photo = $result[photo];
while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result)) :
    print "<div class='row'>";
    print "<div class='artists_left'>";  
    print  "<div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>";
    print  "</div>";  
    print "<div class='artists_right'>";
    print   "<div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>";
    print   "<div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>";
    print     "<ul class='artists'>";
    $albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
    $albums_result = mysql_query($albums_query);
    $album_id = $albums_result[id];
    $album_photo = $albums_result[title_photo];
    $album_dirname = $albums_result[dirname];
    while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($result)) :   
        print "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
    endwhile;
    print "</ul>";
    print      "<a href='#' class='seemore_portfolio'>все работы мастера &gt;</a>";
    print  "</div>";    
    print "</div>";
endwhile;
mysql_close($connect);

The outer query works fine, but the inner one - does not. Could anybody help me to figure this out?

Thanks in advance.

Upvotes: 0

Views: 3763

Answers (1)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

Change the second $result to $albums_result

You can also consider writing the whole thing like this...

<? 
        require('db.php');

        $query = "SELECT * FROM artists";
        $result = mysql_query($query);

        while(list($artist_id, $artist_name, $artist_surname, $artist_email, $artist_about, $artist_photo) = mysql_fetch_row($result))
        {

        $li = '';

        $albums_query = "SELECT id, title_photo, dirname FROM albums WHERE master = $artist_id";
        $albums_result = mysql_query($albums_query);

        while(list($album_id, $album_photo, $album_dirname) = mysql_fetch_row($albums_result))
        {
            $li .= "<li><a href='#'><img src='galleries/$album_dirname/$album_photo' alt='image' /></a></li>";
        }

        echo "<div class='row'>
        <div class='artists_left'>  
        <div class='gallery_cont'><a href='#'><img src='timthumb.php?src=images/artists/$artist_photo&w=240&h=318' alt='$artist_name' /></a></div>
        </div>  
        <div class='artists_right'>
        <div class='artist_title_cont'><span class='model'>Художник:</span><span class='name'>$artist_name $artist_surname</span><span class='mypage'>Личная почта:</span><a href='#' class='mypage'>$artist_email</a></div>
        <div class='artist_title_cont' style='margin-top:20px;'><span class='name'>$artist_about</span></div>
        <ul class='artists'>
        $li
        </ul>
        <a href='#' class='seemore_portfolio'>все работы мастера &gt;</a>
        </div>
        </div>";

        }

Upvotes: 1

Related Questions