php foreach选择项目

时间:2017-10-27 23:44:23

标签: php foreach

<?php
    $directory = "img/media/";
    $files = glob($directory. '/*.{jpg,jpeg,png,gif}', GLOB_BRACE);

//Loop through images
foreach($files as $image)
{
    echo'
        <div class="card animation_one p-0 m-0">
            <img class="card-img" src="'. $image .'" alt="Card image">
            <button type="button" class="btn btn-primary btn-lg btn-center bg-offBlue t-pureWhite" data-toggle="modal" data-target="#exampleModal" onclick="">Bekijk</button>
        </div>

        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
            <img class="img-fluid" src="'. $image .'" alt="Card image">
            </div>
        </div>
    ';
}
?>

我的问题是有些人非常简单,但我刚开始编码,所以我需要一些帮助。如果我有一个foreach循环,我如何知道并显示所选的(在本例中为img)以在模态弹出窗口中显示?

1 个答案:

答案 0 :(得分:0)

试试这个兄弟,

在foreach的帮助下

//Loop through images
$count=0;
foreach($files as $image)
{
    echo'
        <div class="card animation_one p-0 m-0">
            <img class="card-img" src="'. $image .'" alt="Card image">
            <button type="button" class="btn btn-primary btn-lg btn-center bg-offBlue t-pureWhite" data-toggle="modal" data-target="#exampleModal_'.$count.'" onclick="">Bekijk</button>
        </div>

        <div class="modal fade" id="exampleModal_'.$count.'" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
            <img class="img-fluid" src="'. $image .'" alt="Card image">
            </div>
        </div>
    ';
    $count+=1;
}

with for循环

for($i=0;$i<count($files);$i++)
{
    $image=$files[$i];
    echo'
        <div class="card animation_one p-0 m-0">
            <img class="card-img" src="'. $image .'" alt="Card image">
            <button type="button" class="btn btn-primary btn-lg btn-center bg-offBlue t-pureWhite" data-toggle="modal" data-target="#exampleModal_'.$i.'" onclick="">Bekijk</button>
        </div>

        <div class="modal fade" id="exampleModal_'.$i.'" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
            <img class="img-fluid" src="'. $image .'" alt="Card image">
            </div>
        </div>
    ';
}