dragon8899 2014-07-07 18:30
浏览 18
已采纳

PHP:获取函数的价值

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条回答 默认 最新

  • dongpeng0127 2014-07-07 18:32
    关注

    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'])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?