Solved

Convert csv list to html dropdown in php form

Posted on 2014-02-01
9
1,997 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysqli 3 21
Change text to radio button and calendar form 2 34
Session timeout 5 14
bootstrap footer centering and expand problems 7 29
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.
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

920 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

15 Experts available now in Live!

Get 1:1 Help Now