0

わかりました。私はphpにかなり慣れていないので、私の質問は信じられないほどばかげているかもしれませんが、考えられるすべての組み合わせを試しましたが、これを正しく機能させることができません。

空のギャラリーにメッセージがまったく表示されないか、画像が含まれているかどうかに関係なく、すべてのギャラリーに3回表示されます。

サムネイルの周囲が最も望ましいものではないことは知っていますが、自動画像のトリミングの周囲でサムネイルを作成しようとしましたが、それを一生機能させることができませんでした。

私の主な関心事は空のギャラリーメッセージですが、信頼できる画像トリミングスニペットを組み込んでより良いサムネイルを作成するのを手伝ってくれるなら..してください:)

function lightbox_display($dir_to_search, $rel){
    $image_dir = $dir_to_search;
    $dir_to_search = scandir($dir_to_search);
    $image_exts = array('gif', 'jpg', 'jpeg', 'png');
    $excluded_filename = '_t';
    foreach ($dir_to_search as $image_file){
        $dot = strrpos($image_file, '.');
        $filename = substr($image_file, 0, $dot);
        $filetype = substr($image_file, $dot+1);
        $thumbnail_file = strrpos($filename, $excluded_filename);
        if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false){
            echo "<a href='".$image_dir.$image_file."' rel='".$rel."'>
                  <img src='".$image_dir.$image_file."' alt='".$filename."' width='100' height='80' title='' border='none'/>
                  </a>"."\n";
        } else {
            echo 'Currently there are no machines available for sale, please check back with us soon.';
        }
    }
}

更新されたphpコーディング:

$ imagesFoundに配列を追加して、サーバー側のフォルダーが含まれる可能性を排除してみました。

    function lightbox_display($dir_to_search, $rel){
    $image_dir = $dir_to_search;
    $dir_to_search = scandir($dir_to_search);
    $image_exts = array('gif', 'jpg', 'jpeg', 'png');
    $excluded_filename = '_t';
    $imagesFound = array('gif', 'jpg', 'jpeg', 'png') && 0;
        foreach ($dir_to_search as $image_file){
        $dot = strrpos($image_file, '.');
        $filename = substr($image_file, 0, $dot);
        $filetype = substr($image_file, $dot+1);
        $thumbnail_file = strrpos($filename, $excluded_filename);
    if ((!$thumbnail_file) && array_search($filetype, $image_exts) !== false) {
    $imagesFound++;
    echo "<a href='$image_dir$image_file' rel='$rel'>
          <img src='$image_dir$image_file' alt='$filename' width='100' height='80' title='' border='none'/>
          </a>\n";
}

    if ((0 === $imagesFound) !== true){
    echo 'Currently there are no machines available for sale, please check back with us soon.';
}
   }
 }
4

1 に答える 1

1

同じメッセージが 3 回表示されている場合は、ファイルが 3 つあると考えられますが、$dir_to_search画像はありません。.それらは、..および何か他のものである可能性があります。有効な画像ではないファイルを見つけるたびにメッセージを出力しているようです。そのため、代わりにできることは、見つかった画像の数を数えて、見つかった場合にのみテキストを出力することです。あなたは何も見つかりませんでした。例えば

$imagesFound = 0;
foreach ($dir_to_search as $image_file){
    $dot = strrpos($image_file, '.');
    $filename = substr($image_file, 0, $dot);
    $filetype = substr($image_file, $dot+1);
    $thumbnail_file = strrpos($filename, $excluded_filename);
    if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false) {
        $imagesFound++;
        echo "<a href='$image_dir$image_file' rel='$rel'>
              <img src='$image_dir$image_file' alt='$filename' width='100' height='80' title='' border='none'/>
              </a>\n";
    }
}

if (0 === $imagesFound) {
    echo 'Currently there are no machines available for sale, please check back with us soon.';
}

以下のコメントに応じて更新します。

lightbox_display() を複数回呼び出していて、メッセージを 1 回だけ表示したい場合は、見つかった画像の数を返し、それを使用できます。このようなもの:

function lightbox_display($dir_to_search, $rel) {
    $imagesFound = 0;
    foreach ($dir_to_search as $image_file){
        if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false) {
            $imagesFound++;
            echo "<img...";
        }
    }
    return $imagesFound;
}

$totalImagesFound = 0;
foreach ($galleries as $gallery) {
    $totalImagesFound += lightbox_display($gallery['dir'], $rel);
}

if (0 === $totalImagesFound ) {
    echo 'Currently there are no machines available for sale, please check back with us soon.';
}

あなたのシステムについてもっと知らなくても、私は本当に知りませんが、それは助けになるかもしれません.

于 2013-03-15T16:30:20.013 に答える