Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
Do you get any error from the second sql query? –  Ashley Oct 16 '11 at 11:58
 
What "does not" work about it? Are you getting errors, does nothing happen, do you get the wrong results? And why don't you use a join to select the image data on beforehand, instead of querying for each album? –  CodeCaster Oct 16 '11 at 11:59
add comment

1 Answer

up vote 2 down vote accepted

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

        }
share|improve this answer
 
ah!!!... silly me :) Thank you very much! –  cycero Oct 16 '11 at 12:11
 
Also.. fyi, the whole 6 line chunk of code where you assign $artist_id = $result[id], etc, etc.. is unnecessary. $result[id] doesn't exist and the subsequent list() just replaces all the values anyway –  Thilo Savage Oct 16 '11 at 12:11
 
@cycero, it happens :) Accept as correct please. –  webarto Oct 16 '11 at 12:18
 
@Thilo Savage, I suggested him something like that based on your comment.. –  webarto Oct 16 '11 at 12:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.