开发者

Foreach loop not doing what it looks like it should - or perhaps it's in called function? PHP

开发者 https://www.devze.com 2023-02-23 14:06 出处:网络
I have a fairly simple function that deletes a private message from a user\'s in or sent box: public function delete($messageID) {

I have a fairly simple function that deletes a private message from a user's in or sent box:

public function delete($messageID) {
    $type = $this->findType($messageID);
    if ($type == 'in') {
        foreach ($this->inArr as $key => $value) {
            if ($this->inArr[$key]->messageID != $messageID)
                $implodeData[$key] = $this->inArr[$key]->messageID;
            }
        if (!isset($implodeData))
            $imploded = '0';
        else
            $imploded = implode(',', $implodeData);
        $result = $this->_database->updatePMUser('inArr', $imploded, 'UID', $this->UID);
        $result2 = $this->_database->deletePM('messageID', $messageID);
        return;
        }
    else {
        foreach ($this->sentArr as $key => $value) {
            if ($this->sentArr[$key]->messageID != $messageID)
                $implodeData[$key] = $this->sentArr[$key]->messageID;
            }
        if (!isset($implodeData))
            $imploded = '0';
        else
            $imploded = implode(',', $implodeData);
        $result = $this->_database->updatePMUser('sentArr', $imploded, 'UID', $this->UID);
        $result2 = $this->_database->deletePM('messageID', $messageID);
        return;
        }
    }

I realize that I can reduce this code in half by working off the type variable, I'm going to do that, but that's not the concern at the moment. My issue is that this function works wonderfully when it is called just once by the delete function at the bottom of a view message page but when it is called multiple times in succession by a foreach loop that deletes "checked" message straight from the in or sent box - it does some odd things. If I check one message at a time and delete, there aren't problems - if I check two or more, it fully works on the first message it deletes and on the subsequent ones it only deletes the actual message (the call to deletePM) but it does not remove the messageID value from the user's database row (which is what the entire first section does, this is the call to updatePMUser).

At first I thought it was a caching issue, but I've resolved that and still no go. It should do everything the exact same way as the checkbox deletes work on a foreach loop in a different class - 开发者_如何学编程all the inputs are the same, it's just called a few times one right after the other, fully returning, and then looping. Here's the code of the calling method:

private function _processSelectedSent() {
    $pmObj = unserialize(base64_decode($_POST['pmObj']));
    $i=1;
    foreach ($_POST as $key => $value) {
        if ($value == 'marked') {
            $checkedArray[$i] = $key;
            $i++;
            }
        }
    if ($_POST['submitter'] == 'Delete Selected') {
        if (is_array($checkedArray)) {
            foreach ($checkedArray as $key => $value)
                $pmObj->delete($value);
            }
        else
            $pmObj->delete($checkedArray[1]);
        header("Location: ".HOME_PAGE.PM_PAGE."?view=sentbox&nocache=".time());
        }
    }

There is one of those for the sentbox processing and one for the inbox processing, both are functionally identical for our intents and purposes. Oh, and I've var_dumped the values of the checkedArray and such, they are all good so the loops should be getting proper inputs. Can anyone see where the hiccup is? Thanks.


Nevermind, I figured it out. It was very simple, I just needed to delete the same value from the array that I was deleting from the database - it was always working on the same object so that variable never changed and each subsequent call once again saw previously deleted messages as valid messages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号