级联下拉列表CSV

时间:2015-01-09 19:30:15

标签: php csv menu cascading

我有一个表单,用户从下拉列表中选择一个课程,从下拉列表中选择一个位置,并手动填写文本字段中的日期。

我需要让用户选择课程和位置,用户可以从可用日期列表中进行选择。

我想使用CSV文件。我真的没有其他选择,因为我不能在网上拥有我们的本地数据库。这是每周从我们的数据库生成两次的东西。

CSV文件只包含课程,地点和日期。例如:

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

我是PHP的基础,不知道从哪里开始。我发现很少有关于使用CSV文件处理级联下拉菜单的信息。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

我相信CSV文件如下

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

说过你可以阅读每一行,每行都用“,”爆炸。 它会产生像

这样的数组
array("Dreamweaver Intro Minneapolis", "MN", "3/5/2015");

创建三个数组 - 当然,位置和日期。继续将元素添加到单个列表中。

使用列表生成下拉列表。示例代码如下:

<?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";
}


?>

答案 1 :(得分:0)

我们将使用this question's answer逐行读取您的CSV文件,使用fgets()并在while循环中我们将收集您的选项列表值数组,如下所示:

<?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>
  

注意

     

您必须检查csv文件的有效性。即在其开始时没有专门的空行,每行有三个值   用逗号分隔。