Kent Miller
Kent Miller

Reputation: 509

PHP: Getting value of a function

It's the first time I want to use a function in a PHP script. Somehow I cannot get the value returned from the function.

Here is the relevant part of my script:

// FUNCTIONS
// ---------
function getActivityName($event_types_id) {
        $query_activity_name = "SELECT * FROM event_types WHERE id=$event_types_id";
        $results_activity_name = $db->query($query_activity_name);
        while ($result_activity_name = $results_activity_name -> fetch_assoc()) {
            $activity_name = $result_activity_name['title'];
        }
        echo $activity_name;
}   

// DO SEARCH AND OUTPUT RESULTS
// ----------------------------
$results = $db -> query($query);
if (mysqli_num_rows($results) > 0) {

    while ($result = $results -> fetch_assoc()) {

        $response = '<div class="admin_event">';
        $response .= '<a href="events-single.php?event_id='.$result['id'].'"><h3>' . $result['title'] . '</h3></a>';
        $response .= '<p>' . getActivityName($result['event_types_id']) . '</p>';
        $response .= '<p>' . $result['start'] . '</p>';
        $response .= '</div>';
        echo $response;
    }

} else {
    $response = '<p class="no_results">No results found. Please modify your selection.';
    echo $response;

}

My goal is to get the activity name in my $response-loop based on the event_types_id. What is wrong about my usage of the function?

EDIT:

The code does not work when I use "return" instead of "echo" at the end of the function getActivityName(). It only works when I use the function code inside my $response loop:

// DO SEARCH AND OUTPUT RESULTS
// ----------------------------
$results = $db -> query($query);
if (mysqli_num_rows($results) > 0) {

    while ($result = $results -> fetch_assoc()) {

        // get name of event type
        $event_types_id = $result['event_types_id'];
        $query_activity_name = "SELECT * FROM event_types WHERE id=$event_types_id";
        $results_activity_name = $db->query($query_activity_name);
        while ($result_activity_name = $results_activity_name -> fetch_assoc()) {
            $activity_name = $result_activity_name['title'];
        }


        $response = '<div class="admin_event">';
        $response .= '<a href="events-single.php?event_id='.$result['id'].'"><h3>' . $result['title'] . '</h3></a>';
        $response .= '<p>' . $activity_name . '</p>';
        $response .= '<p>' . $result['start'] . '</p>';
        $response .= '</div>';
        echo $response;
    }

} else {
    $response = '<p class="no_results">No results found. Please modify your selection.';
    echo $response;

}

Why does this version work but not my function version above?

Upvotes: 0

Views: 35

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

You are echoing $activity_name. You want to return it so that the returned value can be assigned.

Instead of:

echo $activity_name;

Use:

return $activity_name;

Also, you should be developing with error_reporting enabled:

error_reporting(E_ALL);
ini_set('display_errors', '1');

This would tell you that $db is not an object, as well as other issues. You need to pass in $db for it to be available in the function. Also, you have an unused var and you don't need a loop for one record. Consider using LIMIT as well:

function getActivityName($db, $event_types_id) {
        $query_activity_name = "SELECT * FROM event_types WHERE id=$event_types_id LIMIT 1";
        $results_activity_name = $db->query($query_activity_name);
        $activity_name = $results_activity_name->fetch_assoc();

        return $activity_name['title'];
}

Call it with:

getActivityName($db, $result['event_types_id'])

Upvotes: 3

Dhee
Dhee

Reputation: 106

You should use return keyword to return the value.

Use return $activity_name; to return the value

Echo is used to Output one or more strings

Upvotes: 0

Related Questions