Jump to content

Recommended Posts

I have this code which makes a 2 column table with images from a folder, it gets the captions from a text file - see below

 

 


<?php 

$images = "uploads/"; # Location of small versions
$big    = "thumbs/"; # Location of big versions (assumed to be a subdir of above)
$cols   = 2; # Number of columns to display
$text   = "des.txt";

$fh=fopen($text, "r");

while(!feof($fh))
{
  $temp = explode(",", $line);
  $description[$temp[0]] = $temp[1];
  $line=fgets($fh);
  unset($temp);
}

if ($handle = opendir($images)) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && $file != rtrim($big,"/")) {
           $files[] = $file;
       }
   }
   closedir($handle);
}

$colCtr = 0;

echo '<table width="100%" cellspacing="3"><tr>';

foreach($files as $file)
{
  if($colCtr %$cols == 0)
    echo '</tr><tr><td colspan="' . $cols . '"><hr /></td></tr><tr>';
  $allfiles .= $file . ',<br />';
  echo '<td align="center"><a href="' . $images . $big . $file . '"><img width="300px" src="' . $images . $file . '" /></a><br />' . $description[$file][0] . '</td>';
  $colCtr++;
}

echo '</table>' . "\r\n";
echo $allfiles;

?> 

 

this file des.txt has      file_name,caption      however when implemented it only seems to display the 1st letter of the caption - any ideas why and how to solve? Thanks

 

scan0006.jpg,Pic1
scan0005.jpg,scan0005.jpg
scan0003.jpg,
scan0009.jpg,
scan0002.jpg,
scan0008.jpg,
scan0001.jpg,
scan0004.jpg,
scan0007.jpg,

Link to comment
https://forums.phpfreaks.com/topic/109187-adding-cations-to-images/
Share on other sites

in this piece of your code I don't understand what is getting exploded. $line doesn't get defined until after explode(). Please explain

 

while(!feof($fh))

{

  $temp = explode(",", $line);

  $description[$temp[0]] = $temp[1];

  $line=fgets($fh);

  unset($temp);

}

ok great. now we have the $description array I don't know how you want to iterate through it but if its consecutive we can just grab the first key and then move the pointer to the second and so on

 

foreach($files as $file)

{

  if($colCtr %$cols == 0)

  $descript = current($description);

    echo '</tr><tr><td colspan="' . $cols . '"><hr /></td></tr><tr>';

  $allfiles .= $file . ',<br />';

  echo '<td align="center"><a href="' . $images . $big . $file . '"><img width="300px" src="' . $images . $file . '" /></a><br />' . $descript . '</td>';

  $colCtr++;

next($description);

}

 

 

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.