How to add or remove 1 year
Solvedjordane45 Posted messages 30426 Registration date Status Moderator Last intervention -
I have developed an annual calendar, here are the sources:
index.php
[code block with PHP and HTML content]
The Year.php class
[code block with PHP content]
How can I add or subtract a year by clicking the 'next' or 'previous' buttons?
Thank you for your help.
Best regards
8 answers
-
Hello,
You should first ensure that your buttons trigger an action ...
Either via a link or via an Ajax call.
Once you have that in place, pass along a variable that will contain two values: the current date and whether to do prev or next; that will be easy.
From there, in your PHP, you can retrieve these two variables and use them to add or subtract whatever you want.
.
Best regards,
Jordane -
Indeed,
Here are the modified lines:
<?php namespace App; use \DateTime; require_once __DIR__ . '/vendor/autoload.php'; $year = new DateTime(); $year = $year->format('Y'); $year = intval($year); if(isset($_POST['prev'])) { $anne = $year; $anne--; var_dump($anne); } if(isset($_POST['next'])) { $year++; } $year = new Year($year); ?> <nav> <form method="post"> <input class="btn btn-primary" name="prev" type="submit" value="Précédant"> </form> <button class="btn btn-success" id="premier">Premier semestre</button> <button class="btn btn-success" id="second">Deuxième semestre</button> <form method="post"> <input class="btn btn-primary" name="next" type="submit" value="Suivant"> </form> </nav>The problem I have now is that it only does it once: prev -> 2022, next -> 2024.
I'm probably doing something wrong somewhere.
If you have an idea, I would be very grateful.
-
Thanks,
I only want the year, it only works once.
Here is the code again:
<?php namespace App; require_once __DIR__ . '/vendor/autoload.php'; $year = new Year($_GET['year'] ?? null); if(isset($_POST['prev'])) { $year->year--; $year = new Year($year->year); } if(isset($_POST['next'])) { $year->year++; $year = new Year($year->year); } ?> -
Thank you,
I tried to put everything in _GET, but nothing shows up in the URL, so I enter the URL.
I am a bit of a beginner in PHP.
thanks for helping me to code it properly.
-
How do you send data to your PHP code? (What HTML or JS code is used for your buttons?)
-
-
You use a POST form to transmit your data
<form method="post"> <input class="btn btn-primary" name="next" type="submit" value=">"> </form>
As I’ve already told you... it would just require adding a hidden input containing the current year so that you can then add or subtract from that year...
.
Best regards,
Jordane -
Thank you for everything.
In fact, I just added 2 methods to my Year class like this:
/** * Sends the next year * @return Year */ public function nextYear(): Year { $year = $this->year + 1; return new Year($year); } /** * Sends the previous year * @return Year */ public function prevYear(): Year { $year = $this->year - 1; return new Year($year); }and in index.php it becomes:
<nav> <a href="<?= $url ?>?year=<?= $year->prevYear()->year ?>" class="btn btn-primary">Prev</a> <button class="btn btn-success" id="premier">Premier semestre</button> <button class="btn btn-success" id="second">Deuxième semestre</button> <a href="<?= $url ?>?year=<?= $year->nextYear()->year ?>" class="btn btn-primary">Next</a> </nav>
$url = $_SERVER['PHP_SELF'];
It works exactly as I wanted.