Janessa Bautista
Janessa Bautista

Reputation: 294

How can i Loop TABLE TD with specific date and time range in PHP HTML

I am trying to build a for loop so that i don't need to write it one by one,

i have a list 16 TD.

If today is February 25,

the TH hour will have its value of

ID  |  HOUR       |60 |30 |45 |0 |ADJUSTED |HG |STRETCH |UG |HC
1      Feb 25 6PM
2      Feb 25 7PM
3      Feb 25 8PM
4      Feb 25 9PM
5      Feb 25 10PM
6      Feb 25 11PM
7      Feb 25 12AM
8      Feb 26 1AM
9      Feb 26 2AM
10     Feb 26 3AM
11     Feb 26 4AM
12     Feb 26 5AM
13     Feb 26 6AM
14     Feb 26 7AM
15     Feb 26 8AM

how can i make a loop consist of date?

here is the code that i have currently.

 <table class="table table-striped" id="user_set_goal_table" width="100%">
                         <thead>
                             <tr>
                                 <th>#</th>
                                 <th>Hour</th>
                                 <th>HC-60</th>
                                 <th>HC-30</th>
                                 <th>HC-45</th>
                                 <th>HC-0</th>
                                 <th class="th_red">Adjusted HC</th>
                           <th class="th_yellow">Hourly Goal</th>
                           <th class="th_yellow">Stretch</th>
                           <th class="th_yellow">Updated Goal</th>
                           <th class="th_green">Transfers/HC</th>
                             </tr>
                         </thead>
                         <tbody>
                        <?php
                        for ($i = 1;$i <= 16;$i++)
                        {
                        ?>
                         <tr>
                             <td><?php echo $i; ?></td>
                             <td><?php echo "a" . $i; ?></td>
                            <td><?php echo "<input type='text'  id='hc_60_" . $i . "'  "; ?></td>
                             <td><?php echo "<input type='text'  id='hc_30_" . $i . "'  "; ?></td>
                            <td><?php echo "<input type='text'  id='hc_45_" . $i . "'  "; ?></td>
                            <td><?php echo "<input type='text'  id='hc_0_" . $i . "'  "; ?></td>
                            <td><?php echo "f" . $i; ?></td>
                            <td><?php echo "g" . $i; ?></td>
                            <td><?php echo "h" . $i; ?></td>
                            <td><?php echo "i" . $i; ?></td>
                            <td><?php echo "j" . $i; ?></td>
                         </tr>
                         <?php
                         }
                        ?>
                         </tbody>
                     </table>

the time is always static the only thing will change is the date. any help would be really appreciated.

Upvotes: 0

Views: 458

Answers (3)

Deepak Singh
Deepak Singh

Reputation: 42

Please use datatable ajax request method:

HTML:

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </tfoot>
</table>

jQuery:

$(document).ready(function() {
$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "server-site.php"
} );

} );

check out for reference:

Datatable Server Side Process

Upvotes: -2

Beniamin
Beniamin

Reputation: 568

If we stay with your code you can simply use modify() method on date object for example:

$referenceDate = new DateTimeImmutable('today 06:00PM');

for ($i = 1; $i <= 16; $i++) {
    //...
    echo $referenceDate->modify(sprintf("+ %s hours", $i))->format('M d gA');
    //...
}

Upvotes: 3

PatNowak
PatNowak

Reputation: 5812

I think you should very similar for or foreach loop for range of days you have.

  1. You have to specify the range of $days you want to process (with hardcoded values, DateTime etc.

  2. Loop through it (this loop is outer for existing one in your code)

<?php foreach ($days as $day) {
?>
 // here put your loop and use $day variable
<?php
}
?>

Upvotes: 1

Related Questions