【问题标题】:Cascading Dropdown CSV级联下拉 CSV
【发布时间】:2015-01-09 19:30:14
【问题描述】:

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

我需要让用户选择课程和地点,并且用户可以从可用日期列表中进行选择。

我想使用 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 文件处理级联下拉菜单的信息。任何帮助将不胜感激。

【问题讨论】:

  • 你是正确的 CSV 文件吗? i,e 它应该是Dreamweaver Intro, Minneapolis MN, 1/5/2015,换句话说,每行应该有两个逗号!

标签: php csv menu cascading


【解决方案1】:

我相信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";
}


?>

【讨论】:

    【解决方案2】:

    我们将使用this question's answer 逐行读取使用fgets() 的CSV 文件,在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 文件的有效性。即在它的开头没有特别的空行,每行有三个值 用两个逗号分隔。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 2011-03-03
      • 2021-11-16
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多