Jump to content

show only week-days ONCE inside "for-loop" - how?


rootKID
 Share

Recommended Posts

Hello W3S! Been a while as always.

 

So my new project is a calendar. I got it working. I found some basic code online HOWEVER! I have a simple HTML-Table that generates the calendar dates (thats perfect), but the thing is, at the very TOP of that table, i wish to show the week-days for a single week (mon, tue, wed...) but only once and dynamicly if possible - just not sure on how to achieve this! Any ideas would be lovely! - PS: Sorry for my code... have not ordered anything just yet, trying to re-do a lot of code and re-write it so that i can understand it just a little better myself since its been a while ^^

Here is my current code - thanks n advance! :)

 

<?php
require_once("calendar_1-config.php");
require_once("calendar_1-functions.php");
?>
<!DOCTYPE html>
<?= $CONFIG___html_lang; ?>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>NORMAL KALENDER (+ EVENTS)</title>
        <!-- Favicons -->
        <link href="assets/img/favicon.png" rel="icon">
        <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">
        <!-- Vendor CSS -->
        <link href="assets/vendor/aos/aos.css" rel="stylesheet">
        <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
        <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
        <link href="assets/vendor/glightbox/css/glightbox.min.css" rel="stylesheet">
        <link href="assets/vendor/swiper/swiper-bundle.min.css" rel="stylesheet">
        <!-- Main CSS -->
        <link rel="stylesheet" href="assets/css/calendar_1-style.css">
    </head>
    <body>

        <?php

            # LINE-BREAKS AND A LINE -> just info for each variable and what they show!
            echo '<br />';
            echo '<hr />';
            echo '...(1) - NEXT PART...';
            echo '<br />';
            echo '...VARIABLES AND WHAT THEY SHOW EACH...';
            echo '<hr />';
            echo '<br />';

            # GET -> Year & Month
            if( isset($_GET['GET___year_AND_month']) )
            {
                $GET___year_AND_month = $_GET['GET___year_AND_month']; // Format: 1992-11 # Year, Month
            }
            else
            {
                $GET___year_AND_month = date('Y-m'); // Format: 1992-11 # Year, Month
            }
            echo '<p>"$GET___year_AND_month" -> <b>'.$GET___year_AND_month.'</b></p>';

            # Checking The Format
            $TIMESTAMP_UNIX___FROM___GET___year_AND_month = strtotime($GET___year_AND_month . '-01');
            if( $TIMESTAMP_UNIX___FROM___GET___year_AND_month === false )
            {
                $GET___year_AND_month = date('Y-m'); // Format: 1992-11 # Year, Month
                $TIMESTAMP_UNIX___FROM___GET___year_AND_month = strtotime($GET___year_AND_month . '-01');
            }
            echo '<p>"$GET___year_AND_month" -> <b>'.$GET___year_AND_month.'</b></p>';
            echo '<p>"$TIMESTAMP_UNIX___FROM___GET___year_AND_month" from "$GET___year_AND_month" -> <b>'.$TIMESTAMP_UNIX___FROM___GET___year_AND_month.'</b></p>';

            # Today (Format: yyyy-mm-dd)
            $today = date('Y-m-j'); // Format: 1992-11-08 # Year, Month, Day
            echo '<p>"$today" -> <b>'.$today.'</b></p>';

            # Title_1 (Format: MONTH_NAME, yyyy) -> (Example Format: November, 1992)
            $TITLE_1___CURRENT_month_AND_year = date('F, Y', $TIMESTAMP_UNIX___FROM___GET___year_AND_month);
            echo '<p>"$TITLE_1___CURRENT_month_AND_year" from "$TIMESTAMP_UNIX___FROM___GET___year_AND_month" -> <b>'.$TITLE_1___CURRENT_month_AND_year.'</b></p>';

            # Title_2 (Format: yyyy, MONTH_NAME, DATE (...1'st...)) -> (Example Format: 1992, November, 8'th)
            $TITLE_2 = date('Y, F, d\'S', $TIMESTAMP_UNIX___FROM___GET___year_AND_month);
            echo '<p>"$TITLE_2" from "$TIMESTAMP_UNIX___FROM___GET___year_AND_month" -> <b>'.$TITLE_2.'</b></p>';

            # CREATE LINKS FOR - Previous & Next - Month's
            $prev = date('Y-m', strtotime('-1 month', $TIMESTAMP_UNIX___FROM___GET___year_AND_month)); // Previous Month
            $next = date('Y-m', strtotime('+1 month', $TIMESTAMP_UNIX___FROM___GET___year_AND_month)); // Next Month
            echo '<p>"$prev" from "$TIMESTAMP_UNIX___FROM___GET___year_AND_month" -> <b>'.$prev.'</b></p>';
            echo '<p>"$next" from "$TIMESTAMP_UNIX___FROM___GET___year_AND_month" -> <b>'.$next.'</b></p>';

            # Total Numbers Of Days In The Month
            $month_days_count = date('t', $TIMESTAMP_UNIX___FROM___GET___year_AND_month);
            echo '<p>"$month_days_count" from "$TIMESTAMP_UNIX___FROM___GET___year_AND_month" -> <b>'.$month_days_count.'</b></p>';

            # 1:Monday,
            # 2:Tuesday,
            # 3:Wednesday,
            # 4:Thursday,
            # 5:Friday,
            # 6:Saturday,
            # 7 "0?":Sonday,
            # ...Day of the week...
            $STR___integer_string___of_the_day_of_the_week_we_are_in = date('N', $TIMESTAMP_UNIX___FROM___GET___year_AND_month); // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
            $STRING_1 = $STR___integer_string___of_the_day_of_the_week_we_are_in;
            echo '<p>"$STR___integer_string___of_the_day_of_the_week_we_are_in" from "$TIMESTAMP_UNIX___FROM___GET___year_AND_month" -> <b>We\'re currently in day nr.<u>'.$STR___integer_string___of_the_day_of_the_week_we_are_in.'</u> of the week!</b></p>';

            # ...Day of the year...
            $STR___integer_string___day_of_the_year = date('z', $TIMESTAMP_UNIX___FROM___GET___year_AND_month);
            // $STRING_2 = $STR___integer_string___day_of_the_year;
            echo '<p>"$STR___integer_string___day_of_the_year" from "$TIMESTAMP_UNIX___FROM___GET___year_AND_month" -> <b>We\'re currently in day nr.<u>'.$STR___integer_string___day_of_the_year.'</u> of the year!</b></p>';

            # ...Week number of the year...
            $STR___integer_string___week_of_the_year = date('W', $TIMESTAMP_UNIX___FROM___GET___year_AND_month);
            // $STRING_3 = $STR___integer_string___week_of_the_year;
            echo '<p>"$STR___integer_string___week_of_the_year" from "$TIMESTAMP_UNIX___FROM___GET___year_AND_month" -> <b>We\'re currently in week nr.<u>'.$STR___integer_string___week_of_the_year.'</u> of the year!</b></p>';

            # LINE-BREAKS AND A LINE
            echo '<br />';
            echo '<hr />';
            echo '...(2) - NEXT PART...';
            echo '<hr />';
            echo '<br />';

            # Array For Calendar
            // $weeks = [];
            // $week = '';
            $HTMLOUT_weeks = []; // Creating an array.
            $HTMLOUT_week = '';

            # Adding Empty Cell's
            $HTMLOUT_week .= str_repeat('<td></td>', $STRING_1 - 1); // str_repeat == "Repeat a string"

            # LINE-BREAKS AND A LINE
            echo '<br />';
            echo '<hr />';
            echo '...(3) - NEXT PART...';
            echo '<hr />';
            echo '<br />';

            # Programming The Calendar Output
            $TIMESTAMP_UNIX = $TIMESTAMP_UNIX___FROM___GET___year_AND_month;
            for( $day = 1; $day <= $month_days_count; $day++, $STRING_1++ )
            {
                $CALCULATE_dates = date('Y-m-'.$day, $TIMESTAMP_UNIX);

                $FIND_dates_names_FULL_DETAILS = date("D M j G:i:s T Y", strtotime($CALCULATE_dates));
                // echo $FIND_dates_names_FULL_DETAILS . '<br />';

                if( $today == $CALCULATE_dates )
                {
                    // $HTMLOUT_week .= '<td class="today" title="(FIND_CURRENT_dates_names_FULL_DETAILS) - mangler at lave følgende!">';
                    $HTMLOUT_week .= '<td style="background-color: rgba( 0, 0, 0, 0.5 );">';
                }
                else
                {
                    // $HTMLOUT_week .= '<td title="(FIND_CURRENT_dates_names_FULL_DETAILS) - mangler at lave følgende!">';
                    $HTMLOUT_week .= '<td>';
                }
                $HTMLOUT_week .= $day . '</td>';

                # Sunday OR Last Day Of The Month
                if( $STRING_1 % 7 == 0 || $day == $month_days_count )
                {
                    # Last Day(s) Of The Month
                    if( $day == $month_days_count && $STRING_1 % 7 != 0 )
                    {
                        # Add Empty Cell's
                        $HTMLOUT_week .= str_repeat('<td></td>', 7 - $STRING_1 % 7);
                    }

                    // $HTMLOUT_weeks[] = '<tr>';
                    //     $HTMLOUT_weeks[] = '<td align="middle">';
                    //         // $HTMLOUT_weeks[] = '<b>MAN</b>';
                    //         $HTMLOUT_weeks[] = '<b>Man</b>';
                    //     $HTMLOUT_weeks[] = '</td>';
                    //     $HTMLOUT_weeks[] = '<td align="middle">';
                    //         // $HTMLOUT_weeks[] = '<b>TIR</b>';
                    //         $HTMLOUT_weeks[] = '<b>Tir</b>';
                    //     $HTMLOUT_weeks[] = '</td>';
                    //     $HTMLOUT_weeks[] = '<td align="middle">';
                    //         // $HTMLOUT_weeks[] = '<b>ONS</b>';
                    //         $HTMLOUT_weeks[] = '<b>Ons</b>';
                    //     $HTMLOUT_weeks[] = '</td>';
                    //     $HTMLOUT_weeks[] = '<td align="middle">';
                    //         // $HTMLOUT_weeks[] = '<b>TOR</b>';
                    //         $HTMLOUT_weeks[] = '<b>Tor</b>';
                    //     $HTMLOUT_weeks[] = '</td>';
                    //     $HTMLOUT_weeks[] = '<td align="middle">';
                    //         // $HTMLOUT_weeks[] = '<b>FRE</b>';
                    //         $HTMLOUT_weeks[] = '<b>Fre</b>';
                    //     $HTMLOUT_weeks[] = '</td>';
                    //     $HTMLOUT_weeks[] = '<td align="middle">';
                    //         // $HTMLOUT_weeks[] = '<b>LØR</b>';
                    //         $HTMLOUT_weeks[] = '<b>Lør</b>';
                    //     $HTMLOUT_weeks[] = '</td>';
                    //     $HTMLOUT_weeks[] = '<td align="middle">';
                    //         // $HTMLOUT_weeks[] = '<b>SØN</b>';
                    //         $HTMLOUT_weeks[] = '<b>Søn</b>';
                    //     $HTMLOUT_weeks[] = '</td>';
                    // $HTMLOUT_weeks[] = '</tr>';

                    $HTMLOUT_weeks[] = '<tr>' . $HTMLOUT_week . '</tr>';
                    $HTMLOUT_week = '';
                }
            }

            echo '<div class="container-fluid">';
                echo '<div class="row">';
                    echo '<div class="col-lg-12">';

                        echo '<div class="table-responsive">';
                            echo '<table class="table table-bordered">';

                                echo '<tr>';

                                    echo '<td colspan="0">';
                                        echo '&nbsp;';
                                    echo '</td>';

                                    echo '<td align="right">';
                                        echo '<a href="#">';
                                            echo '&lt; PREV';
                                        echo '</a>';
                                    echo '</td>';

                                    echo '<td colspan="0">';
                                        echo '&nbsp;';
                                    echo '</td>';

                                    echo '<td align="middle">';
                                        echo '<a href="#">';
                                            echo 'PRESENT';
                                        echo '</a>';
                                    echo '</td>';

                                    echo '<td colspan="0">';
                                        echo '&nbsp;';
                                    echo '</td>';

                                    echo '<td align="left">';
                                        echo '<a href="#">';
                                            echo 'NEXT &gt;';
                                        echo '</a>';
                                    echo '</td>';

                                    echo '<td colspan="0">';
                                        echo '&nbsp;';
                                    echo '</td>';

                                echo '</tr>';

                                foreach( $HTMLOUT_weeks as $week )
                                {
                                    echo $week;
                                }

                            echo '</table>';
                        echo '</div>';

                    echo '</div>';
                echo '</div>';
            echo '</div>';

            /*
            // echo '<table border="1" cellpadding="0" cellspacing="0">';
            echo '<table class="table table-bordered">';

                echo '<tr>';

                    echo '<td colspan="0">';
                        echo '&nbsp;';
                    echo '</td>';

                    echo '<td align="right">';
                        echo '<a href="#">';
                            echo '&lt; PREV';
                        echo '</a>';
                    echo '</td>';

                    echo '<td colspan="0">';
                        echo '&nbsp;';
                    echo '</td>';

                    echo '<td align="middle">';
                        echo '<a href="#">';
                            echo 'PRESENT';
                        echo '</a>';
                    echo '</td>';

                    echo '<td colspan="0">';
                        echo '&nbsp;';
                    echo '</td>';

                    echo '<td align="left">';
                        echo '<a href="#">';
                            echo 'NEXT &gt;';
                        echo '</a>';
                    echo '</td>';

                    echo '<td colspan="0">';
                        echo '&nbsp;';
                    echo '</td>';

                echo '</tr>';

            echo '</table>';
            */

        ?>

        <!-- Vendor JS -->
        <script src="assets/vendor/aos/aos.js"></script>
        <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
        <script src="assets/vendor/glightbox/js/glightbox.min.js"></script>
        <script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
        <script src="assets/vendor/purecounter/purecounter.js"></script>
        <script src="assets/vendor/swiper/swiper-bundle.min.js"></script>
        <script src="assets/vendor/typed.js/typed.min.js"></script>
        <script src="assets/vendor/waypoints/noframework.waypoints.js"></script>
        <!-- Main JS -->
        <script src="assets/js/main.js"></script>

    </body>
</html>

 

Link to comment
Share on other sites

The way to solve it is to set the table headers outside of the loop, before the loop begins. In the case of your code it can be done near the line where $HTMLOUT_weeks is first initialized.

# Array For Calendar
// $weeks = [];
// $week = '';
$HTMLOUT_weeks = []; // Creating an array.
$HTMLOUT_week = '';

// Add weekdays header
$HTMLOUT_weeks[] = '<tr><th>Man</th><th>Tir</th><th>Ons</th><th>Tor</th><th>Fre</th><th>Lør</th><th>Søn</th></tr>';

 

Link to comment
Share on other sites

16 hours ago, Ingolme said:

The way to solve it is to set the table headers outside of the loop, before the loop begins. In the case of your code it can be done near the line where $HTMLOUT_weeks is first initialized.

# Array For Calendar
// $weeks = [];
// $week = '';
$HTMLOUT_weeks = []; // Creating an array.
$HTMLOUT_week = '';

// Add weekdays header
$HTMLOUT_weeks[] = '<tr><th>Man</th><th>Tir</th><th>Ons</th><th>Tor</th><th>Fre</th><th>Lør</th><th>Søn</th></tr>';

 

Thanks alot! However, is it not possible to do this dynamicly? Would i be able to create another loop maybe (instead of a static array you do here, just replacing this with a loop? - and should it be a for loop like the one under?) - will take a look at it either way when home - on the phone right now.

Thanks for the answer so far tho! :)

Link to comment
Share on other sites

The names of the weekdays each need to be added manually unless you can pull them from a database. Using a loop here would just make the code longer and less organized. If you really want to add another loop, you can create an array of weekday names and construct the HTML for a table row by looping through them.


// Add weekdays header
$weekday_names = ['Man','Tir','Ons','Tor','Fre','Lør','Søn'];
$header = '<tr>';
foreach($weekday_names as $name) {
  $header .= "<td>$name</td>";
}
$header .= '</tr>';
$HTMLOUT_weeks[] = $header;

 

Link to comment
Share on other sites

Hi. I just got home and thanks for the quick reply - i think i will have some considering to do but otherwise both options looks pretty good to me. Thanks for the support! For now i will do your first option but i will still consider your second optional code for future ^^

Many thanks ^^

Link to comment
Share on other sites

It would be better to separate the HTML and the PHP to make the code much easier to maintain. The PHP should be all at the beginning of the document before anything, even before the <!DOCTYPE> declaration.

Here's an example of how to separate HTML from PHP.

CSS (goes in the <head>)

<style>
.highlight { background: rgba(0, 0, 0, 0.5); }
</style>

HTML template (goes in the <body>)

<table class="table table-bordered calendar">

    <tr>
        <td colspan="0">&nbsp;</td>
        <td align="right"><a href="?GET___year_AND_month=<?php echo $prev; ?>">&lt; PREV</a></td>
        <td colspan="0">&nbsp;</td>
        <td align="middle"><a href="?GET___year_AND_month=<?php echo $year_month; ?>">PRESENT</a></td>
        <td colspan="0">&nbsp;</td>
        <td align="left"><a href="?GET___year_AND_month=<?php echo $next; ?>">NEXT &gt;</a></td>
        <td colspan="0">&nbsp;</td>
    </tr>
    
    <tr>
        <th>Man</th>
        <th>Tir</th>
        <th>Ons</th>
        <th>Tor</th>
        <th>Fre</th>
        <th>Lør</th>
        <th>Søn</th>
    </tr>
    
    <?php foreach($table_content as $week) { ?>
        <tr>
            <?php foreach($week as $day) { ?>
                <?php if($today == "$year_month-$day") { ?>
                    <td class="highlight"><?php echo $day; ?></td>
                <?php } else { ?>
                    <td><?php echo $day; ?></td>
                <?php } ?>
            <?php } ?>
        </tr>
    <?php } ?>

</table>

PHP logic (goes at the beginning of the document):

<?php
/* Collect data */

// Create a timestamp from user input or, if user input is invalid, the current month
$year_month = $_GET['GET___year_AND_month'] ?? date('Y-m');
$timestamp = strtotime($year_month . '-01');
if($timestamp === false ) {
  $year_month = date('Y-m');
  $timestamp = strtotime($year_month . '-01');
}

// Get current date
$today = date('Y-m-j');

// Navigation links
$prev = date('Y-m', strtotime('-1 month', $timestamp)); // Previous Month
$next = date('Y-m', strtotime('+1 month', $timestamp)); // Next Month

// Data information
$month_days_count = date('t', $timestamp); // Days in the month
$current_weekday = date('N', $timestamp); // Weekday number (0-6 -> Sun-Mon)

/* Construct content for the table cells */

// Initialize variables
$weekday_counter = $current_weekday;
$table_content = [];

// Add empty cells for weekdays preceding the beginning of the month
$table_content[0] = array_fill(0, $weekday_counter - 1, '');

// Populate the array
$week = 0;
for($day = 1; $day <= $month_days_count; $day++, $weekday_counter++ ) {
  $table_content[$week][] = (string)$day;
  
  // Increment week counter
  if($weekday_counter % 7 == 0) {
    $week++;
    $table_content[$week] = [];
  }
}

// Add cells for weekdays following the end of the month
while(count($table_content[$week]) < 7) {
  $table_content[$week][] = '';
}
?>

 

Link to comment
Share on other sites

5 hours ago, Ingolme said:
// Add empty cells for weekdays preceding the beginning of the month
$table_content[0] = array_fill(0, $weekday_counter - 1, '');

Hmm, it seems much easier, yes. But... i have to ask, in the peace of code you have here, what are you doing? I mean, when you minus 1 with weekday counter, that part i am a bit unsure what does to be honest...

Link to comment
Share on other sites

That line of code is equivalent to this line in your original code:

# Adding Empty Cell's
$HTMLOUT_week .= str_repeat('<td></td>', $STRING_1 - 1); // str_repeat == "Repeat a string"

You can see the minus 1 there too.

It creates empty cells for each weekday before the first day of the month. Since empty cells are only needed before the first day and not exactly on the first day, we subtract 1 from the weekday number of the first day of the month.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...