我的网站上有一个"全部显示"功能。目前有7个子文件夹被迭代以显示类别。(没有显示空文件夹,可能会添加更多的子文件夹)。
我不想设置每页[图像]的限制,我只想在不同的页面上显示每个文件夹的所有图像。
尽管进行了大量的谷歌搜索和实验,但我能想到的最好的办法是在一个长页面上显示多个html页面,所有页面都是一个接一个地附加的。(啊!)从昨晚开始我就一直在做,所以我有点受够了。
我已经剥离了displayall.php的基本功能。以下是目前的情况(没有分页):
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<link rel='stylesheet' type='text/css' href='css/bootstrap_combined.css'>
<link rel='stylesheet' type='text/css' href='css/style.css'>";
<title>Gallery Display</title>
</head>
<body>
<div id="wrapper">
<header>
<h1>The Ultimate Gallery Compiler</h1>
<div id='menu'><a class='head' href='index.html'>Upload Photo</a> <a class='head' href='gallery.html'>Browse Gallery</a></div>
</header>
<!---end header-->
<div id='content'>
<h2>Gallery: All Images</h2>
<?php
//$pages = isset($_GET['page'])?intval($_GET['page']-1):0; //get page
//$pageno = $_REQUEST['page'];
//compile an associative array
$subfolder= array('Fashion/LifeStyle'=>'1', 'Music/Video'=>'2','Sport/Leisure'=>'3','Electronics/Technology'=>'4','Pets/Animals'=>'5','Health/Beauty'=>'6','Other'=>'7' );
$carray=count($subfolder);
// iterate through the sub directories, and count them
$files = "images/*/*";
$sub=0;
foreach(glob($files) as $file)
{
$sub++; //total number of images
}
foreach ($subfolder as $subf=>$value)
{
$folder = "images/".$value."/";
$link = 'displayall.php?'.$value;
$mykey= $subf;
$counter = 0;
// Open the appropriate subfolder, and display its contents.
if ($dir = opendir($folder)) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$counter++;
$images[] = $file;
}
}
closedir($dir);
}
//don't show empty categories
if ($counter!== 0){
echo "<input type='hidden' id='current_page'/>";
echo "<input type='hidden' id='show_per_page' />";
echo "<h3>Category: ".$mykey."</h3><br>"; //display heading
echo "<h4>There are " .$sub. " photos in the <a href='gallery.html'>Gallery</a> to date</h4>"; //display total overall
echo '<div id="multi">';
foreach($images as $image) {
echo '<span class="gallery"><img src="';
echo $folder.$image;
echo '" alt="" width="200" height="auto" /></span>'; //show images
}
echo '</div>';
//$num= $pages +1;
echo "<br><strong>".$counter. " photos</strong><br>"; //display total per category
echo "<span id='pageNo'>Page Number: ".$value."</span><br>";
echo "<a href = ".$link. ">Next</a>";
echo "<br><br><hr><br>"; //put space and a line between the categories
};
//$pageno++;
}
?>
<footer>
<p class='foot'>© Copyright 2015-2016 MMA2 Rachel Gallen, Ysabel Pheifer and Rebecca Merrigan.</p>
</footer>
</div>
</body>
</html>
(可以在这里查看)
如有任何关于分页的帮助,我们将不胜感激。所有的教程似乎要么与mysql相关,要么处理单个数组。V令人沮丧!
(对php、jquery、javascript或引导程序解决方案开放!)
感谢并快乐chrimbo
Rachel
edit:以前的编辑删除了
您的页面正在所有子文件夹中循环,看起来似乎试图只显示由querystring参数page
指定的子文件夹。这可以通过对PHP:进行小的编辑来实现
<?php
$pages = isset($_GET['page'])?intval($_GET['page']):1; //get page
//$pageno = $_REQUEST['page'];
//compile an associative array
$subfolder= array('Fashion/LifeStyle'=>'1', 'Music/Video'=>'2','Sport/Leisure'=>'3','Electronics/Technology'=>'4','Pets/Animals'=>'5','Health/Beauty'=>'6','Other'=>'7' );
$carray=count($subfolder);
// iterate through the sub directories, and count them
$files = "images/*/*";
$sub=0;
foreach(glob($files) as $file)
{
$sub++; //total number of images
}
$value = $pages;
$folder = "images/".$value."/";
$link = 'displayall.php?page='.$value + 1;
//$mykey= $subf;
$arrayKeys = array_keys($subfolder);
$mykey = $arrayKeys[$value];
$counter = 0;
// Open the appropriate subfolder, and display its contents.
if ($dir = opendir($folder)) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$counter++;
$images[] = $file;
}
}
closedir($dir);
}
//don't show empty categories
if ($counter!== 0){
echo "<input type='hidden' id='current_page'/>";
echo "<input type='hidden' id='show_per_page' />";
echo "<h3>Category: ".$mykey."</h3><br>"; //display heading
echo "<h4>There are " .$sub. " photos in the <a href='gallery.html'>Gallery</a> to date</h4>"; //display total overall
echo '<div id="multi">';
foreach($images as $image) {
echo '<span class="gallery"><img src="';
echo $folder.$image;
echo '" alt="" width="200" height="auto" /></span>'; //show images
}
echo '</div>';
//$num= $pages +1;
echo "<br><strong>".$counter. " photos</strong><br>"; //display total per category
echo "<span id='pageNo'>Page Number: ".$value."</span><br>";
echo "<a href = ".$link. ">Next</a>";
echo "<br><br><hr><br>"; //put space and a line between the categories
};
//$pageno++;
?>
变更摘要:
我取消了对$pages
变量的注释,因此它从查询字符串参数中提取。
看起来您还试图通过$link
变量发送该查询字符串,但格式不正确。我将其更新为$link = 'displayall.php?page='.$value + 1;
最后,我删除了for循环,并将$value
设置为当前的$page
参数,这样页面上只显示一个子文件夹。
编辑#1后所做的更改
$pages
上的三元运算将值设置为0,导致在查找/images/0/
时出现错误。更改为$pages = isset($_GET['page'])?intval($_GET['page']):1;
添加了一个数组来处理关联数组中的键,因为querystring使用的是整数值而不是类别名称$arrayKeys = array_keys($subfolder);
$mykey = $arrayKeys[$value];
您可能需要仔细研究将字符串值而不是整数值传递到$page。它将有助于更好的SEO,更好的文件夹名称,并消除了使用关联数组将整数映射到类别名称并再次映射的需要
最后注释:
这当然有扩展的空间,您可以考虑进行错误检查,以确保值在边界内并且文件夹有效。上一个和下一个链接,以及快速导航到特定页码也可能很有用。
希望能有所帮助!