nady gold
nady gold

Reputation: 109

unable to display result through foreach loop

I have array its name is $img_arr and it shows following records. I want to display images with its relevant ids. so I have the following array. but unable to implement it.

Array(
[0] 1
[1] 2
[2] 3
[3] 4
)
Array(
[0] adsense-approval.png
[1] feedback.jpg
[2] logo1.jpg
[3] logo2.png
)

my code is following. in id and data-id i want to get array result 0,1,2..

<?php 
    if(isset($img_arr) && !empty($img_arr)){ 
        foreach($img_arr as $img){ ?>

    <div class="img-wrap" id="img_<?php echo $img;?>">
        <span class="close">&times;</span>
        <img src="./gallery/<?php echo $img ?>" class="img-circle" style="height:50px; width:50px;" data-id="<?php echo $img ?>">
    </div>
<?php 
    } 
}
?>

Upvotes: 0

Views: 50

Answers (3)

Javad Alirezaeyan
Javad Alirezaeyan

Reputation: 135

Your image path is wrong, You must write "../gallery" instead of "./gallery".

<?php 
    if(isset($img_arr) && !empty($img_arr)){ 
        foreach($img_arr as $img){ ?>

    <div class="img-wrap" id="img_<?php echo $img;?>">
        <span class="close">&times;</span>
        <img src="../gallery/<?php echo $img ?>" class="img-circle" style="height:50px; width:50px;" data-id="<?php echo $img ?>">
    </div>
<?php 
    } 
}
?>

Upvotes: 0

Ankur Tiwari
Ankur Tiwari

Reputation: 2782

You can try this way with your HTML code:

$idArray = array(1, 2, 3, 4);
$imageArray = array('adsense-approval.png', 'feedback.jpg', 'logo1.jpg', 'logo2.png');

foreach($idArray as $key => $value) 
{
    echo $value;
    echo $imageArray[$key];
}
?>

If we merge code with your HTML it looks as follow

<?php 
    if(is_array($imageArray))
    { 
        foreach($imageArray as $key => $img)
        { ?>
            <div class="img-wrap" id="img_<?php echo $img;?>">
                <span class="close">&times;</span>
                <img src="./gallery/<?php echo $img ?>" class="img-circle" style="height:50px; width:50px;" data-id="<?php echo $idArray[$key] ?>">
            </div>
<?php 
    } 
}
?> 

Basically, you need to manage your image with your Id array in the key-value format for that you need to use the $key variable in you foreach

Upvotes: 2

Pupil
Pupil

Reputation: 23958

foreach can be used with only values or with key value pairs.

Only Values:

foreach ($array as $value) { // Where $value is array value
...

Key Value Pairs:

foreach ($array as $key => $value) { // Where $key -> key & $value -> value
...

You need to add key in foreach's definition.

Working Code:

<?php if(isset($img_arr) && !empty($img_arr)){ 
                  foreach($img_arr as $key => $img){ ?>
                <div class="img-wrap" id="img_<?php echo $key;?>">
                     <span class="close">&times;</span>
                            <?php echo $key;?> <img src="./gallery/<?php echo $img ?>" class="img-circle" style="height:50px; width:50px;" data-id="<?php echo $key ?>">
                        </div>
                  <?php } }?>

Upvotes: 1

Related Questions