Determine minimum and maximum of all CSV columns

后端 未结 1 613
[愿得一人]
[愿得一人] 2021-01-06 16:59

I need to find the minimum and maximum values of all the columns in CSV and use those values for my slider in my web page. Initially, I am determining the total columns in t

相关标签:
1条回答
  • 2021-01-06 17:12

    You need to modify piece of code where you get $totalcolumns to:

    $data = [];
    $total_columns = 0;
    $handle = fopen('data.csv', 'r');
    
    while (false !== ($row = fgetcsv($handle, 1000, ','))) {
        0 === $total_columns and $total_columns = count($row);
    
        $i = 0;
        while (++$i <= $total_columns) {
            $data[$i][] = (int) $row[$i - 1];
        }
    }
    
    $i = 0;
    while (++$i <= $total_columns) {
        $_SESSION["min-column-$i"] = min($data[$i]);
        $_SESSION["max-column-$i"] = max($data[$i]);
    }
    
    $_SESSION['totalcolumns'] = $total_columns;
    fclose($handle);
    

    For such data.csv:

    3,4,5,6,7
    10,8,1,8,8
    3,6,7,8,2
    

    you'll have this $_SESSION values:

    var_dump($_SESSION);
    array(11) {
        'min-column-1' => int(3)
        'max-column-1' => int(10)
        'min-column-2' => int(4)
        'max-column-2' => int(8)
        'min-column-3' => int(1)
        'max-column-3' => int(7)
        'min-column-4' => int(6)
        'max-column-4' => int(8)
        'min-column-5' => int(2)
        'max-column-5' => int(8)
        'totalcolumns' => int(5)
    }
    

    Then you'll be able to use range values like this:

    $i = 0;
    while (++$i <= $_SESSION['totalcolumns']) {
        $range = $_SESSION["min-column-$i"] . ',' . $_SESSION["max-column-$i"];
        echo '<input type="text"
                     data-slider="true"
                     data-slider-range="', $range, '"
                     data-slider-step="1"/>';
    }
    
    0 讨论(0)
提交回复
热议问题