如何使用下一个 botton 和自动查找文件夹为 HTML 模板创建演示栏


How to create Demo bar for HTML Templates with Next botton and automatic finding folders?

我有一些HTML模板,我想用iFrame显示它们,其中一些模板有2到11种颜色。

folder name ex.
1
2
3
3-1
3-2
3-3
3-4
3-5
4
5
6-1
6-2
7
and more...

我有一个用于显示下一个模板的 botton。

我想在 php 中使用 is_dir 来检查目录 1 或 3-2 是否可用,然后显示它。

<?php
$id = $_GET['id'];
$c=$id+1;
$c1=$c."-1";
$xm=$c;
$xm1=$c1;   
if (is_dir($xm)) {
    $c=$c;  
}
if (is_dir($xm1)) {$c=$c1;}
?>
<div id="header-bar">
    <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="hidden" name="id" value="<?php echo $c ?>">
        <input type="submit" value="NEXT">
    </form>
</div>
<iframe src="http://barg.ir/demo/<?php echo $id; ?>"></iframe>

问题1:我可以显示文件夹3-1,但我不能显示文件夹3-2和3-3以及...,这是怎么做的?

问题2:可以用php数组编码吗?

问题3:用JavaScript编码吗?在javascript中is_dir什么?

你为什么不考虑使用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