CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 loop through multiple records to unlink files

Print topic Send  topic

Author Message
andy


Posts: 183
Posted: 02/03/2008, 2:56 PM

Hi
My Master table has some linked child tables (one-to-many relationship).
The master table is a user's details.
One of the child tables is a document repository containing (multiple) files for that user.

I am trying to crack the code to delete the related files as well as the chlid table records when the master record is deleted.
No problem deleting the related records using the Before Execute Delete event.
I am struggling with the loop to delete the related files.

  
global $DBlocalconnection;  
global $linguist;  
global $filename;  
global $filefolder;  
  
$lingid =$linguist->ling_id->GetValue(); // Assigns the Key field  
$db = new clsDBlocalconnection();  
$sqldelete = "DELETE from ling_adhoc WHERE userid=$lingid"; //deletes another related child table  
$db->query($sqldelete);  //deletes another related child table  
  
// Routine to get all related file attachments from ling_files_down and delete the files HELP!!  
  
$SQL = "SELECT filename FROM ling_files_down WHERE userid=".$lingid;  
$db->query($SQL);  
  
while ($db->next_record()) {  
$filename = CCDLookUp("filename","ling_files_down","userid=$lingid",$DBlocalconnection);  
$filefolder = "fromlinguists";  
$file_name = $db->f("filename");  
unlink('../../' . $filefolder . '/' . $file_name);    
}  
  
$sqldelete = "DELETE from ling_files_down WHERE userid=$lingid";  
$db->query($sqldelete);  
$db->close();  
  

Can anyone help me to loop through the child table of filenames, get each record's filename and delete that file?

_________________
Andy

RAD tools for rich UI controls:
http://www.koolphptools.com
View profile  Send private message
datadoit
Posted: 02/04/2008, 6:25 AM

In short, you're using the same $db connection to do different things,
which will cause unwanted results.

In your while() loop, create a new connection to do your lookup with:

$db2 = new clsDBlocalconnection();

Be sure to close this connection when you're done in your while() loop.

Finally, there are three database things happening in this little bit of
code, so I would make sure each are uniquely defined and used (including
the $DBlocalconnection from your page's grid).
andy


Posts: 183
Posted: 02/04/2008, 12:43 PM

Thanks datadoit for the hint. You put me on the right track and it now works fine.
This is the code:
   
global $DBlocalconnection;  
global $linguist;  
global $filename;  
global $filefolder;  
  
// Deletes related records in ling_adhoc, ling_conference and ling_translation   
	$lingid =$linguist->ling_id->GetValue();  
	$db = new clsDBlocalconnection();  
	$sqldelete = "DELETE from ling_adhoc WHERE userid=$lingid";  
	$db->query($sqldelete);  
	$sqldelete = "DELETE from ling_conference WHERE userid=$lingid";  
	$db->query($sqldelete);  
	$sqldelete = "DELETE from ling_translation WHERE userid=$lingid";  
	$db->query($sqldelete);  
	$db->close();  
  
// Routine to get all related file attachments from ling_files_down and delete the files  
	$db2 = new clsDBlocalconnection();  
	$SQL = "SELECT filename FROM ling_files_down WHERE userid=$lingid";  
	$db2->query($SQL);  
	while ($db2->next_record()) {  
	$filefolder = "filearchive/fromlinguists";  
	$file_name = $db2->f("filename");  
	unlink('../../' . $filefolder . '/' . $file_name);    
}  
	$db2->close();  
  
// Deletes related records in ling_files_down   
	$db3 = new clsDBlocalconnection();  
	$sqldelete = "DELETE from ling_files_down WHERE userid=$lingid";  
	$db3->query($sqldelete);  
	$db3->close();  
  

Thanks again.

_________________
Andy

RAD tools for rich UI controls:
http://www.koolphptools.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.