确定所有CSV列的最小值和最大值 [英] Determine minimum and maximum of all CSV columns
问题描述
我需要找到CSV中所有列的最小值和最大值,并在我的网页中为我的滑块使用这些值。最初,我确定CSV文件中的总列,并基于总列,我创建了许多滑块(如亚马逊的价格滑块)。这是用于创建滑块的代码段。
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 the CSV file and based on the total columns, I am creating those many sliders (like amazon's price slider). This is the piece of code for creating the sliders.
<?php
for ( $i = 1; $i < $totalcolumns; $i++ ) {
echo '<input type="text" data-slider="true" data-slider-range="1,500" data-slider-step="1"/>';
}
?>
在上面的代码中,数据滑块范围给定为1,500 。 但是,我需要根据CSV列的最小值和最大值定义这些值。如果我事先知道我的总列,我能够确定所有列的最小值和最大值,如
In the above piece of code, the data-slider-range is given as 1,500. However, I need to define these values based on the minimum and maximum values of my CSV columns. If I know my total columns beforehand, I am able to determine the minimum and maximum values of all the columns as discussed in this link.
假设我的CSV文件中有5列,我需要包括5个滑块,并找到所有5列的最小值和最大值,并将它们用作上述代码中的 data-slider-range 。
Suppose if there are 5 columns in my CSV file, I need to include 5 sliders and find the minimum and maximum for all the 5 columns and use them as the data-slider-range in the above code.
有人能以正确的方式引导我吗?我不知道这是否将在PHP或JavaScript中完成,因为这些技术是全新的。
Can someone please guide me in the right way? I am not sure if this is to be done in PHP or javascript as am entirely new to these technologies.
编辑:
为了找到总列,我使用另一个PHP文件其中我使用下面的代码。
To find the total columns, I use another PHP file in which am using the below piece of code.
$handle = fopen('demo.csv', 'r');
if (($data = fgetcsv($handle, 1000, ',')) !== false) {
}
然后,我使用SESSION将计数($ data)传递到我当前的PHP文件。
And then, I am using SESSION to pass the count($data) to my current PHP file.
$_SESSION["totalcolumns"] = count($data);
在我当前的PHP文件中,我访问我的CSV文件中的总列,如下所示。
In my current PHP file, I am accessing the total columns in my CSV file as below.
$totalcolumns = $_SESSION["totalcolumns"];
推荐答案
您需要修改一段代码, code> $ totalcolumns 到:
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);
对于 data.csv :
3,4,5,6,7
10,8,1,8,8
3,6,7,8,2
您将拥有 $ _ SESSION
值:
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"/>';
}
这篇关于确定所有CSV列的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!