你为什么不考虑使用glob()
?将以下内容放入您要从中获取信息的文件夹中:
// we'll call it grabber.php
<?php
$files = glob('*', GLOB_ONLYDIR); sort($files, SORT_NATURAL);
?>
// now you can include grabber.php in your form file
<?php
session_start(); include_once 'PATH/grabber.php'; $end = count($files)-1;
<div id='header-bar'>
<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<input type='submit' value='BACK' name='back' />
<input type='submit' value='NEXT' name='next' />
</form>
if(isset($_POST['back'])){
$_SESSION['fileNum']--;
if($_SESSION['fileNum'] < 0)$_SESSION['fileNum'] = $end;
}
elseif(isset($_POST['next'])){
$_SESSION['fileNum']++;
if($_SESSION['fileNum'] > $end)$_SESSION['fileNum'] = 0;
}
else{
$_SESSION['fileNum'] = 0;
}
$f = $files[$_SESSION['fileNum']];
echo " <iframe src='http://barg.ir/demo/$f'></iframe>".
'</div>';
?>
显然PATH
需要改变。
最好使用 JavaScript:
// You'll still need `grabber.php` in the same location as before, only now
// we'll actually make the PHP page into a String for JavaScript usage, like:
<?php
$dirs = glob('*', GLOB_ONLYDIR); sort($dirs, SORT_NATURAL);
$dirsJS = implode("', '", $dirs); // implode into a String for JavaScript Array
echo "//<![CDATA[
var doc = document, bod = doc.body;
bod.className = 'js'; // use .njs class in CSS for users without JavaScript
function E(e){
return doc.getElementById(e);
}
function direct(backId, nextId, iframeId, iframeSrcBase){
var dirs = ['$dirsJS'];
var dl = dirs.length-1, n = 0, f = E(iframeId), s = iframeSrcBase;
f.src = s+dirs[0];
E(backId).onclick = function(){
if(--n < 0)n = dl;
f.src = s+dirs[n];
}
E(nextId).onclick = function(){
if(++n > dl)n = 0;
f.src = s+dirs[n];
}
}
//]]>";
?>
/*
Now back to your main page. I like XHTML, but you can use whatever.
The reason for JavaScript is to avoid scrolling issues and page flashing
This is your main page again without as much PHP. There's no need to
include the other file in PHP, or use a session. We use the `script`
tag instead. Pay attention:
*/
<?php
echo "<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<style type='text/css'>
@import 'yourCSS.css';
</style>
</head>
<body class='njs'>
<div id='header-bar'>
<form method='post' action='{$_SERVER['PHP_SELF']}'>
<input type='button' value='BACK' name='back' id='back' />
<input type='button' value='NEXT' name='next' id='next' />
</form>
<iframe id='ifr' src=''><noscript>Your Browser Does Not Support JavaScript</noscript></iframe>
</div>
<script type='text/javascript' src='PATH/grabber.php'></script>
<script type='text/javascript'>
direct('back', 'next', 'ifr', 'http://barg.ir/demo/');
</script>
</body>
</html>";
?>
再一次,这一次在script
标签中,PATH
相应地改变。