Jump to content

Simple function - seems to work differently on subsequent calls...


linus72982

Recommended Posts

but never on the first call.  Okay, I posted this on StackOverflow but no one there seems to have an answer either.  I have this simple function:

 

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;
		}
	}

 

It is a delete function for a private messaging program for a forum script I'm writing.  Anyway, here's the issue - it works!  But only sometimes.  It is called in 3 different places, always from a form processing class I have, once in the view message section to delete a message you're viewing, in a foreach from the sentbox options section and then a foreach in the inbox options section.  The inbox and sentbox option sections do that whole "delete the checked messages" for the mass removal functionality.  The delete function above works in all ways shapes and forms when I use it in single calls - like when I'm deleting a message while viewing it or when I only check one message from the inbox, etc - but when I call it multiple times (as in I have checked multiple messages) - it fully deletes one (both the message and the reference to the message in the user's db row) and then only deletes the actual message on the others (deleting the message is the call to deletePM - deleting the reference is the call to updatePMUser).

 

Okay, if you need further information - the function above checks the type the message is (in the inbox or in the sentbox) and then uses that to foreach through that array (inArr or sentArr) of the user.  It logs in all the messageIDs of the those that DON'T match the one we're deleting and then at the end it implodes those caught IDs into a string that is then updated in the user's row as a comma separated string of values each representing a message in the DB - you get the picture.  I realize I have some trimming to do (for one I can cut the above function down by about half by using variable variables) but I'll get to that after I get the thing working.  I can't figure out why it's doing what it's doing.  If you need the function that calls this in the foreach, I have it below.  Oh, and the thing that really boggles me is that this function is called fully for each checked message in the foreach - it fully returns and then loops - if it works once, I don't see how it wouldn't work on a second call from a loop - the variables it uses should be trashed when it leaves the function, they aren't global or object properties or anything.  Color me confused.

 

Thanks for any help, oh, here's one of the functions that calls it to delete checked messages (this one is for the sentbox, there is another for the inbox):

 

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());
		}
	}

Link to comment
Share on other sites

How about adding all the curly brackets to your functions and if/else statements.

 

myFunction($string,$functions) {
    $funcs = explode(",",$functions);

    foreach ($funcs as $func)  {
        if (function_exists($func)) {
    $string = $func($string);
    } else {
    $string = trim($string);
    }

    return $string;
}

 

Then carefully look through them to make sure in fact that rules reside within the specified amount of brackets under certain conditions.

 

Place an echo in each of your statements and see what it's actually doing, it seems the else statements are executing as well as the if's under certain conditions of your function.

 

Link to comment
Share on other sites

I was under the impression that if brackets were excluded that the if or foreach or whatever it is only looks at the next line so they aren't needed for one line conditionals and the like?

 

I thought that's how it worked.  But anyway, yeah, I'll have to go in and inject some troubleshooting hints along the way to see what's going on.  I did that a bit and found nothing, I guess I need to go back and check everything.

Link to comment
Share on other sites

Frankly, without enough of the code, (sample) data, and knowledge of the data structures to simulate/reproduce the problem, there's not much anyone here or in any other forum can do to help. You're the only one here who has the ability to troubleshoot what the code/data is doing and to pin down at what point the code/data is doing what you expect and at what point it is not.

Link to comment
Share on other sites

About the only things I can suggest - are you debugging this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed and do you have error checking and error reporting/logging logic in your code to test if database queries are executing with/without errors and if rows are actually being updated/deleted as the result of those queries?

Link to comment
Share on other sites

Yep, I figured it out by going crazy on echoing and var_dumping values.  It just turns out that I was deleting the reference to the message in the database, but I would add it right back in when it was called again because I never deleted the variable in the array that pointed to it.  Each time it ran it would see that message as valid and it had already been deleted and since the script "makes" the new comma strings from scratch and uploads them, they would reappear in further calls.

 

Thanks for the help.  I know it was a hard one without a bunch more code but I was pulling my hair out and stackoverflow had failed me, etc - it was a desperation move :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.