0

So I'm having little trouble, I would like to order randomly all the folders in order to display them in a grid, with thumbnails, so here's my code

<?php
if ($handle = opendir('.')) {
    $blacklist = array('.htaccess', '.', '..', 'css', 'js', 'fonts', 'images', 'cv.pdf', 'includes', 'index.php', '.DS_Store', 'About');
    echo "<ul class='grid' id='grid'>";
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
shuffle($file);
            $titre = file_get_contents($file . "/" . "Titre.txt");
            $categorie = file_get_contents($file . "/" . "Categorie.txt");
            $class_isotope = file_get_contents($file . "/" . "Classe.txt");
            echo "<li class='element mix " . $class_isotope . "' ><a href=" . $file . ">
    <img src='images/thumb-" . $file . ".jpg' alt='' /><div><h3>" . $titre . "
    <span class='subtitle'>" . $categorie . "</span></h3></div></a></li>";
        }
    }
    echo "</ul>";
    closedir($handle);
}
?>

I tried to use the shuffle function but it doesn't worked.

If you could help me with that it would be great. thanks in advance.

T J
  • 42,762
  • 13
  • 83
  • 138
  • Just saying it "doesn't work" doesn't help us help you figure out what the problem is. Update your question to tell us if you're getting a syntax error or unexpected output or anything else that'd help us help you narrow down the problem. – Amal Murali Mar 29 '14 at 12:01
  • 1
    `opendir`, `readdir` and `closedir` are functions that all result in a manual enumeration, eg it's not an array until you make it an array. Take a look at the [glob](http://us1.php.net/glob) function, which returns an array instead. – Codecat Mar 29 '14 at 12:02
  • I'm not getting any syntax error, but when I use shuffle I get a white page. The thing is, in local I was displaying my folders by numeric order, and it worked, but on my server, it's all mixed up, so i'm looking for a way to display them randomly or by numeric order. I've been looking for a way to do that for three ours but I found nothing. I tried the glob function but I'm not very experimented with php, that is why I am asking you ^^ – user3475638 Mar 29 '14 at 12:45

2 Answers2

0

$file contains a single file reference as a string

it is not an array of all the files in the directory

shuffle(&$arr) expects an array passed by reference as parameter $arr

so wont work with a string

If you want to randomize the order of the files in the directory first build an array with all the files, shuffle it, then do what needs to be done with each file.

Paolo
  • 15,233
  • 27
  • 70
  • 91
  • Thanks, I will try that. I want to shuffle the order of the files to display them randomly in a grid every time you refresh the page. – user3475638 Mar 29 '14 at 12:51
0

Ok so instead I used this, and it worked perfectly :

$files = array();
$dir = new DirectoryIterator('.');
foreach ($dir as $fileinfo) {
$files[$fileinfo->getMTime()] = $fileinfo->getFilename();
}

shuffle($files);
echo "<ul class='grid' id='grid'>";
foreach($files as $file){
    if ($file == "index.php" or $file == ".." or $file == "/"  or $file == "." or $file == "css" or $file == "js" or $file == "fonts" or $file == "images" or $file == "cv.pdf" or $file == "includes" or $file == ".DS_Store" or $file == "About" or $file == "404.php"    ){
    }else{
         $titre = file_get_contents($file."/"."Titre.txt"); 
    $categorie = file_get_contents($file."/"."Categorie.txt"); 
    $class_isotope = file_get_contents($file."/"."Classe.txt"); 
        echo "<li class='element mix ".$class_isotope."' ><a href=".$file."><img src='images/thumb-".$file.".jpg' alt='' /><div><h3>".$titre."<span class='subtitle'>".$categorie."</span></h3></div></a></li>";
    }
}
echo "</ul>";