Amr Mashrah
Amr Mashrah

Reputation: 187

PHP Is there a way to display my list from the foreach separately?

I want to display the links separately. One list would include all the .html links and the other all the .jpg links.

right now it displays this and I understand why it is doing it, but when i do another foreach outside or even right before the echo, its like nothing is passing into it.

.jpg
.html
.jpg
.html

I need it to display like this

HTML-Backup          HTML
.jpg                .html
.jpg                .html

php Code

$array = array();
$html= "";
$htmlBackup= "";
if(file_exists("uploads/" . $_POST["prefix"] .'/'))
{
    $dir = 'uploads/' . $_POST["prefix"] . '/';
    $files = preg_grep('~\.(jpeg|jpg|png)$~', scandir($dir));
    $prefixDir = scandir($dir);
    foreach($prefixDir as $dir_files)
    {
        $secondDir = $dir . $dir_files; 
        //$finalDir=scandir($secondDir);
        if((is_dir($secondDir)))
        {
            $finalDir=preg_grep('~\.(html|jpg)$~', scandir($secondDir));
            $i = 0;
            foreach($finalDir as $lastDir)
            {
                if(strpos($lastDir, "HTML5.jpg") !== false)
                {
                    echo'<a href="'.$secondDir.'/'.$lastDir.'">'.$lastDir.'</a> </br>';
                    //variable to store list of dir
                    $html=$lastDir;
                } else if(strpos($lastDir, "HTML5.html") !== false )
                {
                    echo'<a href="'.$secondDir.'/'.$lastDir.'">'.$lastDir.'</a> </br>';
                    //variable to store the list of dir
                    $htmlBackup=$lastDir;
                }
            }
        }
    } 
} 

Upvotes: 0

Views: 31

Answers (2)

Amr Mashrah
Amr Mashrah

Reputation: 187

I found a solution by including arrays.

foreach($prefixDir as $dir_files)
            {
                $secondDir = $dir . $dir_files; 
                //$finalDir=scandir($secondDir);
                if((is_dir($secondDir)))
                {
                    $finalDir=preg_grep('~\.(html|jpg)$~', scandir($secondDir));
                    $i = 0;
                    
                    
                    foreach($finalDir as $lastDir)
                    {

                        
                        if(strpos($lastDir, "HTML5.html") !== false || strpos($lastDir, "H5.html") !== false)
                        {
                            //variable to store the list of dir
                           array_push($html_array, $lastDir);
                           array_push($html_dir,$secondDir.'/'.$lastDir);
                        }
                        else if(strpos($lastDir, "HTML5.jpg") !== false || strpos($lastDir, "H5.jpg")!== false )
                        {
                            //variable to store list of dir
                            array_push($html_backup_array, $lastDir);
                             array_push($html_backup_dir,$secondDir.'/'.$lastDir);
                        }
                    }
                }
          }

and then display them under in another foreach as I want

 if(!empty($html_array))
        {
            echo "<p>";
            echo ("<p><span class='heading'>HTML5 Units</span></p>");
            foreach (array_combine($html_dir, $html_array) as $html_dirr => $html_arrays)
            {   
                
                    echo ('<a href="'.$html_dirr.'">'.$html_arrays.'</a>');
                
                echo "<br>";
            }
            echo "</p>";
        }

Upvotes: 0

Barmar
Barmar

Reputation: 781210

Don't put <br> at the end of each echo. Do it after the inner loop so all links from the same directory will be on the same line.

    foreach($prefixDir as $dir_files)
    {
        $secondDir = $dir . $dir_files; 
        //$finalDir=scandir($secondDir);
        if((is_dir($secondDir)))
        {
            $finalDir=preg_grep('~\.(html|jpg)$~', scandir($secondDir));
            $i = 0;
            foreach($finalDir as $lastDir)
            {
                if(strpos($lastDir, "HTML5.jpg") !== false)
                {
                    echo'<a href="'.$secondDir.'/'.$lastDir.'">'.$lastDir.'</a>';
                    //variable to store list of dir
                    $html=$lastDir;
                } else if(strpos($lastDir, "HTML5.html") !== false )
                {
                    echo'<a href="'.$secondDir.'/'.$lastDir.'">'.$lastDir.'</a>';
                    //variable to store the list of dir
                    $htmlBackup=$lastDir;
                }
            }
            echo "<br>";
        }
    } 

Upvotes: 1

Related Questions