Virtual folders

problem with subfolders and counting

Page 1 of 1

1 Replies - 1401 Views - Last Post: 04 August 2008 - 03:09 AM Rate Topic: -----

#1 Evilish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-July 07

Virtual folders

Posted 04 August 2008 - 02:25 AM

I have a web gallery system setup on my site. It supports having 'folders' that don't actually exist, their just data in a mysql table.

Each folder now also has a counter above it to show how many images are in that folder and all sub folders. Because folders only contain their parent folder, and not their children folder, every folder is updated at the same time once a week.

For awhile now, it's seemed to work fine. However as the folder structure got more complex, and there was more subfolders in each other, image counting no longer works.

		  //Update folder image count
		  //Reset all image counters
	  mysql_query("UPDATE ".$prefix."folders SET files = 0");
	  $res=mysql_query("SELECT * FROM ".$prefix."folders");
		  //Loop through all folders
	  while($folder=mysql_fetch_assoc($res)){
			//Get image count
		$res2=mysql_query("SELECT id FROM ".$prefix."files WHERE folder = ".$folder["id"]."");
		$folder["files"]+=mysql_num_rows($res2);
				//Add it to current folder
		mysql_query("UPDATE ".$prefix."folders SET files = ".$folder["files"]." WHERE id = ".$folder["id"]."");
		if($folder["parent"]!=-1){
		  $fol["parent"]=$folder["parent"];
				  //-1 is the root folder, so while we're not at it, we go up the subfolders and add to each one.
		  while($fol["parent"]!=-1){
			$res2=mysql_query("SELECT * FROM ".$prefix."folders WHERE id = ".$fol["parent"]."");
			if(mysql_num_rows($res2)){
			  mysql_query("UPDATE ".$prefix."folders SET files = ".(mysql_result($res2,0,"files")+$folder["files"])." WHERE id = ".$fol["parent"]."");
			  $fol["parent"]=mysql_result($res2,0,"parent");
			}
			else{
			  $fol["parent"]=-1;
			}
		  }
		}
	  }
	  echo "Update folder image count<br />";


Now I know it's not the most efficient of code (having arrays of each value would of made it 100x quicker). The problem is some folders get absolutely no responses from their subfolders. However I'm not sure if it's not working because of too many mysql queries (the table currently holds ~200 folders)

Root / Folder 1 / Folder 2 / Folder 3 / Folder 4 / Folder 5

Folder 1 shows the proper amount.
Folder 2 shows the proper amount
Folder 3 shows it's own amount and folder 5's, skipping 4.
Folder 4 shows the proper amount.
Folder 5 shows the proper amount.

Note that I'm not 100% sure about folder 1, as the image count is rounded to the nearest thousand.

Any help or theories would be appreciated, I would post a link to the site but the error is occuring in the adult content section and I don't quite know the rules about linking to that on these forums.

Is This A Good Question/Topic? 0
  • +

Replies To: Virtual folders

#2 Evilish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-July 07

Re: Virtual folders

Posted 04 August 2008 - 03:09 AM

Eh, nevermind. The error was caused by having the original $folder's amount not being updated. So values would be reset back to zero depending on the order it was processed.

I switched to using arrays and it's all fixed. Sorry for that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1