0

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?

2 Answers 2

3

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'])
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it with "return", but still get no output. I only get the ouput when i use the getActivityName code inside the $response loop. See my edited answer --> do you know why it works then but not with a function?
Added a little. However, I would assume that a JOIN would work better than querying in a loop.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.