Вопрос

I have two directories : one has images and the other has ZIP files. The files in both directories have the same names e.g : 1.zip , 1.png

I scanned each folder like this :

$images    = 'screenshots/';
$scanned_images = array_diff(scandir($images), array('..', '.'));
$zips = 'download/';
$scanned_zips = array_diff(scandir($zips), array('..', '.'));

then :

foreach ($scanned_images as $value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
foreach ($scanned_zips as $val)
{
echo '<div class="portfolioDescription">';
echo'<h4>Download:'.$val.'</h4>';
echo'<p><a href="download/'.$val.'">Click here to download</a></p>';
echo'</div></div>';
}
}

This does not work. Each image in first directory will have the whole zip files of second directory in its description!

I have also tried to combine the two arrays into one array and use foreach ($result as list ($a, $b)) but as list always gives an error.

How to overcome this ?

Это было полезно?

Решение

no need to have nested foreach.

use this:

<?php

$images    = 'screenshots/';
$scanned_images = array_diff(scandir($images), array('..', '.'));
$zips = 'download/';
$scanned_zips = array_diff(scandir($zips), array('..', '.'));

foreach ($scanned_images as $value)
{
    $name = substr($value, 0, strrpos($value, '.'));
    $pos = array_search($name.'.zip', $scanned_zips);
    if($pos != null){
        echo '<div class="portfolioItem">';
        echo    '<a href="'.$images.$value.'" class="zoom img" title="'.$value.'" rel="portfolio">';
        echo        '<img src="'.$images.$value.'" class="portfolio-image" alt="'.$value.'" />';
        echo    '</a>';
        echo    '<div class="portfolioDescription">';
        echo        '<h4>Download:'.$scanned_zips[$pos].'</h4>';
        echo        '<p><a href="'.$zips.$scanned_zips[$pos].'">Click here to download</a></p>';
        echo    '</div>';
        echo '</div>';
    }
}

Другие советы

Add a break; statement at the end of your inner foreach loop and that should fix it. You have two foreach loops and hence the downloads are listed multiple times. To fix this issue,

Change your code to:

<?php
foreach ($scanned_images as $value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
    foreach ($scanned_zips as $val)
    {
    echo '<div class="portfolioDescription">';
    echo'<h4>Download:'.$val.'</h4>';
    echo'<p><a href="download/'.$val.'">Click here to download</a></p>';
    echo'</div></div>';
    break; //exiting
    }
}

?>

One way is to hash your files by name without extension. Then use same key to retrieve image data and zip data. Example:

$scanned_images = array('1.png', '2.png');
$scanned_zips = array('1.zip', '2.zip');

//Should be like that after hashing
$scanned_images = array('1' => '1.png', '2' => '2.png');
$scanned_zips = array('1' => '1.zip', '2' => '2.zip');

So the code could be:

function get_file_name($path) {
    $name = basename($path);
    $name = substr($name, 0, strrpos($name, '.'));
    return $name;
}

function hash_files_by_name($items) {
    $hashed = array();
    foreach($items as $item) {
        $name = get_file_name($item);
        $hashed[$name] = $item;
    }

    return $hashed;
}


$scanned_images = array('1.png', '2.png'); // get images directory filesnames
$scanned_zips = array('1.zip', '2.zip'); // get zips directory filenames.

$imgs = hash_files_by_name($scanned_images);
$zips = hash_files_by_name($scanned_zips);

foreach ($imgs as $key=>$value)
{
    echo '<div class="portfolioItem">';
    echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
    echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
    if(isset($zips[$key])) {
        echo '<div class="portfolioDescription">';
        echo'<h4>Download:'.$zips[$key].'</h4>';
        echo'<p><a href="download/'.$zips[$key].'">Click here to download</a></p>';
        echo'</div></div>';
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top