0

I'm really beginner in web programming. I want to make webpage which contain photo gallery. This is the code

<body>
    <div class="container">
        <header class="clearfix">
        </header>
        <div class="main">
            <a class="fancybox" href="gallery/1.jpg" data-fancybox-group="gallery" title="Lorem ipsum dolor sit amet"><img src="gallery/1.jpg" alt="" /></a>
            <a class="fancybox" href="gallery/2.jpg" data-fancybox-group="gallery" title="Etiam quis mi eu elit temp"><img src="gallery/2.jpg" alt="" /></a>
            <a class="fancybox" href="gallery/3.jpg" data-fancybox-group="gallery" title="Cras neque mi, semper leon"><img src="gallery/3.jpg" alt="" /></a>
            <a class="fancybox" href="gallery/4.jpg" data-fancybox-group="gallery" title="Lorem ipsum dolor sit amet"><img src="gallery/4.jpg" alt="" /></a>
            <a class="fancybox" href="gallery/5.jpg" data-fancybox-group="gallery" title="Etiam quis mi eu elit temp"><img src="gallery/5.jpg" alt="" /></a>
            <a class="fancybox" href="gallery/6.jpg" data-fancybox-group="gallery" title="Cras neque mi, semper leon"><img src="gallery/6.jpg" alt="" /></a>
            <a class="fancybox" href="gallery/7.jpg" data-fancybox-group="gallery" title="Lorem ipsum dolor sit amet"><img src="gallery/7.jpg" alt="" /></a>
        </div>
    </div>
</body>

I have to type the name of photos manually. But it will be tired if the amount of photos is too much. I want to add php code to upload those images from a folder automatically. How to use php code inside the

<div class="main"></div>

Please give me example.

3 Answers 3

0

Try this in your <div>: <body> <div class="container"> <header class="clearfix"> </header> <div class="main"> <?php $image_array = array_diff(scandir("images"), array('..', '.')); $i = 0; foreach ($image_array as $key) { ?> <a class="fancybox" href="<?php echo 'images/'.$key; ?>" data-fancybox-group="gallery" title="Lorem ipsum dolor sit amet"> <img src="<?php echo 'images/'.$key; ?>" alt="" height="150" width="150"/></a> <?php $i++; } ?> </div> </div> </body> Change images to your folder name.

Sign up to request clarification or add additional context in comments.

9 Comments

i have tried the code, the webpage just showing these symbol "; } ?>
Do you open with Web server or just HTML>?
i use xampp and the apache service has been started, and i try the code in new file, just this code in the body tag. And images appear with the actual size
Have you setting your width images in your CSS? You must try @jitendra parmar answer. I think it had completed
the result is same with your way, that gives me symbol "; } ?>
|
0

This should be use full

<body>
    <div class="container">
        <header class="clearfix">
        </header>
        <div class="main">
            <?php
                $image_array = array_diff(scandir("YOUR IMAGES FOLDER NAME"), array('..', '.'));
                $i = 0;
                foreach ($image_array as $key) { ?>
            <a class="fancybox" href="<?= 'YOUR IMAGES FOLDER NAME/'.$key ?>" data-fancybox-group="gallery" title="Lorem ipsum dolor sit amet"><img src="<?= 'YOUR IMAGES FOLDER NAME/'.$key ?>" alt="" height="150" width="150"/></a>
            <?php $i++; } ?>
        </div>
    </div>
</body>

1 Comment

i have tried the code, the webpage just showing these symbol "; } ?>
0

you can use PHP RecursiveDirectoryIterator

<body>
    <div class="container">
        <header class="clearfix">
        </header>
        <div class="main">
        <?php $images = new RecursiveDirectoryIterator('images');
        foreach($images as $img){
        if(is_dir($img->getFileName())){ continue; }
            echo '<a class="fancybox" href="folder/'.$img->getFileName().'" data-fancybox-group="gallery" title="'.$img->getFileName().'"><img src="folder/'.$img->getFileName().'" alt="'.$img->getFileName().'" /></a>';
        }?>

        </div>
    </div>
</body>

1 Comment

this is not valid - as each element in the loop will only contain private methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.