【问题标题】:PHP - Create UL Nested Lists From FlatfilePHP - 从平面文件创建 UL 嵌套列表
【发布时间】:2013-05-27 17:16:02
【问题描述】:

(在任何人问之前......我不能在这个项目中使用 MySQL 数据库。)

使用 PHP,我需要从平面文件数据库创建嵌套的 UL。我遇到的问题是我不希望显示重复的项目。我可以进一步解释,但如果你看一下下面的代码,你会看到数据和目标。谢谢。

平面文件数据:

部分|类别|服务 第一节|第一类| 第一节|第二类|SC1 第一节|第二类|SC2 第一节|第二类|SC3 第一节|第二类|SC4 第一节|第三类| 第二节|第四类|SC5 第二节|第四类|SC6 第三节|第五类|SC7


HTML 目标输出:

<ul class="section">
    <li>Section One
        <ul class="category">
            <li>Category One</li>
                <!-- no service -->
        </ul> <!-- /category -->
        <ul class="category">
            <li>Category Two</li>
                <ul class="service">
                    <li>SC1</li>
                    <li>SC2</li>
                    <li>SC3</li>
                    <li>SC4</li>
                </ul> <!-- /service -->
            </li>
        </ul> <!-- /category -->
        <ul class="category">
            <li>Category Three</li>
                <!-- no service -->
        </ul> <!-- /category -->
    </li>
</ul> <!-- /section -->

<ul class="section">
    <li>Section Two
        <ul class="category">
            <li>Category Four</li>
                <ul class="service">
                    <li>SC5</li>
                    <li>SC6</li>
                </ul> <!-- /service -->
            </li>
        </ul> <!-- /category -->
    </li>
</ul> <!-- /section -->

<ul class="section">
    <li>Section Three
        <ul class="category">
            <li>Category Five</li>
                <ul class="service">
                    <li>SC7</li>
                </ul> <!-- /service -->
            </li>
        </ul> <!-- /category -->
    </li>
</ul> <!-- /section -->

我的第一次尝试...认为我需要首先检查“节”是否存在,然后将当前“节”分配给“节最后”以将下一个“节”与当前“节”值进行比较。 ..如果没有,请打印“部分”...然后打印到类别中。 我没有编写“服务”值部分的代码,因为我在部分和类别值方面没有取得任何成功。似乎我在这背后的“逻辑”方面遇到了问题,或者可能有一种方法可以将循环中的过去值与 PHP 提供的下一个值进行比较,但我错过了。

在看到@Kyle S 的示例代码后,我可能走上了一条完全不正确的编码路径。

<?php 
$section = '';
$category = '';
$service = '';
$section_last = '';
$category_last = '';
$service_last = '';

$x = 0;

$file = fopen("categories_data.txt", "r");
if (!$file) { echo 'ERROR: Unable to open file: <strong>'.$file.'</strong>'; }
fgets($file); // IGNORE FIRST LINE IN FLATFILE - column names

while (!feof($file) ) {
    $lines = fgets($file);
    $ele = explode('|', $lines);

    $section = $ele[0];
    $category = $ele[1];
    $service = $ele[2];

    $service = str_replace(array("\n", "\r", "\r\n", "\n\r"), '',$service);

    if(strlen($section)>0) {
        if(!($section == $section_last)) {
            echo '
    <ul>
        <li>'.$section;
            $section_last = $section;
            $x++;
        }
    }
    echo '
            <ul><li>'.$category.' > '.$service.'</li></ul>
';
        if($x) {
            if($section != $section_last) {
                echo '
        </li>
    </ul>
';
        }
    }


} // end $data_file WHILE

fclose($file);
?>

【问题讨论】:

  • 能否将您编写的代码添加到问题中,好吗?您尝试了哪些方法,为什么没有成功?

标签: php list flat-file


【解决方案1】:

我建议使用嵌套数组并使用 PHP 的 in_array 函数检查 service 项是否存在。

例如,要添加一行的数据,在这样做之前先检查服务是否存在:

$data = explode("|", $line);
$section = $data[0];
$category = $data[1];
$service = $data[2];

if ( !array_key_exists($section, $tree) ) {
    $tree[$section] = array();
}

if ( !array_key_exists($category, $tree[$section]) ) {
    $tree[$section][$category] = array(); 
}

if ( !in_array($service, $tree[$section][$category]) ) {
    $tree[$section][$category][] = $service;
}

【讨论】:

    【解决方案2】:

    我不能相信这个解决方案。一位“专家”在另一个网站上回答了我。但是,我真的觉得这个话题足够重要,可以在这个“堆栈”网站上重新发布这个解决方案。

    解决方案:

    <?php
    define('TAB', "\t");
    define('TAB2', "\t\t");
    
    // Open the input file 
    $file = fopen('lines.csv', 'rt');
    // ignore the first line 
    fgets($file);
    // Get the next line into an array splitting on the pipe 
    $line = fgetcsv($file, 255, '|');
    
    // set the initial section value 
    $section = $line[0];
    // Initialise batch array
    $current = array();
    // Loop through all lines in the file 
    do {
    // If this line contains a section that is not the same as the previous line then dump the last section
      if ($section != $line[0]) {
        dump_categories($current, $section);
    // Set our section flag to the next section    
        $section = $line[0];
    // Clear the array of batched items to dump    
        $current = array();
      }
    // Add the line to the batch
      $current[] = $line;
    } while($line = fgetcsv($file, 255, '|'));
    // Remember to dump batch that has built up
    dump_categories($current, $section);
    fclose($file);
    
    function dump_categories(& $lines, $section)
    {
    // Set our category flag
      $category = $lines[0][1];
    // initialise batch array
      $current = array();
    // Start a new seection  
      echo '<ul class="section">' . PHP_EOL;
      echo TAB . '<li>' . $section . PHP_EOL;
    // If there are categories in this section then process them - don't want empty bullets here  
      if (!empty($lines[0][1])) {
        foreach($lines as $l) {
    // if this is a new category then dump category and services for this category    
          if ($category != $l[1]) {
            dump_services($current, $category);
    // set our category flag        
            $category = $l[1];
    // ... and clear the batch array        
            $current = array();
          }
    // add to batch      
          $current[] = $l;
        }
    // and dump batched lines    
        dump_services($current, $category);
      }
      echo TAB . '</li>' . PHP_EOL;
      echo '</ul>';
    }
    // Services we assume is the end of the line so output the service if it exists.
    function dump_services(& $lines, $category)
    {
      echo TAB2 . '<ul class="category">' . PHP_EOL;
      echo TAB . TAB2 . '<li>' . $category . PHP_EOL;
      if (!empty($lines[0][2])) {
        echo TAB2 . TAB2 . '<ul class="service">' . PHP_EOL;
        foreach($lines as $l) {
          if (!empty($l)) {
            echo TAB . TAB2 . TAB2 . '<li>' . $l[2] . '</li>' . PHP_EOL;
          }
        }
        echo TAB2 . TAB2 . '</ul>' . PHP_EOL;
      }
      echo TAB . TAB2 . '</li>' . PHP_EOL;
      echo TAB2 . '</ul>' . PHP_EOL;
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多