1

So i am working on a site on php and i have to do a slider for the years of publications. Explaining in more detail, what i want is only to slide through the years when the publications were made.

// Your database connection and other PHP code

// Query to retrieve the minimum and maximum publication years
$query = "SELECT MIN(YEAR(data)) AS min_year, MAX(YEAR(data)) AS max_year FROM publicacoes";
$stmt  = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

// Retrieve the minimum and maximum years from the query result
$minYear = $result['min_year'];
$maxYear = $result['max_year'];
// JavaScript function to update the displayed value when slider changes
function updateSliderValue() {
  var slider              = document.getElementById("yearSlider");
  var selectedYearDisplay = document.getElementById("selectedYearDisplay");
  
  // Update the displayed value
  selectedYearDisplay.innerText = slider.value;
}
<input id="yearSlider" type="range" 
  min="<?= $minYear ?>" max="<?= $maxYear ?>" 
  value="<?= $maxYear ?>" onchange="updateSliderValue()">
<span id="selectedYearDisplay"></span>

This code only limit the slider from the first year that a publication were made until the most recent publication year.

The years are in the column "data" of the publications table.

I thought about storing the distinct year values in an array and then passing it to the slider but i don't know how to do that. Any help would be appreciated.

6
  • You can use datalist. You can see this answer: stackoverflow.com/questions/26612700/… You can create datalist values with the loop of database data and refer the range at the datalist Commented May 12, 2024 at 15:35
  • Extend your query with grouping by year and create a datalist from that. When you order by year, just grab the first as lowest and last as max value. Commented May 12, 2024 at 15:43
  • @MarkusZeller i will try that, but with the data list, it still shows years between that are not years of publications. How can I remove those? Commented May 12, 2024 at 15:55
  • I do not understand. If you query only the data you need, the datalist only contains what you searched for. Commented May 12, 2024 at 16:08
  • I tried to manually put the years just for experimenting in a data list and under the slider there are some small black stripes which are those years that I manually added to the data list but inside the slider, some values are not from the data list. So for example, the first year is 1966 and the next one is 1972, if I slide through them there are the years 1967,1968,1969...1972 and I don't know why those years are shown in the slider if the data list does not contain those years in between. Commented May 12, 2024 at 16:19

1 Answer 1

0

Your query has only 2 values, min and max.

So you change your request, grouping by "year field name". Then you have the list of all the years. It can use foreach and create slides.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.