?
Solved

Convert csv list to html dropdown in php form

Posted on 2014-02-01
9
Medium Priority
?
2,291 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 800 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 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Find out what you should include to make the best professional email signature for your organization.
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…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Suggested Courses

718 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