PHPRad Classic Initial 2.7.3 Forum

PHPRad Forum => Questions => Topic started by: machobymb on November 03, 2020, 10:19:22 PM

Title: Create custom function and use it problem ...
Post by: machobymb on November 03, 2020, 10:19:22 PM
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())
Title: Re: Create custom function and use it problem ...
Post by: willvin on November 04, 2020, 05:24:48 PM
@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()));