Solved

Convert csv list to html dropdown in php form

Posted on 2014-02-01
9
2,219 Views
Last Modified: 2014-02-03
I have a list of spoken languages in a csv file that I need to quickly put into a WordPress php form. It should display as a drop down list. If it is an easier task from xlxx or tab separated values, I can convert them.

Mark
0
Comment
Question by:MarkJulie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39827401
Can you give us an example of data, please?
0
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 100 total points
ID: 39827414
you can try this
function readCSV($csvFile){
	$file_handle = fopen($csvFile, 'r');
	while (!feof($file_handle) ) {
		$line_of_text[] = fgetcsv($file_handle, 1024);
	}
	fclose($file_handle);
	return $line_of_text;
}


// Set path to CSV file
$csvFile = 'test.csv';

$csv = readCSV($csvFile);
<select>
  <?php foreach($csv as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39827420
No need to reinvent the wheel: you can simply use native fgetcsv() function to read the csv file into an array.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39827654
Agree with Marco... Please post the CSV file here in the code snippet or post a link to the CSV file so we can see the actual format of the data, how many lines, how many positions per line, etc.  If you show us the data you may be able to get a tested-and-working code example.
0
 

Author Comment

by:MarkJulie
ID: 39828712
Sorry. I thought I had attached at original posting. Also, the WP form plugin wants html syntax, not php (though it displays in a .php page.
Languages.csv
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 200 total points
ID: 39829717
Use "view source" to see the HTML fragment created here.
http://www.laprbass.com/RAY_temp_markjulie.php

<?php // RAY_temp_markjulie.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28354227.html

// THE LANGUAGES
$langs = <<<LANGS
"Arabic"
"Armenian"
"Awadhi"
"Azerbaijani"
"Bengali"
"Bhojpuri"
"Bulgarian"
"Burmese"
"Gan (Chinese)"
"Hakka (Chinese)"
"Jinyu (Chinese)"
"Mandarin (Chinese)"
"Min Nan (Chinese)"
"Wu (Chinese)"
"Xiang (Chinese)"
"Yue (Cantonese/Chinese)"
"Danish"
"Dutch"
"English"
"Estonian"
"Farsi"
"Finnish"
"French"
"German"
"Greek"
"Gujarati"
"Hausa"
"Hindi"
"Italian"
"Japanese"
"Javanese"
"Kannada"
"Korean"
"Latvian"
"Lithuanian"
"Maithili"
"Malayalam"
"Marathi"
"Norwegian"
"Oriya"
"Panjabi"
"Persian"
"Polish"
"Portuguese"
"Romanian"
"Romansch"
"Russian"
"Serbo-Croatian"
"Sindhi"
"Spanish"
"Sunda"
"Swedish"
"Tamil"
"Telugu"
"Thai"
"Turkish"
"Ukrainian"
"Urdu"
"Vietnamese"
"Yoruba"
"Other (not listed)"
"Default Language"
LANGS;

// MAKE AN ARRAY OF LANGUAGES AND TURN EACH ONE INTO AN OPTION TAG
$arr = explode(PHP_EOL, trim($langs));
foreach ($arr as $key => $lan)
{
    $lan = trim($lan);
    $txt = trim($lan, '"');
    $arr[$key] = '<option value=' . $lan . '>' . $txt . '</option>';
}

// RECONSTRUCT THE STRING WITH THE OPTION TAGS
$opts = implode(PHP_EOL, $arr);

// CREATE THE FORM ELEMENT
$form = <<<FORM
<select name="lang">
$opts
</select>
FORM;

// SHOW WHAT WE CONSTRUCTED
echo $form;

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39829727
Interesting... The CSV file posted with this question does not have any line endings or commas.  Is that an exact copy of the file you're using?  If not, and you want to post the URL of the file I'll be glad to try to use the original.
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39829823
Though i could not understand what he meant but author has indicated this

the WP form plugin wants html syntax, not php (though it displays in a .php page.

frankly i am quite confuse , some option are reading csv file using html5 but then again java script would be needed to populate options.
besides is there any wordpress plugin that would need just html.
0
 

Author Closing Comment

by:MarkJulie
ID: 39830106
Thanks for the general and specific solutions despite a less than ideal file.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question