Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6140079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:04:18+00:00 2026-05-23T18:04:18+00:00

I am trying to delete an array of ids and when it gets deleted

  • 0

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;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:04:19+00:00Added an answer on May 23, 2026 at 6:04 pm

    You have a few problems in your code:

    1. $uploadedfile is never declared but it is used to find the file path. I assume this is the same as $getdeleted.
    2. You have a foreach loop around the elements in your array which will pick up each element in turn. However you model function deleteGreetings takes 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.
    3. Only at the end of your controller do you check if your cid param is null … what is the point? You should check this first before trying to run any of the other code.

    I would do something like this:

    $arrayIDs = JRequest::getVar ( 'cid', null, 'default', 'array' );
    if ($arrayIDs === null) { //Make sure the cid parameter was in the request
      JError::raiseError ( 500, 'cid parameter missing from the request' );
    }
    $model = & $this->getModel ( 'greetings' );
    jimport ( 'joomla.filesystem.file' );
    if (is_array ( $arrayIDs ) && count ( $arrayIDs ) > 0) {
      $del = $model->deleteGreetings ( $arrayIDs );
      // check this outside the loop, if it is inside you are checking it for 
      // each element in the array. Here we check once and then go forward.
      if ($del) {
        foreach ( $arrayIDs as $k => $id ) {
          $uploadedfile = $model->getUploadpic ( $id );
          $deletefile = JPATH_COMPONENT . DS . "uploads" . DS . $uploadedfile;
          JFile::delete($deletefile);
          //unlink ( $deletefile );
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to delete every follower from an array using PHP. However I
I'm trying to delete several working copy directories, but I get an Access Denied
i am trying to delete some files using a batch file.. (winxp) my problem
I'm trying to build an array in javascript that contains a list of ids.
I am trying to delete certain key/value pairs from a hash, but I get
i am trying to add and delete records(games) from my database using jquery and
I am on my 2nd day(16th hour) of trying to get my delete button
I am trying to delete multiple record(s) by using codeigniter $this->db->delete() and $this->db->where() I
I'm trying to loop through an array and delete and skip the elements until
I'm trying to delete elements from a multidimensional-array based on a value. In this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.