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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nan
Forum Newbie
Posts: 3
Joined: Thu May 14, 2009 7:12 am

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

Post 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']);
}
Last edited by Benjamin on Thu May 14, 2009 12:20 pm, edited 1 time in total.
Reason: Changed code type from text to php.
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

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

Post by lettie_dude »

Try changing this bit of code as below:

Code: Select all

$currentTopicId = findTheParent($topicId);  
echo "returned:".$currentTopicId;
nan
Forum Newbie
Posts: 3
Joined: Thu May 14, 2009 7:12 am

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

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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']);
}
nan
Forum Newbie
Posts: 3
Joined: Thu May 14, 2009 7:12 am

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

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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
Post Reply