I am trying to put together a geodetection for altering slight language variables.
I have the detection working perfectly, but the array check seems to not be working, I need to know if its from a list of countries. If I echo the country then I get the correct name so I know that parts working.
//Get User Country
$country_arr = array(
"Canada" => "ca",
"United States" => "us",
"United Kingdom" => "uk",
"Australia" => "au",
"South Africa" => "za",
"Unknow" => "shot"
);
$country=visitor_country();
if (in_array($country, $country_arr)) {
//include ("languages/" . $lang . ".php");
//echo $country_arr[$country];
echo "yes
";
} else {
//include ("languages/en.php");
echo "no
";
}
echo $country;
Have a functioning sandbox with all the related code working and edible http://sandbox.onlinephpfunctions.com/code/714d5105012f28cada695a6f11dc61516722e6d7
Also not working with a standard 1 dimensional array
$count_array = array("South Africa", "Unknow");
Use array array_key_exists in place of in_array
//Get User Country
$country = visitor_country();
$country_arr = array(
"Canada" => "ca",
"United States" => "us",
"United Kingdom" => "uk",
"Australia" => "au",
"South Africa" => "za",
"Unknown" => "shot"
);
//$count_array = array("South Africa", "Unknown");
if ( array_key_exists($country, $country_arr) ) {
//include ("languages/" . $lang . ".php");
//echo $country_arr[$country];
echo "yes<br>";
} else {
//include ("languages/en.php");
echo "no<br>";
}
echo $country;
For in_array function your $country_arr array should be like this
/* For IN Array */
$country_arr = array(
"Canada",
"United States",
"United Kingdom",
"Australia",
"South Africa",
"Unknown"
);
your $count_array = array("South Africa", "Unknow"); is not working because $country returns Unknown and you had Unknow that's not matching with with the value..
With in_array you check values not keys.
//Get User Country
$country_arr = array(
"Canada" => "ca",
"United States" => "us",
"United Kingdom" => "uk",
"Australia" => "au",
"South Africa" => "za",
"Unknow" => "shot"
);
$country = 'Canada';
if ( isset($country_arr[$country]) )
{
echo "yes";
}
else
{
echo "no";
}
echo "\n$country";
BTW
Keep in mind that PHP even with 'regular' arrays - without keys - have implicit keys so for in_array to work you would have to have:
$country_arr = array( "Canada", "United States", "United Kingdom" );
Above all countries have their keys (but implicit) so countries are values here. On your original code countries are keys.
You have a typo - Unknow / Unknown, also, you're not searching against keys with in_array(), you need to use array_key_exists() or array_flip($country_arr)
Your GeoIP service returns countryName and countryCode fields. Just use countryCode instead of countryName and your code will work:
if($ip_data && $ip_data->geoplugin_countryCode != null)
{
$result = $ip_data->geoplugin_countryCode;
}
Related
I have this so far :
if (is_request_var('requestor') == 'PPA'){
$dataJson[][] = array("status" => "success","value" => $fetchValue ,"postcode" => $fetchPostCode,"params"=>"");
$notice = $dataJson;
}
I want to get (refer below) from PHP how do i arrange my PHP array code
jQuery191013316784294951245_1485527378760([
{
"value": "\u003cstrong\u003eNAIROBI\u003c/strong\u003e KENYATTA AVENUE, GENERAL POST OFFICE, 00100",
"postcode": "00100"
},
{
"value": "\u003cstrong\u003eNAIROBI\u003c/strong\u003e HAILE SALASSIE AVENUE, CITY SQUARE, QLD, 00200",
"postcode": "00200"
}
])
Try this:
$dataJson = array("value" => $fetchValue, "postcode" => $fetchPostCode);
$notice = json_encode($dataJson);
echo $notice;
Returns a string containing the JSON representation of value.
http://php.net/manual/en/function.json-encode.php
I'm currently taking the results of a table and using wp_send_json to using it as a JSON response. The data is encoded as expected, however I'd like to tweak the output a bit by changing the keys, formating, and order. I'm not sure how to rebuild the array and encode as json after so I'm looking for a little bit of help.
$stuff= $wpdb->get_results( $wpdb->prepare("SELECT * FROM wp_table"), ARRAY_A);
wp_send_json($stuff);
As of now the results I get via print_r look as follows.
Array(
[0] => Array(
[id] => 1[gender] => Male[email] => test#loas . com[lat] => 38[long] => - 97[country_srt] => USA[country_long] => UnitedStates
) [1] => Array(
[id] => 2[gender] => Female[email] => femal#test . com[lat] => 38[long] => - 97[country_srt] => USA[country_long] => UnitedStates
)
)
When encoded I get:
[{
"id": "1",
"gender": "Male",
"email": "test#loas.com",
"lat": "45",
"long": "-76",
"country_srt": "USA",
"country_long": "United States"
}, {
"id": "2",
"gender": "Female",
"email": "femal#test.com",
"lat": "98",
"long": "-34",
"country_srt": "USA",
"country_long": "United States"
}]
Thing is, I don't really need some of these values and also need to format some things to output for easy map plotting. For instance the country longform and gender go into an html formatted string. What I'm looking to do is transform this array to result in:
[ idhere: {
"value": "1",
"latitude": "45",
"longitude": "-76",
"tooltip": {"content":"HTML Showing gender variable and country variable"}
}, idhere: {
"value": "2",
"latitude": "98",
"longitude": "-34",
"tooltip": {"content":"HTML Showing gender variable and country variable"}
}]
I think what you need to do is break down the process down into steps (so you can change the data around) instead of sending your sql data to json directly.
build your own array
iterate over your sql result set while adding your own markup
send the output to json
something like:
$preJSON = array();
// select only columns you need
$sql = "SELECT id, gender, country_srt, lat, long
FROM wp_table"
$count = 0; // this is for $preJSON[] index
foreach( $wpdb->get_results( $sql ) as $key => $row ) {
// each column in your row will now be accessible like this:
// $my_column = $row->column_name;
// now we can do:
$value = $row->id;
$latitude = $row->lat;
$longitude = $row->long;
$gender = $row->gender;
$country = $row->country_srt;
$tooltip = array(
"content" => "HTML and stuff" . $gender . "more HTML and stuff" . $country
);
// now we can build a row of this information in our master array
$preJSON[$count] = array(
"value" => $value,
"latitude" => $latitude,
"longitude" => $longitude,
"tooltip" => $tooltip
);
// increment the index
++$count;
}
// after foreach
// send the whole array to json
$json = json_encode( $preJSON );
I believe this should be the basic gist of what you need
So I want the form to find what the user searches for and then return results for that, through the array down below. Is this possible? I've tried to write the php myself, but I'm clueless.
<form action="result.php" method="post">
<input type="text" name="country" size="30" id="autocomplete-ajax" placeholder="Search for country, timezone or city and hit enter!" maxlength="30">
</form>
<?php
$countries = array(
"AD" => "Andorra",
"AE" => "United Arab Emirates",
"AF" => "Afghanistan",
"AG" => "Antiqua and Barbuda",
"AI" => "Anguilla",
"AL" => "Albania",
"ZZ" => "Unkown or Invalid Region");
if (isset($_POST[$countries[0]])){
echo "<p>Correct!</p>";
}
if (isset($_POST[$countries[1]])){
echo "<p>Correct2</p>";
}
?>
You could replace the other issets with this - it will check the post values against the array
if (isset($_POST["country"])){
foreach($countries as $k => $c){
if(strtolower($c) == strtolower(trim($_POST["country"]))){
echo "<p>Correct!</p>";
}
}
}
I have an array of all the countries in the world as such:
$countries = array(
"GB" => "United Kingdom",
"US" => "United States",
"AF" => "Afghanistan",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua And Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"BY" => "Belarus",
"BE" => "Belgium",
"BZ" => "Belize",
"BJ" => "Benin",
"BM" => "Bermuda",
"BT" => "Bhutan",
"BO" => "Bolivia",
"BA" => "Bosnia And Herzegowina",
"BW" => "Botswana",
"BV" => "Bouvet Island",);
And so on for all countries; I am 100% positive every country is listed properly.
I have an application form which stores the result in a file stored on the server. Currently the review page for the application is a basic text version and I am now in the process of putting it into a mock-form for my client to have a more visually appealing method of reviewing applications.
So an array named $in_data stores the results that come from the file. This array is structured as such "emergency_medical_insurance" => "value_user_entered". Each key is the name of the HTML element it came form and the value is what the user put in.
The country select list on the form returns a two-letter code of the country. So what I am trying to do is search $countries with the value of $in_data['country_select'] and then return the name of the country.
echo $in_data['country_select']; returns 'CA' the letter code for Canada and the test country I have entered.
echo $countries['CA']; returns 'Canada'
if (array_key_exists($in_data['country_select'], $countries)){
echo "Country Found";
}
else { echo "failed"; }
returns nothing.
if (array_key_exists('CA', $countries)){
echo "Country Found";
}
else { echo "failed"; }
Also returns nothing. And when I say nothing I mean nothing, not null, not true, not false; just doesn't even run.
My question is simple; how is the code below (taken from the offical PHP manual) which does EXACTLY the same thing my code does, working, but my code won't even return anything?
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
since you are reading from a file, you may be getting other characters, try trim():
if (array_key_exists(trim($in_data['country_select']), $countries)){
echo "Country Found";
}
else { echo "failed"; }
I had a similar problem while reading from a file.
The solution was to remove the BOM from the first line.
function remove_utf8_bom($text) {
$bom = pack('H*','EFBBBF');
$text = preg_replace("/$bom/", '', $text);
return $text;
}
OK, let's say when a user selects a country, they are also added with a "federation". These federations are pretty much region-centric.
Let's say I have something like this:
function getFedration($country_iso) {
// 6 federations
// afc = asian nations
// caf = african nations
// cocacaf = north & central america and Caribbean nations
// conmebol = south america
// ofc = Oceanian nations
// uefa = european nations
$afc = array("Japan", "China", "South Korea");
$caf = array("Cameroon", "Chad", "Ivory Coast");
$concacaf = array("United States" , "Canada", "Mexico");
$conmebol = array("Argetina", "Brazil", "Chile");
$ofc = array("Fiji", "New Zealand", "Samoa");
$uefa = array("Spain", "England", "Montenegro");
/*
PSEUDO-code
If $country_iso is in either of six arrays... mark that as the federation...
*/
return $federation;
}
I know, it says a country's name but when it comes down to it, it will be country's iso like JP instead of Japan, CN instead of China, et cetera.
So, I was wondering, is this a feasible thing or is there a better way you'd think?
How about putting all federations into an array, in order to loop through it? Makes things easier, like so:
function countryToFederation($country_iso) {
$federations = array(
"afc" => array("Japan", "China", "South Korea"),
"caf" => array("Cameroon", "Chad", "Ivory Coast"),
"concacaf" => array("United States" , "Canada", "Mexico"),
"conmebol" => array("Argetina", "Brazil", "Chile"),
"ofc" => array("Fiji", "New Zealand", "Samoa"),
"uefa" => array("Spain", "England", "Montenegro"),
);
foreach($federations as $federation) {
if(in_array($country_iso, $federation)) {
return $federation;
}
}
}
If a federation can only belong to one country, I would create one array instead:
$countryToFederationMap = array(
'Japan' => 'AFC',
'China' => 'AFC',
'Cameroon' => 'CAF',
// ...
);
Then the federation is simply:
return $countryToFederationMap[$country];