Sunny khan
Sunny khan

Reputation: 1

How can I set my for - loop to print images in the HTML - page

I have two images in a folder and their record is in a database so I want to show the images on the page through a for loop. But the problem is that I have classes (left and right) for which I want to define a variable to show images separately, so how can I do. The only problem is with the $val variable.

<div class="col-md-7 wthree-services-bottom-grids">
            <?php for ($i = 0; $i < 2; $i++){ $val = ''?>
            <div class="wthree-services-<?php echo $val; ?>">
                <img src="<?php echo base_url(); ?>uploads/frontend/about_us/<?php echo $about_images[$i]->image; ?>" alt="">
            </div>
            <?php  $val = 'right'; }?>
<!--            <div class="wthree-services-right">
                <img src="<?php echo base_url(); ?>uploads/frontend/about_us/<?php echo $about_images[1]->image; ?>" alt="">
            </div>-->
            <div class="clearfix"> </div>
        </div>

Upvotes: 0

Views: 50

Answers (1)

BudwiseЯ
BudwiseЯ

Reputation: 1826

I'm not sure if I understand correctly what you're trying to do but does this behave like what you're after?

<div class="col-md-7 wthree-services-bottom-grids">

<?php
for ($i = 0; $i < 2; $i++):

    $val = ($i == 0 || $i % 2 == 0 ? 'left': 'right'); ?>

    <div class="wthree-services-<?php echo $val; ?>">
        <img src="<?php echo base_url(); ?>uploads/frontend/about_us/<?php echo $about_images[$i]->image; ?>" alt="">
    </div>
    <div class="clearfix"></div>

<?php endfor; ?>

Upvotes: 1

Related Questions