cmgmyr Posted August 1, 2007 Share Posted August 1, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/ Share on other sites More sharing options...
Grodo Posted August 1, 2007 Share Posted August 1, 2007 try print($new_array); Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-312981 Share on other sites More sharing options...
cmgmyr Posted August 1, 2007 Author Share Posted August 1, 2007 nope...nothing Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-313003 Share on other sites More sharing options...
Grodo Posted August 1, 2007 Share Posted August 1, 2007 What are you tring to make the script do? Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-313028 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 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). Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-313031 Share on other sites More sharing options...
cmgmyr Posted August 1, 2007 Author Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-313038 Share on other sites More sharing options...
cmgmyr Posted August 1, 2007 Author Share Posted August 1, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-313043 Share on other sites More sharing options...
wildteen88 Posted August 1, 2007 Share Posted August 1, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-313049 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 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." Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-313051 Share on other sites More sharing options...
cmgmyr Posted August 1, 2007 Author Share Posted August 1, 2007 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); } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/62875-solved-function-not-returning-array/#findComment-313073 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.