0

I am using wordpress and need to dynamically populate a dropdpown. How ever I am having issues with fopen php function in wordpress. https://stackoverflow.com/questions/21990467/

So I've given up trying to import csv file and resorted to just placing my csv file contents into an wordpress options field for a quick fix.

But I am struggling with exploding the csv string safely to then use specific columns for choice label and values.

Can any please advise on how I can do this?

Thank you in advance.


The function

// rider nationality
function motocom_rider_nationality( $field )
{

    // reset choices
    $field['choices'] = array();

    // get the textarea value from options page without any formatting
    $choices = get_field('nationality_codes', 'option', false);

    // explode the value so that each line is a new array piece
    $choices = explode(",", $choices);

    // remove any unwanted white space
    $choices = array_map('trim', $choices);

    $field['choices'] = array(
        null => 'Select nationality...'
    ); 

    // loop through array and add to field 'choices'
    if( is_array($choices) )
    {

        foreach( $choices as $choice )
        {

            $label = $choice['Country'];
            $value = $choice['A4'];

            $field['choices'][ $value ] = $label;
        }
    }

    // Important: return the field
    return $field;

}
add_filter('acf/load_field/name=rider_nationality', 'motocom_rider_nationality');

The csv string in the options textarea field...

Country,A2,A3,Number
Aaland Islands,AX,ALA,248
Afghanistan,AF,AFG,4
Albania,AL,ALB,8
Algeria,DZ,DZA,12
Samoa,AS,ASM,16
Andorra,AD,AND,20

5
  • 1
    why not use str_getcsv()? Commented Feb 24, 2014 at 15:20
  • str_getcsv() Commented Feb 24, 2014 at 15:20
  • Please see my comments above in question - Can't get str_getcsv to split the string in a proper array. Commented Feb 24, 2014 at 15:33
  • @Joshc: You are not splitting your input into lines, and neither does str_getcsv (it should be obvious from the output that the newline chars gets included into the output). str_getcsv works just fine, fix your code. It's also obvious that str_getcsv should be called inside a loop, which does not happen. Commented Feb 24, 2014 at 15:34
  • @Jon, i know code is wrong and that's why i'm trying to find a method that works. Commented Feb 24, 2014 at 15:47

1 Answer 1

0

Fixed it using this solution...

// csv to array
function motocom_csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}


// rider nationality
function motocom_rider_nationality( $field )
{

    // reset choices
    $field['choices'] = array();

    // get the textarea value from options page without any formatting
    $choices = motocom_csv_to_array( get_template_directory() . '/csv/nationality-codes.csv' );

    $field['choices'] = array(
        null => 'Select nationality...'
    ); 

    // loop through array and add to field 'choices'
    if( is_array($choices) )
    {

        foreach( $choices as $choice )
        {

            $label = $choice['Country'];
            $value = $choice['A3'];

            $field['choices'][ $value ] = $label . ' [' . $value . ']';

        }
    }

    // Important: return the field
    return $field;

}
add_filter('acf/load_field/name=rider_nationality', 'motocom_rider_nationality');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.