Jump to content

Read actual folders and compare with Database


Ferdi22

Recommended Posts

Hello,

 

Sorry for my english...

 

Well, I make a smaill website. members has to login and then  member can see pictures (saved in a folder). When the admin upload pictures a folder wil be created. To get the actual folders in the "album" folder I use this code.

 

<?php
//get the folders
$handle = opendir(realpath('../album/Album/.'));

while ($direc = readdir($handle)) {
   if($direc == '.' || $direc == '..')
      continue;
   else {
      echo "\t\t<input type='checkbox' name='map[]' id='map[]' value='" . $direc. "' />" . $direc . "<br />\n"; 
   }
}
closedir($handle); 
?> 

 

The admin has to assign the folders wich are allowed to see by a member. I use checkboxes for that.

And now my problem: When a member can see folder 1 to 4 and the admin upload 2 new folders and want to give that member to see these 2 new folders I want the following:

6 checkboxes (checkbox 1 > album 1, checkbox 2 > album 2... etc). As is said, this member can see folder 1 to 4, so in the CMS album 1 to 4 has to be checked ans album 5 to 6 not. This is my code for that:

 

<?php
$sql2 = "SELECT url FROM url WHERE gebruiker_id =" . $_GET['id'] . " ORDER BY url ASC";
	$res2 = mysql_query($sql2);
	while ($rij = mysql_fetch_array($res2)) {
		$url = $rij['url'];
		// mappen ophalen
		$handle = opendir(realpath('../album/Album/.'));
		while ($direc = readdir($handle)) {
		if($direc == '.' || $direc == '..' || $direc == $url) {
			continue;
			}
				echo "\t\t<input type='checkbox' name='map[]' id='map[]' value='" . $direc. "' />" . $direc . "<br />\n"; 
		}
		closedir($handle); 
	}
	?>

 

Now  my output is:

 

<input type='checkbox' name='map[]' id='map[]' value='album 2' />album 2<br />

<input type='checkbox' name='map[]' id='map[]' value='album 3' />album 3<br />

 

<input type='checkbox' name='map[]' id='map[]' value='album 4' />album 4<br />

<input type='checkbox' name='map[]' id='map[]' value='album 5' />album 5<br />

<input type='checkbox' name='map[]' id='map[]' value='album 6' />album 6<br />

<input type='checkbox' name='map[]' id='map[]' value='album 1' />album 1<br />

<input type='checkbox' name='map[]' id='map[]' value='album 2' />album 2<br />

<input type='checkbox' name='map[]' id='map[]' value='album 4' />album 4<br />

 

<input type='checkbox' name='map[]' id='map[]' value='album 5' />album 5<br />

<input type='checkbox' name='map[]' id='map[]' value='album 6' />album 6<br />

<input type='checkbox' name='map[]' id='map[]' value='album 1' />album 1<br />

<input type='checkbox' name='map[]' id='map[]' value='album 2' />album 2<br />

<input type='checkbox' name='map[]' id='map[]' value='album 3' />album 3<br />

<input type='checkbox' name='map[]' id='map[]' value='album 5' />album 5<br />

 

<input type='checkbox' name='map[]' id='map[]' value='album 6' />album 6<br />

 

 

What do I have to do to check the checkboxes (album 1-4) and not to check the album 5-6???

My guess it has to do with the While in While loop but...

 

Greetings,

Ferdi

Link to comment
Share on other sites

in the while loop you'll need an if statement checking whether the user is permitted to see that folder or not and then add checked="yes" to the checkbox input tag. Not exactly sure how you're DB is set up to correlate with the folders,

i.e.

//check if the user is granted permission to see folder
if ($checkboxGranted==true) {
echo "\t\t<input type='checkbox' checked='yes' name='map[]' id='map[]' value='" . $direc. "' />" . $direc . "<br />\n"; 
} else {
echo "\t\t<input type='checkbox' name='map[]' id='map[]' value='" . $direc. "' />" . $direc . "<br />\n"; 
}

 

Link to comment
Share on other sites

Hello Joel24,

 

Thx for your fast reply!!

 

Well it didn't work out (yet)

 

My DB:

I got a table "Users" and a Table "url"

My "url":

id

user_id

url

 

In the "url" are the folders named wich are connected with the user_id and the id from the table "Users" (I hope you understand).

 

This is my code:

 

// Get directory's
$handle = opendir(realpath('../album/Album/.'));
while ($direc = readdir($handle)) { //Get the directory's in the Album folder
$sql2 = "SELECT url FROM url WHERE gebruiker_id =" . $id . " ORDER BY id ASC"; // Get the directory's in the db from the user
$res2 = mysql_query($sql2);
while ($rij = mysql_fetch_array($res2)) {
	$url = $rij['url'];
	if($direc == '.' || $direc == '..' || $direc == 'res' || $direc == 'manifest.jmf' || $direc == 'lifeboat.zip' || $direc == 'index.html' || $direc == 'folderimage.jpg') { // Exclude some files to show

	} else {
		if ($direc == $url) { // When actual directory is the same as the folder
			$checked = "checked";
		} else {
			$checked = "";
		}
			echo "\t\t<input type='checkbox' name='map[]' id='map[]' value='" . $direc. "' " . $checked . " />" . $direc . "<br />\n"; // Show it!
	}
}
}
closedir($handle);
?>

 

My output is:

 

<input type='checkbox' name='map[]' id='map[]' value='050218_Rai_auto' checked />050218_Rai_auto<br />

<input type='checkbox' name='map[]' id='map[]' value='050218_Rai_auto'  />050218_Rai_auto<br />

<input type='checkbox' name='map[]' id='map[]' value='050218_Rai_auto'  />050218_Rai_auto<br />

<input type='checkbox' name='map[]' id='map[]' value='050218_Rai_auto'  />050218_Rai_auto<br />

<input type='checkbox' name='map[]' id='map[]' value='050218_Rai_auto'  />050218_Rai_auto<br />

<input type='checkbox' name='map[]' id='map[]' value='050304 Citroen'  />050304 Citroen<br />

<input type='checkbox' name='map[]' id='map[]' value='050304 Citroen' checked />050304 Citroen<br />

<input type='checkbox' name='map[]' id='map[]' value='050304 Citroen'  />050304 Citroen<br />

<input type='checkbox' name='map[]' id='map[]' value='050304 Citroen'  />050304 Citroen<br />

<input type='checkbox' name='map[]' id='map[]' value='050304 Citroen'  />050304 Citroen<br />

<input type='checkbox' name='map[]' id='map[]' value='050429 Matiz'  />050429 Matiz<br />

<input type='checkbox' name='map[]' id='map[]' value='050429 Matiz'  />050429 Matiz<br />

<input type='checkbox' name='map[]' id='map[]' value='050429 Matiz' checked />050429 Matiz<br />

<input type='checkbox' name='map[]' id='map[]' value='050429 Matiz'  />050429 Matiz<br />

<input type='checkbox' name='map[]' id='map[]' value='050429 Matiz'  />050429 Matiz<br />

<input type='checkbox' name='map[]' id='map[]' value='BMW M3'  />BMW M3<br />

<input type='checkbox' name='map[]' id='map[]' value='BMW M3'  />BMW M3<br />

<input type='checkbox' name='map[]' id='map[]' value='BMW M3'  />BMW M3<br />

<input type='checkbox' name='map[]' id='map[]' value='BMW M3' checked />BMW M3<br />

<input type='checkbox' name='map[]' id='map[]' value='BMW M3'  />BMW M3<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2000'  />Honda Legend 2000<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2000'  />Honda Legend 2000<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2000'  />Honda Legend 2000<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2000'  />Honda Legend 2000<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2000' checked />Honda Legend 2000<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2004'  />Honda Legend 2004<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2004'  />Honda Legend 2004<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2004'  />Honda Legend 2004<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2004'  />Honda Legend 2004<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2004'  />Honda Legend 2004<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 3.2'  />Honda Legend 3.2<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 3.2'  />Honda Legend 3.2<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 3.2'  />Honda Legend 3.2<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 3.2'  />Honda Legend 3.2<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 3.2'  />Honda Legend 3.2<br />

 

As you can see the directory's "050218_Rai_auto", "050304 Citroen", "050429 Matiz", "BMW M3" and "Honda Legend 2000" are permitted. The directory's "Honda Legend 2004" and "Honda Legend 3.2" are not permitted (This user cannot see these directory's). This is ok! :)

 

But why this long list?? I can't figure it out. Because every directory is printed 5 times it would be in the second while loop... (User is permitted to acces 5 directory's). How can I just print is one time as:

 

<input type='checkbox' name='map[]' id='map[]' value='050218_Rai_auto' checked />050218_Rai_auto<br />

<input type='checkbox' name='map[]' id='map[]' value='050304 Citroen' checked />050304 Citroen<br />

<input type='checkbox' name='map[]' id='map[]' value='050429 Matiz' checked />050429 Matiz<br />

<input type='checkbox' name='map[]' id='map[]' value='BMW M3' checked />BMW M3<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2000' checked />Honda Legend 2000<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 2004'  />Honda Legend 2004<br />

<input type='checkbox' name='map[]' id='map[]' value='Honda Legend 3.2'  />Honda Legend 3.2<br />

 

Any tips are more than welcome! :)

 

Greetings,

Ferdi

 

Link to comment
Share on other sites

it's because you are running a while loop inside a while loop.

so each time the first loop iterates, the 2nd while loop goes through a full cycle, and again for the 2nd iteration of the first loop etc.

 

pull out the values from the DB into an array, and then use in_array to check if that folder is permitted...

something like this, not tested though

$permitted = array();
$sql2 = "SELECT url FROM url WHERE gebruiker_id =" . $id . " ORDER BY id ASC"; // Get the directory's in the db from the user
$res2 = mysql_query($sql2);
while ($row = mysql_fetch_array($res2))
{
    $permitted[] = $row['url'];
}

// Get directory's
$handle = opendir(realpath('../album/Album/.'));
while ($direc = readdir($handle)) { //Get the directory's in the Album folder


if($direc == '.' || $direc == '..' || $direc == 'res' || $direc == 'manifest.jmf' || $direc == 'lifeboat.zip' || $direc == 'index.html' || $direc == 'folderimage.jpg') { // Exclude some files to show

} else {

if (in_array($permitted, $direc)) { // When actual directory is the same as the folder

$checked = "checked";

} else {


$checked = "";
}

echo "\t\t<input type='checkbox' name='map[]' id='map[]' value='" . $direc. "' " . $checked . " />" . $direc . "<br />\n"; // Show it!


}

}

closedir($handle);

Link to comment
Share on other sites

Hi Joel24,

 

Thx!! it works!!  :D

 

Here's the code:

 

<?php
// Get directory's
$permitted = array();
$sql2 = "SELECT url FROM url WHERE gebruiker_id =" . $id . " ORDER BY id ASC"; // Get the directory's in the db from the user
$res2 = mysql_query($sql2);
while ($rij = mysql_fetch_array($res2)) {
	$permitted[] = $rij['url'];
}		

$handle = opendir(realpath('../album/Album/.'));
while ($direc = readdir($handle)) { //Get the directory's in the Album folder
	if($direc == '.' || $direc == '..' || $direc == 'res' || $direc == 'manifest.jmf' || $direc == 'lifeboat.zip' || $direc == 'index.html' || $direc == 'folderimage.jpg') { // Exclude some files to show

	} else {

		if (in_array($direc, $permitted))  { // When actual directory is the same as the folder
			$checked = "checked";
		} else {
			$checked = "";
		}
			echo "\t\t<input type='checkbox' name='map[]' id='map[]' value='" . $direc. "' " . $checked . " />" . $direc . "<br />\n"; // Show it!
	}
}

closedir($handle);
?>

 

BTW: It must be "if (in_array($direc, $permitted))"  ;) and not "if (in_array($permitted, $direc))".

 

Greetings,

Ferdi

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.