首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建带有下一个botton和自动查找文件夹的HTML模板演示条?

如何创建带有下一个botton和自动查找文件夹的HTML模板演示条?
EN

Stack Overflow用户
提问于 2013-11-14 23:19:14
回答 1查看 426关注 0票数 2

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

代码语言:javascript
复制
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是否可用,然后显示它。

代码语言:javascript
复制
<?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>

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

Question2:可以用php数组进行编码吗?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-14 23:27:25

为什么不考虑使用glob()呢?将以下内容放入您想从其中获取信息的文件夹中:

代码语言:javascript
复制
// 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:

代码语言: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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19990457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档