如何在循环外使用循环变量?

时间:2013-11-19 16:14:53

标签: php arrays variables loops while-loop

我正在尝试在循环外使用$description变量。请帮帮我。

<?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
}
?>

我应该制作一个数组,还是首先定义它,我很困惑,需要帮助。

2 个答案:

答案 0 :(得分:1)

$albums数组(在while循环中)中,存储您的图片和说明,如下所示:

$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'
    )
);

然后在你的foreach循环中,这样做:

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

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

编辑: 别忘了改变这个

array_slice($a,1)

到此:

array_slice($a['images'],1)

答案 1 :(得分:0)

我只想将整个事物组合成一个循环;以下代码未经测试,但我希望您能遵循它。

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
}

我已经使用urlencode()htmlspecialchars()http_build_query()添加了转义功能,以防止HTML中的潜在中断(或XSS是最糟糕的情况)。