• Resolved bastiwp

    (@bastiwp)


    Hi, I did not find my issue covered in one of the existing forum posts.

    I would like to fill a dynamic dropdown menu with data that shall be loaded from a CSV file (. Is this easily (i.e., without a lot of coding) doable with the plugin? Which workflow would you advise for this, i.e. the loading of CSV followed by the import into your plugin? Or is this not so easily doable with your plugin?

    BTW, is it possible (also performance-wise) to populate with up to 1000 entries?

    Greetings!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    I did not find my issue covered in one of the existing forum posts

    you will find some information in the tutorial section of the plugin where several videos have been created to guide you. One such video is on dynamic fields which has a chapter on custom sources using the filter hook.

    Is this easily (i.e., without a lot of coding

    if you know how to code it’s rather straightforward.

    Which workflow would you advise for this

    follow the same logic as in the video tutorial.

    BTW, is it possible (also performance-wise) to populate with up to 1000 entries?

    sure, and I would enable the select2 field construct in order to simplify the search/selection in a such a large list.

    Thread Starter bastiwp

    (@bastiwp)

    Thanks a lot, I will try this out soon. And thanks for the quick response.

    Thread Starter bastiwp

    (@bastiwp)

    sure, and I would enable the select2 field construct in order to simplify the search/selection in a such a large list.

    With your help it worked out well. Data is loaded from a csv file now using some lines of code, and thanks to select2 searching and selecting is also much easier.

    Thanks a lot ??

    Plugin Author Aurovrata Venet

    (@aurovrata)

    wonderful! glad you managed to get it work!

    Thanks a lot

    do leave a review when you have a moment to spare.

    Thread Starter bastiwp

    (@bastiwp)

    do leave a review when you have a moment to spare.

    I will ?? I am facing a design issue, but I will ask it in a separate thread ??

    Plugin Author Aurovrata Venet

    (@aurovrata)

    sure

    Can I ask you to share your solution? What did you add to the functions file?
    Thank you.

    Thread Starter bastiwp

    (@bastiwp)

    Can I ask you to share your solution? What did you add to the functions file?
    Thank you.

    Sorry that it took so long; here it is. You get the skeleton of the function by clicking on “Filter custom options” in the grid editor, as explained in the video.
    It will look somehow like the first lines below. Just create the skeleton in the grid editor, paste it to functions.php and the copy&paste the respective parts from the code below. Of course adapth path and file name.

    add_filter( 'cf7sg_dynamic_dropdown_custom_options','my_list_dynamic_options',10,3);
    function my_list_dynamic_options($options, $name, $cf7_key){
      if('list-dynamic'!==$cf7_key || 'my-list' !== $name){
        return $options;
      }
    //these are the label users will see when the dropdown opens.
    //you can group your options if need be. Let's assume you have an array of arrays of data to display in groups.
    //$data = ... //fetch your data, either from the database or some other source.
    
    $uploads_folder = "./path/to/file/"; ## relative path to your file, incl. closing slash
    $filename = "abc.csv"; ## name of file
    
    try 
    {
    	$file = fopen($uploads_folder.$filename, 'r'); ## tries to open the file
    	if (! $file) 
    	{	//if that fails...
    		throw new Exception("File could not be opened!" . $uploads_folder.$filename);
    	}
    }
    // error output 
    catch (Exception $e) 
    {
    	echo "Error (File: ".$e->getFile().", line ".
    	$e->getLine()."): ".$e->getMessage();
    }
    
    $data = array();
    ### each line of the csv is a pair of values in the array, line[0] is the first column, line[1] is the second, separated by ";" (if the csv has multiple columns)
    if ( $file)
    while (($line = fgetcsv($file, 0, ";")) !== FALSE) 
    {	// until end of file
    	$data[$line[0]] = $line[0];
    }
    if ($file)
    	fclose($file);
    
    foreach($data as $value)
    {
    	$options .= '<option value="'.$value.'">'.$value.'</option>';
    }
    return $options;
    }
    Plugin Author Aurovrata Venet

    (@aurovrata)

    echo “Error (File: “.$e->getFile().”, line “.
    $e->getLine().”): “.$e->getMessage();

    don’t echo in a filter function, this will create problems if it is executed.

    either log this to the debug log file, store is as a transient and display it as an error admin notification, or put an error message into the dropdown filter options to nitify the front-end user.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Fill dynamic dropdown with data from CSV file’ is closed to new replies.