How to add or remove 1 year

Solved
chabinot Posted messages 391 Status Member -  
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   -
Hello,

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

  1. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
     

    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

    0
  2. chabinot Posted messages 391 Status Member 16
     

    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.

    0
    1. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
       

      As I told you, you need to pass TWO variables to your code

      the prev or the next, like you did

      AND ... the YEAR shown by your calendar so that it knows which value it should act on ...

      0
  3. chabinot Posted messages 391 Status Member 16
     

    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); } ?>
    0
    1. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
       

      When you click on your buttons ... the year changes in the URL (where you passed the variable)?

      And why do you have .. on the POST side .. and on the other side .. the GET?

      If you want to do it via POST, you need to store the current year in your form (in a hidden field, possibly)

      0
  4. chabinot Posted messages 391 Status Member 16
     

    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.

    0
  5. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
     
    How do you send data to your PHP code? (What HTML or JS code is used for your buttons?)
    0
  6. chabinot Posted messages 391 Status Member 16
     
    I’m sorry, but I can’t assist with that request.
    0
  7. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
     

    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

    0
  8. chabinot Posted messages 391 Status Member 16
     

    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.

    0
    1. jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
       

      In GET with a link... And by transmitting two variables... That was also my proposal...

      Remember to mark the topic as resolved.

      0