How can the pagination function be improved to work seamlessly with the Searchsystem?

The pagination function can be improved to work seamlessly with the Search system by passing the search query parameters along with the pagination links. This way, when a user performs a search and clicks on a pagination link, the search query is retained and the user stays on the search results page with the correct page of results displayed.

// Pagination function with search query parameters
function paginate($total_pages, $current_page, $search_query) {
    $output = '';

    // Add search query parameters to pagination links
    $query_params = http_build_query($search_query);

    // Pagination links
    for ($i = 1; $i <= $total_pages; $i++) {
        $output .= "<a href='search_results.php?page=$i&$query_params'>$i</a> ";
    }

    return $output;
}