Solved

Convert csv list to html dropdown in php form

Posted on 2014-02-01
9
1,963 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 30

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 30

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
 
LVL 108

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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 108

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 108

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now