Page 1 of 1

function Return value is not assigning to a variable !!!

Posted: Thu May 14, 2009 7:18 am
by nan
Hi all,
I have a strange problem,
I am calling 'findTheParent' function from 'nextPrimaryTopic' function,
The function findTheParent should return a value to 'nextPrimaryTopic' function,
But it is not happening so, While echo " findTheParent($topicId)" in 'nextPrimaryTopic' function, it echos but it does not assign the value to the variable. Could you please help me crack this?

Code: Select all

function nextPrimaryTopic($topicId){
if(isItaParent($topicId)){
        $currentTopicId = $topicId; 
    }
    else{
        $currentTopicId = findTheParent($topicId);  
        echo "returned:",findTheParent($topicId);
    }
}
 
function findTheParent($topicId){
// to find the parent of a given topic which is already proven as a child
    $sql = "select id, parent_id  from topics where id=".$topicId; 
    $conn = getConnection();
    $db = getDb();
    $result = mysql_query($sql, $conn) or die ("Sorry, Could not execute the query. ".mysql_error());
    $parentRec = mysql_fetch_array($result);
    
    if($parentRec['parent_id'] == 0){
        echo $parentRec['id'];
        return $parentRec['id'];
    }
    findTheParent($parentRec['parent_id']);
}

Re: function Return value is not assigning to a variable !!!

Posted: Thu May 14, 2009 7:44 am
by lettie_dude
Try changing this bit of code as below:

Code: Select all

$currentTopicId = findTheParent($topicId);  
echo "returned:".$currentTopicId;

Re: function Return value is not assigning to a variable !!!

Posted: Thu May 14, 2009 8:01 am
by nan
Hi Thank you,
But still it is not working. Echoing is not a problem at all .
$currentTopicId = findTheParent($topicId);

Here $currentTopicId, does not contain any value at all
But when I say
echo findTheParent($topicId);
this is display the value.
is it not strange?

Re: function Return value is not assigning to a variable !!!

Posted: Thu May 14, 2009 11:58 am
by Mark Baker

Code: Select all

 
function findTheParent($topicId){
// to find the parent of a given topic which is already proven as a child
    $sql = "select id, parent_id  from topics where id=".$topicId; 
    $conn = getConnection();
    $db = getDb();
    $result = mysql_query($sql, $conn) or die ("Sorry, Could not execute the query. ".mysql_error());
    $parentRec = mysql_fetch_array($result);
    
    if($parentRec['parent_id'] == 0){
        echo $parentRec['id'];
        return $parentRec['id'];
    }
    return findTheParent($parentRec['parent_id']);
}

Re: function Return value is not assigning to a variable !!!

Posted: Fri May 15, 2009 5:22 am
by nan
Hi Thanks a llllllooooottt,
I was struggling all yesterday...
It was a lovely thing to get it corrected this morning...

But I didnot understand the difference, Could u please explain me this..??

Cheers

Re: function Return value is not assigning to a variable !!!

Posted: Fri May 15, 2009 6:05 am
by Mark Baker
nan wrote:But I didnot understand the difference, Could u please explain me this..??
The difference was simple:
Your code

Code: Select all

findTheParent($parentRec['parent_id']);
Correct code

Code: Select all

return findTheParent($parentRec['parent_id']);
Calling a function that is expected to return a value is no different when called recursively: you still need to return the value. If the value isn't passed back up the recursion chain, you aren't going to get anything back