简体   繁体   中英

How to use a loop variable outside the loop?

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.

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)

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM