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"/>';
}
?>
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.
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.
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.
EDIT:
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) {
}
And then, I am using SESSION to pass the count($data) to my current PHP file.
$_SESSION["totalcolumns"] = count($data);
In my current PHP file, I am accessing the total columns in my CSV file as below.
$totalcolumns = $_SESSION["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);
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"/>';
}