Why my WordPress plugin options array not working - php

My php variable $ex_product_ids which contain value 18,63,72,91 & I have checked it using echo $ex_product_ids it show the value of my input field of my plugin correctly but when I want to use it in a array like $target_products = array($ex_product_ids); its not working only return result for the first array item.
here is the code not working
$_options = get_option( 'license_page_option' );
$ex_product_ids = $_options['ex_product_ids_warranty']; // it have value 18,63,72,91
$target_products = array($ex_product_ids);
but if I manually use those ids like $target_products = array(18,63,72,91); it works
I'm sorry if i'm doing anything wrong ! please help

I think you need to use explode() because current value can be the simple string and you need to convert it to an array of values.
$_options = get_option( 'license_page_option' );
$ex_product_ids = $_options['ex_product_ids_warranty']; // it have value 18,63,72,91
$target_products = explode(",",$ex_product_ids);
print_r($target_products); // array(18,63,72,91)

Related

Using the same array twice on a page

I have an array that is called within a wordpress loop. I also need to call the same array on the same page outside of the loop.
The second array always returns blank, and that happens even if I use copy the array and add it outside of the loop where I'm using it a second time.
I have no idea why this is happening and how to proceed.
<?php
// get ACF custom relationship field 'select'
$rmcwordwide = get_field('rights_management_control_by_worldwide', $post->ID); $rmcwordwidearray = str_split($rmcwordwide,2);
$rmcnorthamerica = get_field('rights_management_control_by_northamerica', $post->ID); $rmcnorthamericaarray = str_split($rmcnorthamerica,2);
$rmcusaonly = get_field('rights_management_control_by_usaonly', $post->ID); $rmcusaonlyarray = str_split($rmcusaonly,2);
$rmcusalatam = get_field('rights_management_control_by_usalatam', $post->ID); $rmcusalatamarray = str_split($rmcusalatam,2);
$rmclatamonly = get_field('rights_management_control_by_latamonly', $post->ID); $rmclatamonlyarray = str_split($rmclatamonly,2);
// Merger arrays
$rmcarray = array_merge( (array)$rmcwordwidearray, (array)$rmcnorthamericaarray, (array)$rmcusaonlyarray, (array)$rmcusalatamarray, (array)$rmclatamonlyarray );
// GET USERS COUNTRY LOCATION FROM IP USING MAXMIND
require '/home/xxxx.com/public_html/vendor/autoload.php';
$gi = geoip_open("/home/xxxx.com/public_html/GeoIP.dat",GEOIP_STANDARD);
$ip = strtolower($_SERVER['REMOTE_ADDR']);
$countrycode = strtolower(geoip_country_code_by_addr($gi, $ip));
geoip_close($gi);
if (in_array($countrycode, $rmcarray)): ?>HELLO<?php endif; ?>
So there's one string in each of the arrays. I then break down the string and make a new array for each.
Then I merge the arrays.
Then I get the users location and if an entry in the merged array and the users country code match then...
Create your own array var before the loop starts. Inside of the loop, add the loop result to the new array each iteration. Then use the newly populated array anywhere you want outside of the loop.

concatenate text values to access array item in php

I'm trying to create a function in php that will read from an array. I am trying to concatenate text and the array id number, to read the value in the array, however it is the concatenated text that is being returned, not the value of the array.
Here is my code:
//arrays with words in dictionary
$dictionary_word1 = array("test1","test2","test3");
$dictionary_word2 = array("test4","test5","test6");
$dictionary_word3 = array("test7","test8","test9");
$word_to_lookup = "dictionary_word_1";
//value to send to function
$returned_word = convert_word($word_to_lookup);
//value returned from function
echo "<br>the returned word from the function is " . $returned_word;
//the text "$dictionary_word_1[2]" is displayed instead of array value
echo "<br>the value in the array is " . $dictionary_word1[2];
//this displays correcty as "test3"
function convert_word($word_to_convert)
{
global $dictionary_word1;
$converted_word = '$'.$word_to_convert .'[2]';
return $converted_word;
}
Could anyone give me any tips on where I am going wrong?
You need to reference the variable this way to do what you are trying (see PHP: Variable Variables and PHP: Variable Parsing):
$converted_word = ${$word_to_convert}[2];
However, notice the difference between dictionary_word_1 and $dictionary_word1? Won't work.
Regardless, anytime you're doing this you would be better with an array. In this case a multidimensional array. Consider:
$words[1] = array("test1","test2","test3");
$words[2] = array("test4","test5","test6");
$words[3] = array("test7","test8","test9");
Then you're always using $words just changing the index and then using a word from that array.

How to Get data From url value parameter $_GET in PHP

sorry i a begginer of php. I have url example here: http://example.com?category=software-hardware .Actual of category is Software & Hardware I using this script to get url above
foreach ( $key as $value ) {
$c = array(' ', '&-');
$d = array('-', '');
echo "".$value->kategori_laporan."";
}
How To get data from my condition url.? Sorry, i bad using english. I want to create url like category from slideshare. Please help me.! Thanks.!
you can fetch it like:
$cat = $_GET['category'];
and you don't need any foreach
$_GET is an associative array having all the query string keys values.
You can get value of any query string parameter from this array by passing the key of query string parameter.
To get category you can use $_GET["category"] in your case.

How I can insert another value in a array[]?

Working with opencart and am trying to enter another value within the product session. By default it only brings ID. How can I get further information?
if (!in_array($this->request->post['product_id'], $this->session->data['wishlist'])) {
$this->session->data['wishlist'][] = $this->request->post['product_id'];
$this->session->data['wishlist'][] = $my_var;
}
If I read this correctly you are trying to add additional data to an already existing array...what you are looking for is
array_push();
array_push — Push one or more elements onto the end of array
syntax:
int array_push ( array &$array , mixed $value1 [, mixed $... ] )
example:
$array = array();
$addingthistoarray = "value to add";
array_push($array, $addingthistoarray);
I'm not sure exactly what you're looking for, but you should be able to do something like this (assuming your "$_SESSION"):
PHP session array
session_start();
$_SESSION['wishlist'] = array( ... );
...
$_SESSION['wishlist']['product_id'] = $this->request->post['product_id'];
...

Problem with associative arrays

I have always sucked at complex arrays there must be something in my brain preventing me from ever understanding them. I will try to make this example really simple so we will not go off topic. I use this code to use numbers to represent each file name:
$mod_nums = array('1' => $input_zip_path . '01_mod_1.0.2.zip',
'2' => $input_zip_path . '02_mod_1.0.1.zip',
);
So when I use $mod_nums['01'] it will display the path to that file. I have an array from the script that put these $mod_nums values into an array like so:
$files_to_zip = array(
$mod_nums['1'],
$mod_nums['2']
);
That worked fine. Now I wanted to add a $_POST value so that I can enter numbers like 1,2 and other numbers that I add to the $mod_nums array later like 1,3,6,12 for example. So I used an explode for those posted values:
$explode_mods = explode(",", trim($_POST['mods']));
Now for the big question that is racking my brain and spent hours on and cannot get it to work.... I need for $files_to_zip to still be in an array and display the posted values of $mod_nums. So it would be like:
$files_to_zip = array( HAVE $_POSTED VALUES IN HERE );
I hope that makes sense. I need $files_to_zip to remain in array format, grab the file path to the zip files from the $mod_nums array, and display it all correctly so it would dynamically output:
$files_to_zip = array('01_mod_1.0.2.zip', '02_mod_1.0.1.zip');
so posted numbers will appear in an array format for the $files_to_zip variable. Make sense? In short I need an array to have dynamic values. Thanks :)
EDIT
Phew I figured it out myself from memory when I worked on something similar many years ago. This looks tough but it isn't. I had to use a foreach and assign the variable into an array like so:
$blah = array();
foreach ($explode_mods as $value)
{
$blah[] = $mod_nums[$value];
}
then I just assigned $files_to_zip to $blah:
$files_to_zip = $blah;
works perfectly :) I just forgot how to dynamically assign values into an array.
// filenames array
$mod_nums = array('1' => $input_zip_path . '01_mod_1.0.2.zip',
'2' => $input_zip_path . '02_mod_1.0.1.zip',
);
// mod_num keys
$explode_mods = explode(',', trim($_POST['mods']));
// array to hold filenames
$files_to_zip = array();
// loop over all the mod_num keys submitted via POST
foreach($explode_mods as $key){
// save the filename to the corresponding array
$files_to_zip[] = $mod_nums[$key];
}
maybe i havn't understood you right, but won't this just be a simple foreach-loop to add the entrys to $files_to_zip like this:
$explode_mods = explode(",", trim($_POST['mods']));
foreach($explode_mods as $k){
$files_to_zip[] = $mod_nums[$k];
}

Categories