Jump to content

[SOLVED] list images in table


fricx

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.