具有下一个和上一个功能的图库


gallery with next and prev function

我正在制作一个图片库,这是我学习php的第一个真正的项目。我无法以某种方式解决的问题是使用"上一个"按钮。我的"next"函数从文件夹中抓取接下来的 6 张图像并将它们发布到页面上,我的 prev 函数反向执行此操作。但这是不正确的,你能帮忙吗?哦,这段代码是草率的初学者代码,所以我很乐意提供提示:)

<?php 
session_start();
if(session_id() == '') {
 $_SESSION["count"] = 0;
}
function PostHTML($id, $file) {
    if($id==0) {
           echo "<div class='"item'"><div class='"well'"><img 
           class='"img-responsive'" src='"$file'" alt='"'"><p>blahblahblah xxxxxx</p></div></div>";
    }else{
           echo "<div class='"item'"><div class='"well'"><video autoplay 
           loop class='"img-responsive'"><source src='"$file'" type='"video/mp4'"></video></div></div>";
    }
}
function next_img() {
   $dir = "images/src/*.*"; 
   $files = array();
   $start = $_SESSION["count"];
   $end = $_SESSION["count"]+6;
   $y=0;
       foreach(glob($dir) as $file) {
          $files[$y] = $file;
          $y++;
       }
for($i=$start; $i < $end; $i++){
   if(pathinfo($files[$i], PATHINFO_EXTENSION)=="jpg") { 
     PostHTML(0,$files[$i]); 
   } else { 
     PostHTML(1,$files[$i]); }
}

$_SESSION["count"]+=6;

}
function prev_img() {
   $dir = "images/src/*.*"; 
   $files = array();
   $start = $_SESSION["count"]-1; 
   $end = $_SESSION["count"]-6;
   $y=0;
   foreach(glob($dir) as $file) {
        $files[$y] = $file; //100 files
        $y++;  
    }
   for ($i = $start; $i > $end; $i--) {
        if(pathinfo($files[$i], PATHINFO_EXTENSION)=="jpg") { 
         PostHTML(0,$files[$i]); 
       } else { 
         PostHTML(1,$files[$i]); 
       }   
   }
   $_SESSION["count"]-=6;
}
?>

如果我要做这样的事情,我可能会创建一个文件控制器类来管理我可能需要的所有文件操作。

<?php


class FileController
{
    private $_dir = "DIR";
    private $files;
    private $current;
    function__construct()
    {
        $current = 0;
        $files = scandir($_dir); // scans the dir to get a list of file names
        if(!$files) echo "Failed To Load Files"; // failed to load files
        else 
        {
            $this->file = $files; // objects propery contains all file names in array;
        }
    }

    function PostHTML($fileName) {

        $src = $this->_dir + "/" + $fileName;
        if(!$fileName)
        {
                return "<div class='"item'"><div class='"well'"><img 
           class='"img-responsive'" src='"$src'" alt='"'"><p>blahblahblah xxxxxx</p></div></div>";
        }
        else
        {
            return "<div class='"item'"><div class='"well'"><video autoplay 
           loop class='"img-responsive'"><source src='"$src'" type='"video/mp4'"></video></div></div>";
        }
}


    function nextImg()
    {
        $current = $this->getCurrent();
        $end = $current + 6;
        $this->setCurrent($end);
        $files = $this->files;
        $html = "";
        for($i = $current; $i<= $end; $i++)
        {
            $html += postHtml($files[$i]);
        }
        echo $html;

    }

    function prevImg()
    {
        $current = $this->getCurrent();
        $end = $current - 6;
        $this->setCurrent($end);
        $files = $this->files;
        $html = "";
        for($i = $current; $i>= $end; $i--)
        {
            $html += postHtml($files[$i]);
        }
        echo $html;

    }
    function getCurrent()
    {
        return $_SESSION['current'];
    }
    function setCurrent($current)
    {
            $_SESSION['current'] = $current;
    }



}

?>