【问题标题】:gallery with next and prev function具有下一个和上一个功能的画廊
【发布时间】:2016-03-12 01:22:46
【问题描述】:

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

<?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


    【解决方案1】:

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

    <?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;
    
        }
    
    
    
    
    
    
    }
    
    
    ?>
    

    【讨论】:

    • 这实际上比我原来的帖子造成了更多的问题 :p 不过还是谢谢。
    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    相关资源
    最近更新 更多