加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:38:11 所属栏目:PHP教程 来源:网络整理
导读:嗨! 基本上我有一个图像轮播,每个图像动态链接到文章. 我有一个脚本读取目录,将文件名存储到一个数组中. 然后我修剪文件扩展名,通过搜索具有相同/相似名称的文章来查询数据库中我想要图像链接的文章. 例如:blets.jpg链接到皮带文章. 所以我开始工作了. 但
嗨!

基本上我有一个图像轮播,每个图像动态链接到文章.
我有一个脚本读取目录,将文件名存储到一个数组中.
然后我修剪文件扩展名,通过搜索具有相同/相似名称的文章来查询数据库中我想要图像链接的文章.
例如: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",1) != ".") //don't list hidden files
      {
      $name = substr($file,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",1) != ".") //don't list hidden files
  {
  $name = substr($file,'.')); // trim file name extension
  $res = mysql_query("Select id,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";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读