Top Kenzo
Top Kenzo

Reputation: 7

Loop returning some weird dots

My dropdown with folders is returning dots even if i check if there are no one.

https://prnt.sc/q5q5rd

Code:

$d = dir("content/client_areas/");
while (false !== ($entry = $d->read()))
{
    if (is_dir($entry) && $entry != '.' && $entry != '..')
        ?><option value="<?php echo $entry;?>"><?php echo $entry;?></option><?php
}

$d->close();

I can't figure out why i get these dots.

Thanks.

Upvotes: 0

Views: 66

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

You need to construct your if correctly with braces { }. Without them, the statement after the if is not part of the if and is not constrained by the result of the expression:

if (is_dir("content/client_areas/$entry") && $entry != '.' && $entry != '..') {
    ?><option value="<?php echo $entry;?>"><?php echo $entry;?></option><?php
}

Also, $entry will not be the full path so is_dir() will always fail. Add content/client_areas/ or whatever path.

If you were to construct the output in PHP with a terminating semicolon ; it would work as expected:

if (is_dir("content/client_areas/$entry") && $entry != '.' && $entry != '..') 
    echo '<option value="'.$entry.'">'.$entry.'</option>';

Or Alternative Syntax:

if (is_dir("content/client_areas/$entry") && $entry != '.' && $entry != '..'):
    ?><option value="<?php echo $entry;?>"><?php echo $entry;?></option><?php
endif;

Upvotes: 2

Related Questions