Jump to content

looping issue


fife

Recommended Posts

I have a loop which works fine but before the loop I run a query which I store as $AlbumName

 

From this array and within the loop I want to echo $AlbumName['album_name']; but obvisouly its not working.  Can somebody please show me how to do it?

 

 <?php 
	  		  
	  
	  $qImage = "SELECT * FROM photos";
	  $rImage = mysql_query($qImage);
	  $Array = mysql_fetch_array($rImage);

	  $qAlbumName = "SELECT * FROM albums WHERE id =".$Array['album']."";
	  $rAlbumName = mysql_query($qAlbumName);
$AlbumName = mysql_fetch_array($rAlbumName);
	  
	  while ($row_images = mysql_fetch_assoc($rImage) )
	  {
	  ?>
	  
            <?php echo $row_images['id'];
           
             echo $row_images['name']; 
           echo $row_images['photo_description'];
         
		echo $AlbumName['album_name'];

           } ?>

Link to comment
Share on other sites

There's a few things, for one mysql_fetch_assoc will return a numeric array unless you tell it not to be doing

 

mysql_fetch_array($result, MYSQL_ASSOC)

 

Other than that, your first sql query is selecting multiple rows but you're not looping the results.

Link to comment
Share on other sites

First off, let's simplify your code and then check and see if we return data from the queries by dumping it.

 

<?php
    //  This part of the code is going to reuturn just ONE result, keep that in mind.
    //  You may want to add, ORDER BY date DESC or something of that sort
    $imageq     = mysql_query("SELECT * FROM photos");
    $image         = mysql_fetch_array($imageq);

    //  This query is going to get the album name from whatever
    //  image was returned, which is not set to a definite yet.
    $AlbumNameq = mysql_query("SELECT * FROM albums WHERE id = '{$image['album']}'");
    $AlbumName = mysql_fetch_array($AlbumNameq);

    while ($row_images = mysql_fetch_assoc($imageq) )
    {
        print_r($row_images);
        print_r($AlbumName);
        echo $row_images['id'];
        echo $row_images['name']; 
        echo $row_images['photo_description'];
        echo $AlbumName['album_name'];
    }
?> 

 

The results we dump should output the array of keys and values that you want to use, this will help us figure out why it's not looping.

Keep in mind that by doing this we execute a query twice, which is not necessary.  For better code, we can do this instead:

<?php
    //  This part of the code is going to reuturn just ONE result, keep that in mind.
    //  You may want to add, ORDER BY date DESC or something of that sort
    $imageq                 = mysql_query("SELECT * FROM photos ORDER BY id DESC");
    while ($row_images         = mysql_fetch_assoc($imageq) )
    {
        //  Set $row_images['album'] to however the album is defined in the photos table
        $AlbumNameq         = mysql_query("SELECT * FROM albums WHERE id = '{$row_images['album']}'");
        $AlbumName             = mysql_fetch_array($AlbumNameq);
         
        echo $row_images['id'];
        echo $row_images['name']; 
        echo $row_images['photo_description'];
        echo $AlbumName['album_name'];
    } 
?>

 

I strongly recommend using this second code and trying that, after fixing up whatever table values need to be corrected, if you need any explanations feel free to ask.

Link to comment
Share on other sites

infact im wrong.  If i try to echo

	echo
        $AlbumNameq         = mysql_query("SELECT * FROM albums WHERE id = '{$row_rs_images['album']}'");
        $AlbumName             = mysql_fetch_array($AlbumNameq);

 

nothing happens

Link to comment
Share on other sites

To me it feels like the issue is coming from the AlbumName query being used outside the loop, and the $array information being built off a bad query.

 

Have you tried my second code from the previous example? that array query is going to return results if there is an album id that matches the one defined in the photos table.

 

So say you loop the photos, you return stuff like:

photo_id=1

photo_album=1

then the albums query, which is just meant to return ONE specific album, the name of the album that the picture is under obviously, so it makes sense your new album query will work.

 

Go ahead and try the second code, but with this addition:

<?php

    $images                 = mysql_query("SELECT * FROM photos ORDER BY id DESC");
    while ($row                = mysql_fetch_assoc($images) )
    {
        //  whatever the field is called in table photos that holds the album, change $row['album'] to that name
        $albums                = mysql_query("SELECT * FROM albums WHERE id = '{$row['album']}'");
        $album               = mysql_fetch_array($albums);
        
        $row['album']        = $album['album_name'];
         
        echo $row['id'];
        echo $row['name']; 
        echo $row['photo_description'];
        echo $row['album'];
        
        echo '<pre>';
        print_r($row);
        print_r($album);
        echo '</pre>';
    } 
?>

 

Tell me the dumps it provides, I want to know the query results for $row and $album.

 

Edit:

Sorry I didn't see that I had resolved your issue.

Also, what pikachu2000 means is that you shouldn't have  things like:

$sql = "SELECT * FROM blah";

$sqlA = mysql_query($sql);

 

You should just do, $sql = mysql_query("SELECT * FROM blah");

 

Please mark the topic as solved, and glad I could help.

Link to comment
Share on other sites

can you explain please?

 

Form the query string outside mysql_query(), then when things inevitable crater, you have the ability to echo it in it's entirety.

 

$query = "SELECT `field` FROM `table` WHERE `field` = $value";
if( !$result = mysql_query($query) ) {
     echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>';
} else {
     // query succeeded, continue.
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.