Skip to main content
Topic: Create custom function and use it problem ... (Read 677 times) previous topic - next topic

Create custom function and use it problem ...

Hi,

I was created in helpers/Functions.php a new function:
Code: [Select]
function get_location_by_user_ip(): string {
$ip_list = get_user_ip();
$ipArray = explode(",", $ip_list);
$last_ip = end($ipArray);
return substr($last_ip, 0, strrpos($last_ip, '.'));
}

When I created a Query in with this function, in PHPMyAdmin is correct, but in PHPRad list is empty:
Code: [Select]
"SELECT DISTINCT id AS value, name AS label FROM table WHERE is_true=? AND id=(SELECT DISTINCT country_id FROM ip_addresses WHERE ip_addr LIKE ?)"   , array('1',get_location_by_user_ip())

 

Re: Create custom function and use it problem ...

Reply #1
@machobymb‍ it is not advisable to create a function that makes queries in helpers/Functions.php, but if you must you have to initialize the database connection before you can do that. See the example below.

Code: [Select]
$cnt = new BaseController;
$db = $cnt->GetModel();
$db->rawQuery("SELECT DISTINCT id AS value, name AS label FROM table WHERE is_true=? AND id=(SELECT DISTINCT country_id FROM ip_addresses WHERE ip_addr LIKE ?)" , array('1',get_location_by_user_ip()));