php - 使用 PHP + Mysql 对目录中的文件进行排序

标签 php mysql

嗨!

基本上我有一个图像轮播,其中每个图像都动态链接到文章。 我有一个读取目录的脚本,将文件名存储到一个数组中。 然后我剪掉文件扩展名,通过搜索具有相同/相似名称的文章来查询数据库以查找我希望图像链接到的文章。 例如:blets.jpg 链接到腰带文章。 所以我开始工作了。

但我想做的是能够让图像按照我的文章在排序栏中分配给它们的顺序显示。

我现在获取图像并按字母顺序显示它们,我想要做的是让它们按照我分配给文章的顺序显示。

这是我的代码:

     echo "<div id='carousel'>\n";

    // Get image file name
  // open directory
  $myDirectory = opendir("./images/products/carousel/spring-summer-2011/");

  while($fileName = readdir($myDirectory))  // get each file
  {
    $dirArray[] = $fileName;
  }

  closedir($myDirectory); // close directory


  //sort($dirArray); // sort files


  echo "<div class='infiniteCarousel'>
        <div class='wrapper'>
          <ul>\n";

  foreach ($dirArray as $file)
  {
      if (substr("$file", 0, 1) != ".") //don't list hidden files
      {
      $name = substr($file, 0, strrpos($file, '.')); // trim file name extension
      $res = mysql_query("Select id, alias, ordering from content where alias like '{$name}'  ORDER BY ordering"); // order by is pretty useless here !!
              while ($row = mysql_fetch_array($res))
              {
              $id = $row['id'];
              $alias = $row['alias'];
              $ordering = $row['ordering'];

      echo "<li><a href='index.php?option=com_content&view=article&id={$id}' title='{$alias}'>\n";
      echo "<img src='./images/products/carousel/spring-summer-2011/{$file}' height='80' width='120' alt='{$alias}' />";
      echo "</a></li>\n";
              }

      }
  }

  echo "</ul>\n</div>\n</div>\n";

  echo "</div>"; //Close Carousel

我在代码中留下了我的评论。 而且我基本上知道需要做什么,只是不确定如何去做。 我需要专业人士!?帮助。

最佳答案

$Line[] = array();

foreach ($dirArray as $file)
 {
  if (substr("$file", 0, 1) != ".") //don't list hidden files
  {
  $name = substr($file, 0, strrpos($file, '.')); // trim file name extension
  $res = mysql_query("Select id, alias, ordering from content where alias like '{$name}'  ORDER BY ordering"); // order by is pretty useless here !!
          while ($row = mysql_fetch_array($res))
          {
          $id = $row['id'];
          $alias = $row['alias'];
          $ordering = $row['ordering'];

  $Line[] = "<li id='$ordering'><a href='index.php?option=com_content&view=article&id={$id}' title='{$alias}'>\n <img src='./images/products/carousel/spring-summer-2011/{$file}' height='80' width='120' alt='{$alias}' /></a></li>\n";
          }

  }
  }

sort($Line);
foreach ( $Line as $ordering => $line ) {
echo "$line";
}

关于php - 使用 PHP + Mysql 对目录中的文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6103630/

相关文章:

mysql - 通过组合它们来减少结果

php - 应该使用 `if ($a != NULL)` 还是 `if ($a !== NULL)` 来控制程序流?

php - PHP 源代码中的这段代码是什么意思?

php - 使用 Laravel 检查数据库是否存在

mysql - 有没有办法关闭存储过程的结果集?

mysql - 选择计数行然后更新值

php - 如何将 '00:04:13.67' 格式的时间转换为秒?

php - 将 MySQL 现有数字与变量相加

php - 如何在 javascript 操作上重新加载 php 函数?

php - Zend Framework mysql查询返回服务器500错误