2

我有一些 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...

我有一个显示 NEXT 模板的按钮。

我想is_dir在 php 中使用来检查目录 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数组编码吗?

Question3:可以用JavaScript编码吗?is_dirjavascript中什么是相等的?

4

1 回答 1

2

你为什么不考虑使用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相应地改变。

于 2013-11-14T23:27:25.103 回答