I am trying to delete an array of ids and when it gets deleted I want the uploaded pic associated with it also to get deleted using unlink. I am using joomla and mysql for the admin mvc component in joomla.
My code for controller in remove is has follows:
function remove()
{
$arrayIDs = JRequest::getVar ( 'cid', null, 'default', 'array' );
//Reads cid as an array
$model = & $this->getModel ( 'greetings' );
jimport ( 'joomla.filesystem.file' );
if (is_array ( $arrayIDs ) && count ( $arrayIDs ) > 0) {
foreach ( $arrayIDs as $k => $id ) {
$del = $model->deleteGreetings ( $arrayIDs );
if ($del) {
$getdeleted = $model->getUploadpic ( $id );
$deletefile = JPATH_COMPONENT . DS . "uploads" . DS . $uploadedfile;
unlink ( $deletefile );
}
}
}
if ($arrayIDs === null) { //Make sure the cid parameter was in the request
JError::raiseError ( 500, 'cid parameter missing from the request' );
}
$redirectTo = JRoute::_ ( 'index.php?option=' . JRequest::getVar ( 'option' ) );
$this->setRedirect ( $redirectTo, 'Deleted...' );
}
…and for the model my code is:
function deleteGreetings($arrayIDs) {
$query = "DELETE FROM #__greetings WHERE id IN (" . implode ( ',', $arrayIDs ) . ")";
$db = $this->getDBO ();
$db->setQuery ( $query );
if (! $db->query ()) {
$errorMessage = $this->getDBO ()->getErrorMsg ();
JError::raiseError ( 500, 'Error deleting greetings: ' . $errorMessage );
} else {
return TRUE;
}
}
You have a few problems in your code:
$uploadedfileis never declared but it is used to find the file path. I assume this is the same as$getdeleted.deleteGreetingstakes the entire array. Your should remove this function call from your loop else it will be called each for every element in the array. You only want to call this once.I would do something like this: