Forum Moderators: coopster
I know this is going to be something simple, but I thought I would ask you for the best solution. :)
I am reading from a directory and displaying the results in a drop down menu. I want to compare the entries in the directory to a table in my database. If anything matches I DON`T want it to be displayed in the drop down
I`m using the following to read from the directory which works fine:
$dir = opendir("../directory/");while ($read = readdir($dir))
{
if ($read!= "." && $read!= "..") {
echo "<option value = \"" . $read . "\">" . $read . "</option>\n"; }
}closedir($dir);
So now I need to compare it to stuff in the database. Can I put a while loop inside a while loop? Because I did the following and it doesn`t work:
$dir = opendir("../directory/");while ($read = readdir($dir))
{
while ($row = mysql_fetch_array($result))
{
if ($row['blah']!= $read) {
if ($read!= "." && $read!= "..") {
echo "<option value = \"" . $read . "\">" . $read . "</option>\n"; }}}
}closedir($dir);
Also, on the subject of directories, is it possible to read from the contents of a directory on another server?
Thank you!
Anyway I now have this:
while ($read = readdir($dir)){
$dirfiles[] = $read;
}
closedir($dir);
while ($row = mysql_fetch_array($result))
{
foreach ($dirfiles as $file) {
if ($row['fronturl']!= $file) {
if ($file!= "." && $file!= "..") {
echo "<option value = \"" . $file . "\">" . $file . "</option>\n"; }}
}
}
Which isn`t working either. Oh well....
while ($read = readdir($dir)){
if ($read!= "." && $read!= "..") dirfiles[] = $read;
}
closedir($dir);
while ($row = mysql_fetch_array($result))
{
$dirfiles2[] = $row['fronturl'];
}
$mydirs = array_merge($dirfiles,$dirfiles2);
foreach($mydirs as $a)
{
print $a
}
When you are doing the read of your directory don't put the name into the array, but use it as the index into the array. For example:
$dirfiles = array();
while ($read = readdir($dir))
{
if ($read!= "." && $read!= "..") { $dirfiles[$read] = 1; }
}
closedir($dir);
Now in the while loop check if the array has an element with index from the SQL query.
while ($row = mysql_fetch_array($result))
{
if (!isset($dirfiles[$row['fronturl']])) {
echo "<option value = \"" . $row['fronturl'] . "\">" . $row['fronturl'] . "</option>\n"; }
}
Note that there is only one loop in the comparison operation. The other loop is actually implemented as a language feature, which means it is not interpreted.
The only limitation of this method is that all the filenames must be unique. Since they are all coming from a single directory, this shouldn't be an issue.
printf("<pre>");print_r($dirfiles);printf("</pre">);
[a filename] = 1
[another filename] = 1
[a third filename] = 1
.
.
.
[the last filename] = 1
if ($dir = opendir('.')) {
while (false!== ($file = readdir($dir))) {
if ($file!= "." && $file!= "..") {
$dirfiles[$read] = 1;
}
}
closedir($handle);
}