oliverbj
oliverbj

Reputation: 6062

php - return variable from function

I am trying to return a variable from a function. I have the function below, which inserts a postid to the database. I need the postid's value returned to another page.

forum.php - Where the function is.

function newTopic(){
    // Get the POST data
    global $ir;

    $postid = mysql_insert_id();

    mysql_query("UPDATE forum_topics SET post_id='$postid' WHERE topic_id='$topicid'");

    // No error found and the update was succesful - Return success!            
    return 100;
    return $postid;
}

newtopic.php - Where I need the $postid variable.

if($_POST)
{     
    $newTopic = $forum->newTopic();    

        /*
         * Return codes:
         * 100: Success
         */       
    switch($newTopic)
    {
        //If no error = success.    
        case 100:
            $success = 'You have successfully created the topic.';
            $issuccess = 1;
            $stop = true;
        break;
    }

    $checkerror = $error; 
    $checksuccess = $success;

} 

if($checksuccess){ 
    $contents.="
    ".alert("success","$success")."";
    refresh("3","/forum/t$id-$postid");
}

As you can see, I am trying to use $postid variable from the function newTopic(). Although, the $postid variable is empty.

How can I get the value from the function newTopic.php located in forum.php, to newtopic.php?

Upvotes: 0

Views: 428

Answers (4)

A. Zalonis
A. Zalonis

Reputation: 1609

try with reference like in the below code

function newTopic(&$postid){
    // Get the POST data
    global $ir;

    $postid = mysql_insert_id();

    mysql_query("UPDATE forum_topics SET post_id='$postid' WHERE topic_id='$topicid'");

    // No error found and the update was succesful - Return success!            
    return 100;
}

....... //some codes
$postid = null;
newTopic($postid);
$my_postId = $postid; //Now you have your post ID 

OR in you exists code like

if($_POST)
{ 
    $last_postid = null;    
    $newTopic = $forum->newTopic($last_postid );    

        /*
         * Return codes:
         * 100: Success
         */       
    switch($newTopic)
    {
        //If no error = success.    
        case 100:
            $success = 'You have successfully created the topic.';
            $issuccess = 1;
            $stop = true;
            $postid = $last_postid;
        break;
    }

    $checkerror = $error; 
    $checksuccess = $success;

} 

if($checksuccess){ 
    $contents.="
    ".alert("success","$success")."";
    refresh("3","/forum/t$id-$postid");
}

EDIT: Call-time pass-by-reference fixed.

Upvotes: 1

Drk_alien
Drk_alien

Reputation: 307

Your code looks that is returning value 100 before returning $postid variable. That's wrong.Your code will exit the function at first return.

comment the line "//return 100;"

OR return an array. You cannot return two values like u do. Instead of doing

 return 100;
 return $postid;

Do

 //return 100;
 return array("successs"=>100,"id"=>$postid);

Then use your $newTopic variable as folows: In switch:

switch($newTopic['success'])

Use the postId anywhere else

$newTopic['id']

Upvotes: 1

Jimmy T.
Jimmy T.

Reputation: 4190

If you want to return two values you can not just write two returns.

There are several options to return more values. One is using an array:

return array(100, $postId);

and

list($status, $postId) = $forum->newTopic();

You can also use an associatve array as s.d suggested.

However, as your one variable only contains the status you can also use exceptions in the case the operation fails.

Upvotes: 0

s.d
s.d

Reputation: 255

When you have used

return 100;

your code will never see

return $postid;

You can use this code to return

return array("code"=>"100","postid"=>$postid);

Now in new_topic.php use the code as shown

if($_POST)
{

    $newTopic = $forum->newTopic();


        /*
         * Return codes:
         * 100: Success
         */

    switch($newTopic['code'])
    {

            //If no error = success.    
            case 100:
                $success = 'You have successfully created the topic.';
                $issuccess = 1;
                $stop = true;
            break;
    }

    $checkerror = $error; 
    $checksuccess = $success;

} 

        if($checksuccess){ 
        $contents.="
        ".alert("success","$success")."";
        refresh("3","/forum/t$id-$newTopic['postid']");
        }

Upvotes: 4

Related Questions