0

I am trying to use $description variable outside the loop. Help me do it please.

<?php
$sql_album = "SELECT * FROM albums";

$res_album = mysql_query($sql_album) or die(mysql_error());

$albums = array();

$description = "";

while ($row_album = mysql_fetch_assoc($res_album)) {

    $description = $row_album['description'];

    $albums[$row_album['title']] = array(
        'images/albums/'.$row_album['title'].'/1.jpg',
        'images/albums/'.$row_album['title'].'/2.jpg',
        'images/albums/'.$row_album['title'].'/3.jpg',
        'images/albums/'.$row_album['title'].'/4.jpg'
    );
}

foreach ($albums as $name => $a) {
?>
<div id="album">
    <a href="view_album.php?name=<?php echo $name; ?>" data-images="<?php echo implode('|', array_slice($a,1))?>" class="album">
        <img src="<?php echo $a[0]?>" alt="<?php echo $name?>" />
        <span class="preloader"></span>
    </a>
    <div class="album_info">
        <a href="view_album.php?name=<?php echo $name; ?>"><h4><?php echo $name?></h4></a>
        <p><?php echo $description; ?></p>
    </div>
</div>
<?php
}
?>

Should I make an array, or define it first, i am totally confused, need help.

2
  • 1
    Accessing $description wouldn't help you. Think about it, it would only have the value of the last iteration of the while loop. You wouldn't be able to get the value for the corresponding image. Commented Nov 19, 2013 at 16:24
  • The mysql_* functions are no longer maintained and shouldn't be used in any new codebase. It is being phased out in favor of newer APIs. Instead you should use prepared statements with either PDO or MySQLi. Commented Nov 19, 2013 at 18:09

2 Answers 2

1

In your $albums array (in the while loop), store your images and description like this:

$albums[$row_album['title']] = array(
    "description" => $row_album['description'], 
    "images" => array(
        'images/albums/'.$row_album['title'].'/1.jpg',
        'images/albums/'.$row_album['title'].'/2.jpg',
        'images/albums/'.$row_album['title'].'/3.jpg',
        'images/albums/'.$row_album['title'].'/4.jpg'
    )
);

Then in your foreach loop, act like this:

<img src="<?php echo $a['images'][0]?>" alt="<?php echo $name?>" />

and

<p><?php echo $a['description']; ?></p>

Edit: Don't forget to change this

array_slice($a,1)

to this:

array_slice($a['images'],1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank, it works perfectly. I would probably come to this, but i was lacking logic :P
0

I would just combine the whole thing into a single loop; the below code is untested, but I hope you can follow it.

while ($row_album = mysql_fetch_assoc($res_album)) {
    print_album($row_album);
}

function print_album(array $album)
{
    $name = htmlspecialchars($album['title'], ENT_QUOTES, 'UTF-8');
    $description = htmlspecialchars($album['description'], ENT_QUOTES, 'UTF-8');

    $view_link = sprintf('view_album.php?%s', http_build_query([
        'name' => $album['title'],
    ]);

    $images = array_map(function($index) use ($album) {
        return sprintf(
            'images/albums/%s/%d.jpg',
            urlencode($album['title']),
            $index
        );
    }, range(1, 4));

    $images_data = htmlspecialchars(join('|', array_slice($images, 1)), ENT_QUOTES, 'UTF-8');

?>
<div id="album">
    <a href="<?php echo $view_link ?>" data-images="<?php echo $images_data; ?>" class="album">
        <img src="<?php echo htmlspecialchars($images[0], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo $name; ?>" />
        <span class="preloader"></span>
    </a>
    <div class="album_info">
        <a href="<?php echo $view_link; ?>"><h4><?php echo $name; ?></h4></a>
        <p><?php echo $description; ?></p>
    </div>
</div>
<?php
}

I've added escaping with the use of urlencode(), htmlspecialchars() and http_build_query() to prevent a potential break in your HTML (or XSS is the worst case).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.