如何将标题添加到此自动创建的php库?

问题描述:

<?php   
    /* settings */ 
    $image_dir = 'gallery/'; 
    $per_column = 3; 
    $count=0; 
     if ($handle = opendir($image_dir)) { 
     while (false !== ($file = readdir($handle))) 
     { 
      if ($file != '.' && $file != '..') 
      { 
       if(strstr($file,'-thumb')) 
       { 
        $files[] = $file; 
       } 
      } 
     } 
     closedir($handle); 
    } 
    if(count($files)) 
    { 
     foreach($files as $file) 
     { 
      $count++; 
      echo '<a class="thumbnail" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>'; 
      if($count % $per_column == 0) { echo '<div class="clear"></div>'; } 
     } 
    } 
    else 
    { 
     echo '<p>There are no images in this gallery.</p>'; 
    } 

?> 

如何为每个图像添加标题? 非常感谢您的回答!如何将标题添加到此自动创建的php库?

+0

什么样的标题?像你从哪里得到每张照片的标题? – Vap0r

+0

这就是我想知道如何去做的。为每个文件写一个标题,并在当前脚本中使用php显示它。这可能与数组有关吗? –

您的代码遍历文件指定的目录内寻找在文件名含有...-thumb...文件,将它们附加到一个数组,然后在阵列上循环到用于显示缩略图图像库生成HTML。

添加额外的信息是这样的,由于非常有限的代码,您所提供的,可以以多种方式

  1. 你可以为每个文件名和列 实现数据库完成包含字幕/说明的相关列。这可能是更多的问题,比它的价值取决于你想要达到什么。
  2. 你可以使用它你可以解析由线(而不是穿越了...-thumb...图像0​​ 文件夹)线的平面文件,包含一些格式像

    file1-thumb.png|some caption here 
    file2-thumb.png|some caption here 
    file3-thumb.png|some caption here 
    ... 
    
  3. 你可以includ一个小的标题在文件名中,并从文件名中解析/格式化标题。这可能是最快的路线,但大多数限制字幕允许的长度/字符的灵活性。

    - file1-thumb--some_caption_here.png 
    - file2-thumb--some_caption_here.png 
    - file3-thumb--some_caption_here.png 
    

要真正字幕添加到生成的HTML,您可以使用title属性作为@rockerest建议,但是我个人添加这样一个标题图像本身,因为这是字幕被描述(不是链接)

<img src="..." title="..." ... /> 

UPDATE

回答您的评论(这提供了代码更好的格式),比方说,我们有一个名字file1-thumb--some_description_here.jpg一个文件,你可以分析和处理preg_replace

$filename = 'file1-thumb--some_description_here.jpg'; 
$caption = preg_replace(array('/^.+-thumb--/', '/\.(jpg|jpeg|gif|png|bmp)$/', '/_/'), array('','',' '), $filename); 

$caption格式化标题是现在一些描述在这里

+0

伟大的提示队友,我如何在标题中写下标题?或者,如果我使用str_replace(' - thumb','',$ file),如何删除.jpeg文本? –

+0

我会将此添加到我的答案中。 – deefour

当鼠标悬停在图像上时,html title提供了“标题”。

foreach($files as $file) 
     { 
      $count++; 
      echo '<a title="',$caption,'" class="thumbnail" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>'; 
      if($count % $per_column == 0) { echo '<div class="clear"></div>'; } 
     }