Forum Moderators: coopster

Message Too Old, No Replies

Comparing results from a directory & mysql!

some info please.....

         

dreamcatcher

11:00 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Guys,

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!

dreamcatcher

11:03 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, forget this one...

Birdman

11:14 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well come on, tell us what you did ;)

Personally, I would have put the readdir() results into an array and then done a for loop inside the whiule loop. Of course, that's probably not even close to the best solution :)

dreamcatcher

11:50 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Birdman. I was going to post something else about directories but worked that out after I posted. Thats how daft I am.

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....

Birdman

12:29 am on Jan 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't actually tested this and haven't even used the function myself but, array_merge() looks like a solution. It supposed to overwrite dupes from what I read.

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

}

BitBanger

12:53 am on Jan 27, 2004 (gmt 0)

10+ Year Member



Actually there is a really sneaky way to do this.

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.

dreamcatcher

7:11 am on Jan 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the help guys, however I`ve tried both your syntaxes and neither work I`m afraid. BitBanger, yours doesn`t display anything at all and Birdman, the array_merge just displays the info from both directories and so duplicates everything.

Got anything else I can try?

Birdman

12:33 pm on Jan 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I think I got it.

$mydirs = array_unique(array_merge($dirfiles,$dirfiles2));

BitBanger

12:50 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



Stick this right after the closedir($dir) statement:
printf("<pre>");print_r($dirfiles);printf("</pre">);

You should get an array listed that looks like this:

[a filename] = 1
[another filename] = 1
[a third filename] = 1
.
.
.
[the last filename] = 1

Also, your readdir loop needs to properly check for the end of the directory. Use this code:

if ($dir = opendir('.')) {
while (false!== ($file = readdir($dir))) {
if ($file!= "." && $file!= "..") {
$dirfiles[$read] = 1;
}
}
closedir($handle);
}

I would also echo $row['fronturl'] at the top of the mysql_fetch_array() while loop. This will let you verify that the data being returned is what you expect.

dreamcatcher

6:56 pm on Jan 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys, but after giving it some thought, I`m going to instead opt for using the unlink() function to delete the file from the directory after its added to the database. Thanks for your help though, I`ve made a note of your codes for future use. :)