Solved

Convert csv list to html dropdown in php form

Posted on 2014-02-01
9
2,095 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

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 109

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 109

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

828 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