簡體   English   中英

php foreach選擇項目

[英]php foreach select item

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

試試這個兄弟

在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;
}

與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>
    ';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM