This topic is locked
[SOLVED]

 get values for all the rows in a table

7/21/2014 8:02:58 AM
PHPRunner General questions
C
cristi author

I have a table in which one column has ip addressees.

I need to to have another column that associate the ip address with the country.

This Country column does not need to be in the database - the values will only be shown at display time and not stored.
I found this very nice geolocation library: http://chir.ag/projects/geoiploc/ that has also a cache mechanism but I have the following problem:
I can get the ip values from the ip column with this:

global $conn;
$strSQL = "select \"IP\" from \"public\".\"testip\"";
$rs = db_query($strSQL,$conn);

$data = db_fetch_array($rs)

echo getCountryFromIP($data);


The problem is that I don't have only one ip row in the column so it will always display the same value (the last inserted).

How can I fix this?

admin 7/21/2014

I think you can just use 'View as' type Custom and put your PHP code there.

Something like this will work:

$value = getCountryFromIP($data["IP"]);
C
cristi author 7/21/2014



I think you can just use 'View as' type Custom and put your PHP code there.

Something like this will work:

$value = getCountryFromIP($data["IP"]);



I can't use custom view because I don't want to have the country field in the database - hence the field does not exists.

I have the table with only 2 columns: primary key: ID and IP. I created a column in visual designer near the ip column and inserted this code as php code snippet, but It always show the value from the last entry for all the rows...

I think that I need a loop for this but no matter what I tried it will always show in all the rows on the country Column the most recent inputted value: If the last entry is an IP from US than all the rows in country are US even if the other IP's are form other countries...
Maybe try with javascript???
I am not too advanced in php but how I think the loop is like this:

$arr = explode(",",$value);//define array of strings
$count = count($arr) - 1; //count rows
global $conn; //define the conection

$ips = "";//define an empty variable that will hold the results for each row

$i = 0; //define a counter
//next the loop
while($i <= $count)

{

$sql = "select IP_ADDRESS from IP";

$data = db_query($sql,$conn);
$ips = db_fetch_array($data);
$value=$ips["IP_ADDRESS"];
echo getCountryFromIP($value);


$i++;
}


So, basically because the returned IP is always the same (latest entry) the function will always return the same country. for all ip's.

admin 7/21/2014

Add a calculated fields to your SQL query and setup this field as 'View as' Custom i.e.

select ID, IP, '' as Country

from mytable
C
cristi author 7/22/2014



Add a calculated fields to your SQL query and setup this field as 'View as' Custom i.e.

select ID, IP, '' as Country

from mytable



Thank you very much!

Indeed a very simple and working solution and not "my complication".