Nathalia Maroot
Nathalia Maroot

Reputation: 25

Loop DIV using php to show images

How do I display all images in a folder on my page?

HTML

<div class="itemS">
    <li><div class="itemType"><input type="image" src="image/blah1.jpg"/><gt_descA>Description here</gt_descA></div></li>
</div>

I want to use php to loop thru the images and display all of them along with the file name. But it doesn't work.

<?php
$folder = 'blah/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
for ($i = 0; $i < $count; $i++) {
   echo "<div class="itemS">
    <li><div class="itemType"><input type="image" src="'.$files[$i].'"/><gt_descA>"substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder))"</gt_descA></div></li>
</div>";      
}     
?>

Upvotes: 2

Views: 1058

Answers (3)

Ashish-Joshi
Ashish-Joshi

Reputation: 231

changes in you code is as follows :

hi i write this code for you and its working ...

you don't need the one more $count variable to be define.

 <?php

    $folder = 'images/';
    $filetype = '*.*';

    $div= '';

    foreach (glob($folder.$filetype) as $files) {    
        $div .= '<div class="itemS">';
        $div .= '<li><div class="itemType"><input type="image" src="'.$files.'"/><gt_descA>"'.substr($files,strlen($folder),strpos($files, '.') - strlen($folder)).'"</gt_descA></div></li>';
        $div .= '</div>';   
    }
    echo $div;
    ?>

Upvotes: 0

WAQAS AJMAL
WAQAS AJMAL

Reputation: 289

here problem is quotes positions single and double.. try this code

$folder = 'blah/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
for ($i = 0; $i < $count; $i++) {
   echo "<div class='itemS'>
<li><div class='itemType'><input type='image' src='".$files[$i]."'/> <gt_descA>".substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder))."</gt_descA></div></li>
</div>";      
}     

Upvotes: 1

Brandon Kum
Brandon Kum

Reputation: 3

I do believe that there is a need to concatenate the string in ECHO for php

try the below

<?php
$folder = 'blah/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
for ($i = 0; $i < $count; $i++) {
   echo "<div class="itemS">
    <li><div class="itemType"><input type="image" src="'.$files[$i].'"/><gt_descA>".substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder))."</gt_descA></div></li>
</div>";      
}     
?>

Upvotes: 0

Related Questions