我正在图像库上工作,这是我学习php的第一个真正的项目..我无法以某种方式绕过的问题是使用"之前的"按钮。我的"下一个"函数抓取文件夹中的下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;
}
?>
答案 0 :(得分:0)
如果我要做这样的事情,我可能会创建一个文件控制器类来管理我可能需要的所有文件操作。
<?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;
}
}
?>