0

I have a form where the user selects a course from a dropdown, location from a dropdown and fills in a date in a text field manually.

I need to have the user select the course and location and the user can select from a list of available dates.

I would like to use a CSV file. I really have no other option because I cannot have our local DB on the web. This is something that would be generated twice weekly from our database.

The CSV file just contains course, location and dates. For example:

Course            Location        Date
Dreamweaver Intro Minneapolis, MN 1/5/2015
Dreamweaver Intro Minneapolis, MN 3/5/2015
Dreamweaver Intro Minneapolis, MN 5/5/2015
Illustrator Intro Orlando, FL     3/5/2015
Illustrator Intro Orlando, FL     1/5/2015
Illustrator Intro Orlando, FL     5/5/2015

I am basic with PHP and am not sure where to begin. I have found little information on working with cascading dropdown menus with CSV files. Any help would be appreciated.

  • Are you that correct CSV file? i,e it should be `Dreamweaver Intro, Minneapolis MN, 1/5/2015` in other words it should has two commas per line! – SaidbakR Jan 09 '15 at 20:50

2 Answers2

0

I believe the CSV file is as follows

Dreamweaver Intro Minneapolis, MN, 5/5/2015
Illustrator Intro Orlando, FL, 3/5/2015

Having said that you can read every line, the explode each line by ", ". It will result in array like,

array("Dreamweaver Intro Minneapolis", "MN", "3/5/2015");

Create three arrays - for course, location and date. Keep adding the elements to individual list.

Generate dropdown using the list. Sample code would be like:

<?php

generateDropdownFromCsv(
"Course,            Location,        Date
Dreamweaver Intro Minneapolis, MN, 1/5/2015
Dreamweaver Intro Minneapolis, MN, 3/5/2015
Dreamweaver Intro Minneapolis, MN, 5/5/2015
Illustrator Intro Orlando, FL,     3/5/2015
Illustrator Intro Orlando, FL,     1/5/2015
Illustrator Intro Orlando, FL,    5/5/2015");

function generateDropdownFromCsv($fileContent){
  $courses = array();
  $locations = array();
  $dates = array();

  foreach( explode( "\n", $fileContent ) as $eachLine ){
    $column = explode( ",", $eachLine );
    array_push( $courses, $column[0] );
    array_push( $locations, $column[1] );
    array_push( $dates, $column[2] );    
  }

  echo "<select>\n";

  foreach( $courses as $course ){
    echo "<option>" . trim($course) . "</option>\n";
  }

  echo "</select>\n";
  echo "<select>\n";

  foreach( $locations as $location ){
    echo "<option>" . trim($location) . "</option>\n";
  }

  echo "</select>\n";
  echo "<select>\n";

  foreach( $dates as $date ){
    echo "<option>" . trim($date) . "</option>\n";
  }

  echo "</select>\n";
}


?>
Bikal
  • 1
  • 1
0

We will use this question's answer to read your CSV file line by line which is using fgets() and inside the while loop we will collect an array of your options list values as follows:

<?php
$handle = fopen("path/to/your/inputfile.csv", "r");
$courses = array();
$locations = array();
$dates = array();
if ($handle) {
    $l = 0;
    while (($line = fgets($handle)) !== false) {
        if ($l === 0){
            //escaping the csv header line
            $l++;
            continue;
        }
        $options = explode("," $line);
        $courses[] = trim($options[0]);
        $locations[] = trim($options[1]);
        $dates[] = trim($options[2]);
        $l++;
    }
} else {
    echo "<h1>Error: Opening the file has been failed.</h1>";
} 
fclose($handle);
?>
<!-- HTML and inside a form-->
<select name="courses">
 <?php for ($i = 0; $i < count($courses); $i++): ?>
   <option value="<?php echo $courses[$i]; ?>"><?php echo $courses[$i]; ?></option>
 <?php endfor; ?>
</select>
<select name="locations">
 <?php for ($i = 0; $i < count($courses); $i++): ?>
   <option value="<?php echo $locations[$i]; ?>"><?php echo $locations[$i]; ?></option>
 <?php endfor; ?>
</select>
<select name="dates">
 <?php for ($i = 0; $i < count($courses); $i++): ?>
   <option value="<?php echo $dates[$i]; ?>"><?php echo $dates[$i]; ?></option>
 <?php endfor; ?>
</select>

Notice

You must check your csv file validity. i.e There is no empty lines specially at its beginig and it has three values per line separated by two commas.

Community
  • 1
  • 1
SaidbakR
  • 13,303
  • 20
  • 101
  • 195