Jump to content

[SOLVED] Function not returning array


cmgmyr

Recommended Posts

Why is this function not returning my array? When I print it out within the function it works fine.

 

<?php
function getChildMess($mid, $userid, $x=0){
	global $db, $bug;
	//echo "mid: $mid, userid: $userid, x: $x <br />";
	$new_sql = "SELECT * FROM mail2 WHERE (`to` = $userid OR `from` = $userid) AND `status` != 2 AND `parentid` = $mid LIMIT 1";
	if($db->numRowsQ($new_sql) > 0){
		$new_result = $db->query($new_sql);
		while($new = $db->fetch($new_result)){
			$new = $this->cleanArray($new);

			$status = $new['status'];
			$date = $new['date_sent'];
			$mid = $new['mid'];
			$message = $this->shortDesc($new['message'], 50);
		}
		$x++;
		$this->getChildMess($mid, $userid, $x);
	}else{
		if($x > 0){
			$new_sql = "SELECT * FROM mail2 WHERE (`to` = $userid OR `from` = $userid) AND `status` != 2 AND `mid` = $mid LIMIT 1";
			if($db->numRowsQ($new_sql) > 0){
				$new_result = $db->query($new_sql);
				while($new = $db->fetch($new_result)){
					$new = $this->cleanArray($new);

					$status = $new['status'];
					$date = $new['date_sent'];
					$message = $this->shortDesc($new['message'], 50);

					return array("status" => $status, "date" => $date, "message" => $message);
				}
			}
		}
	}
?>

 

...and this doesn't print anything

<?php 

$new_array = $func->getChildMess(1, 1);

echo "<pre>";
print_r($new_array);
echo "</pre>";

?>

 

also...if there is an easier way to do this. please feel free to share. I'm trying to get the last message in a conversation.

 

Thanks,

-Chris

Link to comment
Share on other sites

i'm going to guess it's because you never actually return anything, as far as i can tell.  while you will return the last value, the recursion won't return anything.  you'll pass that last $x run upward, but it will stop right there.  you need to return the recursion (i think):

 

		if($db->numRowsQ($new_sql) > 0){
		$new_result = $db->query($new_sql);
		while($new = $db->fetch($new_result)){
			$new = $this->cleanArray($new);

			$status = $new['status'];
			$date = $new['date_sent'];
			$mid = $new['mid'];
			$message = $this->shortDesc($new['message'], 50);
		}
		$x++;
		return $this->getChildMess($mid, $userid, $x);

 

also, unless i've missed something here, your logic will just run indefinitely.  you don't use $x to discriminate at the start, so it'll keep running the same query (ie. getChildMess(1, 1) will run the same query as getChildMess(1, 1, 1) because the first if() conditional will always be the same value, as the query doesn't depend on $x).

Link to comment
Share on other sites

I'm trying to make a "facebook" type messaging system. In your inbox you have conversations, if there has been a newer post to the conversation this function is supposed to overwrite the old information with the new. So pretty much taking the child message instead of the parent.

Link to comment
Share on other sites

at the very bottom of the script you will see

return array("status" => $status, "date" => $date, "message" => $message);

 

This repeats the function if there is another newer message

$this->getChildMess($mid, $userid, $x);

The $x is only there to see if there is more then 1 newer message...if there isn't it will just take the older (parent) information (not shown)/

 

Thanks

Link to comment
Share on other sites

Surely you'll need to return on this line:

$this->getChildMess($mid, $userid, $x);

 

as akitchin said.

 

When PHP calls the new instance of the getChildMess function PHP will run through that function again and again causing an infinity recursion.

 

I cannot see any logic

Link to comment
Share on other sites

i see what you mean about the $x variable; i didn't notice you were running the function with the new $mid.  recursion isn't the issue (obviously you'd be timing out otherwise).

 

the problem is that if you have a matching row and run the getChildMess() again with the new $mid, the "parent" getChildMess() does nothing with the value returned from that getChildMess() called with the child $mid.  you have to return it from the parent instance in order to pass it right up the line to the original "caller."

Link to comment
Share on other sites

hmm that second return did the trick. Thanks! Here is the updated code.

 

<?php
function getChildMess($mid, $userid, $x=0){
	global $db;
	$new_sql = "SELECT * FROM mail2 WHERE (`to` = $userid OR `from` = $userid) AND `status` != 2 AND `parentid` = $mid LIMIT 1";
	if($db->numRowsQ($new_sql) > 0){
		$new_result = $db->query($new_sql);
		while($new = $db->fetch($new_result)){
			$new = $this->cleanArray($new);

			$status = $new['status'];
			$date = $new['date_sent'];
			$mid = $new['mid'];
			$message = $this->shortDesc($new['message'], 50);
		}
		$x++;
		return $this->getChildMess($mid, $userid, $x);
	}else{
		if($x > 0){
			$new_sql = "SELECT * FROM mail2 WHERE (`to` = $userid OR `from` = $userid) AND `status` != 2 AND `mid` = $mid LIMIT 1";
			if($db->numRowsQ($new_sql) > 0){
				$new_result = $db->query($new_sql);
				while($new = $db->fetch($new_result)){
					$new = $this->cleanArray($new);

					$status = $new['status'];
					$date = $new['date_sent'];
					$message = $this->shortDesc($new['message'], 50);

					return array("status" => $status, "date" => $date, "message" => $message);
				}
			}
		}
	}		
}
?>

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.