Jump to content

Operations with variables from different arrays


Recommended Posts

Hello,

I am a PHP biginner. I would be grateful if you could help with the issue below.

I have created three input dynamic variables in a separate php file using a For lopp.

<?php
                    
                    For ($i=1; $i<=31; $i++) {
                    
                    echo '<div class="row">';
                    echo '<div class="col span-1-of-4">';
                    
                    //Dates en français
                                    
                                    $jour = $jour + 1;
                                    $dateTest = $annee . "-" . $mois . "-" . $jour;
                                    $date = date_create("$annee-$mois-$jour");
                        
                    echo '<input name="workday[]" id="workday[]" class="donnee-pointage1" value="';
                    echo date_format($date, 'd/m/Y');
                    echo '">';
                        echo " ";
                    echo '</div>';
                        echo '<div class="col span-1-of-4">';
                            echo '<input type="time" name="startTime[]" id="startTime[]">';
                        echo '</div>';
                        echo '<div class="col span-1-of-4">';
                            echo '<input type="time" name="endTime[]" id="endTime[]">';
                        echo '</div>';                    
                    echo '</div>';
                        
                    }
                    
       ?>

 

The form works well.

In another PHP I would like to use the three arrays workday[], startTime[] and endTime to calculate the duration between startTime and endTime for each working day. I can access the different arrays using Foreach like below but I don't see how to combine thos arrays in order to be able to calculate the duration between two data that are in diffrent arrays. Is it possible to make calculations using variables stored in different arrays ? If so could you help me understand how ?

 

<?php

                if (isset($_POST['workday'])) {
                    foreach ($_POST['workday'] as $i => $workday) {

                   echo $workday . "<br>;

                    }
                }


                if (isset($_POST['startTime'])) {
                    foreach ($_POST['startTime'] as $i => $startTime) {

                    echo $startTime. "<br>;
                    }
                }

                if (isset($_POST['endTime'])) {
                    foreach ($_POST['endTime'] as $i => $endTime) {

                     echo $endTime. "<br>;
                    }
                }

               
                }
            
        ?>

Link to post
Share on other sites

One way would be to rename your input fields to something like

name = 'jour[$jour][workday]'
name = 'jour[$jour][startTime]'
name = 'jour[$jour][endTime]'

your posted data will then be nicely grouped

Array
(
    [jour] => Array
        (
            [1] => Array
                (
                    [workday] => 01/12/2020
                    [startTime] => 08:00
                    [endTime] => 06:00
                )

            [2] => Array
                (
                    [workday] => 02/12/2020
                    [startTime] => 08:30
                    [endTime] => 06:30
                )

            [3] => Array
                (
                    [workday] => 03/12/2020
                    [startTime] => 09:00
                    [endTime] => 04:30
                )

             . . .
)

 

Edited by Barand
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.