2017-01-02 11 views
0

私は地元のサッカークラブでテレビ画面用のプログラムを調整しようとしています。私はすでにサイトを作っていましたが、今はもっとユーザーフレンドリーにしたいと思っています。htmlでphpを使用してスライドショーに画像を追加する

具体的には、誰かが特定のフォルダに画像を追加するだけで、フォルダ内のすべての画像をスライドショーで使用できるようにしたいと考えています。

コードの関連部分は以下のとおりです。

<html> 
    <head> 
     <script type="text/javascript" src="js/tabs.js"></script> 
     <script src="js/jquery.js"></script> 
     <link rel="stylesheet" type="text/css" href="css/style.css"> 
     <script type="text/javascript" src="js/slider.js"></script> 
    </head> 
    <body> 
     <div class="corner_1"> 
      <?php include("inc/script.php"); ?> 
     </div> 
     <div class="slider" id="slider"> 
      <ul> 
       <li style="background-image:  url('http://homedir.com/images/IMG_4947.JPG'); no-repeat center center fixed; 
    background-repeat: no-repeat; 
     background-size: 100% 100%; 
    background-position: center top; 
    background-attachment: fixed;"> 
       </li> 
       <li style="background-image:  url('http://homedir.com/images/IMG_4939.JPG'); no-repeat center center fixed; 
    background-repeat: no-repeat; 
     background-size: 100% 100%; 
    background-position: center top; 
    background-attachment: fixed;"> 
       </li> 
       <li style="background-image:  url('http://homedir.com/images/IMG_4922.JPG'); no-repeat center center fixed; 
    background-repeat: no-repeat; 
     background-size: 100% 100%; 
    background-position: center top; 
    background-attachment: fixed;"> 
       </li> 
      </ul> 
     </div> 
    </body> 
    </html> 

は、私はPHPでのglob()およびエコー機能で何かを作ってみましたが、それは仕事を得ることができませんでした:イメージは、ハードコードされていることに注意してください。

答えて

0

あなたはscandir()簡単に十分な操作を行うことができます。

# I am just going to use this to clean up the html, you don't have to do this... 
$style = array(
    'background: no-repeat center center fixed', 
    'background-repeat: no-repeat', 
    'background-size: 100% 100%', 
    'background-position: center top', 
    'background-attachment: fixed' 
); 
$style = implode('; ',$style); 
# I use a define('ROOT_DIR',__DIR__) in a root-based config file to get the full path 
$path = '/var/www/vhosts/domain/images'; 
# Scan the directory 
$dir = scandir($path); 
# Loop through files in directory 
foreach($dir as $file) { 
    # Use our root path to make sure file exists 
    # (there are . and .. that will be in the array 
    if(!is_file($path.'/'.$file)) 
     continue; 
?> 
     <li style="background-image: url('http://homedir.com/images/<?php echo $file ?>'); <?php echo $style ?>"> 
     </li> 
<?php 
} 
+0

あなたはこれが正しいと確信していますか?予期しないEOFエラーが発生する。 – Jaspervb

+0

どのように?構文は賢明ですか? – Rasclatt

+0

構文は正しいです。この行の配列を取得する必要があります: '$ dir = scandir($ path);'。 '$ path'が有効であることを確認する必要があります。 'echo(is_dir($ path))? '有効': '有効ではありません'; exit; 'ディレクトリが存在することを確認します。 – Rasclatt

関連する問題