Jump to content

Recommended Posts

Hello,

 

I am listing images from folder with this code:

 

$imgdir = 'uploads/'.$nick.'/'; // the directory, where  images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes I want to show

$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
  $a_img[] = $imgfile;
  sort($a_img);
  reset ($a_img);
} 
}

$totimg = count($a_img); // total image number


for($x=0; $x < $totimg; $x++){

echo ' <div class="malaslika" style="width:100px;height:75px"><a href="../index.php?slika='.$a_img[$x].'"><img src="../uploads/'.$nick.'/'.$a_img[$x].'" width="100" height="75" border="0" onclick="changeImg(velika, "'.$kojaslika.'.jpg")" /></a></div>';

}	

 

Now, I want to list this in table with max 4 columns and not important how many rows.

 

So the thing is how to insert images from for loop into table with 4 columns?

 

Anyone can help? Thank you in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/
Share on other sites

I think that stuff between the td tags can be cleaned up but I don't know your dir struct so I left it alone...

 

$imgdir = 'uploads/'.$nick.'/'; // the directory, where  images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes I want to show
$a_img = glob($imgdr."*.{".implode(',',$allowed_types)."}",GLOB_BRACE);
sort($a_img);

echo "<table><tr>";         
foreach ($a_img as $img) {
  if ($colCount % 4 == 0) echo "</tr><tr>";
  $colCount++;
  echo ' <td><a href="../index.php?slika='.$img.'"><img src="../uploads/'.$nick.'/'.$img.'" width="100" height="75" border="0" onclick="changeImg(velika, "'.$kojaslika.'.jpg")" /></a></td>';
}
echo "</tr></table>";

 

 

 

I think that stuff between the td tags can be cleaned up but I don't know your dir struct so I left it alone...

 

$imgdir = 'uploads/'.$nick.'/'; // the directory, where  images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes I want to show
$a_img = glob($imgdr."*.{".implode(',',$allowed_types)."}",GLOB_BRACE);
sort($a_img);

echo "<table><tr>";         
foreach ($a_img as $img) {
  if ($colCount % 4 == 0) echo "</tr><tr>";
  $colCount++;
  echo ' <td><a href="../index.php?slika='.$img.'"><img src="../uploads/'.$nick.'/'.$img.'" width="100" height="75" border="0" onclick="changeImg(velika, "'.$kojaslika.'.jpg")" /></a></td>';
}
echo "</tr></table>";

 

 

 

 

It makes table right, but it doesn't insert images from folder I specified but from my main folder. Don't know why it does that?

 

But thanks for the code.

that's because your path/to/image is probably wack. offhand I see in $imgdr you have it listed as a subfolder of the current working directory (no ../) but in the path/to/image in your image tag, you build it it again instead of using your $imgdr, and it has that "../" at the beginning, which makes it look for the directory above the current working directory. 

 

I would suggest changing your img src to

 

src="'$imgdr.$img.'"

 

edit: oops, forgot the concat dot

 

src="'.$imgdr.$img.'"

that's because your path/to/image is probably wack. offhand I see in $imgdr you have it listed as a subfolder of the current working directory (no ../) but in the path/to/image in your image tag, you build it it again instead of using your $imgdr, and it has that "../" at the beginning, which makes it look for the directory above the current working directory. 

 

I would suggest changing your img src to

 

src="'$imgdr.$img.'"

 

edit: oops, forgot the concat dot

 

src="'.$imgdr.$img.'"

 

Yeahh, but somehow $img variable contains full path to image "uploads/nick/image.jpg" and I need only "image.jpg". Is that possible?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.