jwdbjbebefin jwdbjbebefin - 4 months ago 9
PHP Question

Show every number up to a certain number given in a variable

I have a bit of a problem, I'm trying to make a calendar which I can then change the color of each box depending on the result from the database. at the moment my code gives me how many days there are in the year. is there a way which I can make say for instance 30 boxed with each number ascending to 30, in a way which will let me select a certain number of a box later on and make that a certain color.

<?php
$number = cal_days_in_month(CAL_GREGORIAN, 2, 2003);
?>


I have tried ways I use on fetching information from rows in databases which obviously will not work.
Kind Regards
Jack

Answer

It is hoped, with all sincerity, that we are in sync here. One can presume he understands what you meant but then it may not be that precise. Anyways, you can loop through the variable $number with a for(){} loop and then create your boxes (perhaps an HTML <div> Element) with unique css classes to help you styleit as you wish. Here's what is assumed to be one probable idea:

    <?php
        $number     = cal_days_in_month(CAL_GREGORIAN, 2, 2003);

        // LOOP THROUGH THE NUMBER OF MONTHS USING FOR-LOOP AND CREATE
        // THE BOXES THAT YOU DESIRE (WITH CSS-CLASSES, THOUGH)
        $calBoxes   = "";
        for($cue=1; $cue<=$number; $cue++){
            $fmtDay    = date("D", strtotime("2003-2-{$cue}"));
            $calBoxes .= "<div class='date-box date-box-{$cue}'>" . PHP_EOL;
            $calBoxes .= "<span class='date-number date-number-{$cue}'>{$fmtDay}</span>" . PHP_EOL;
            $calBoxes .= "<span class='weekday weekday-{$cue} weekday-" . strtolower($fmtDay) . "'>{$cue}</span>" . PHP_EOL;
            $calBoxes .= "</div>" . PHP_EOL;
        }

        var_dump($calBoxes);
        // PRODUCES...          
        <div class='date-box date-box-1'>
            <span class='date-number date-number-1'>Sat</span>
            <span class='weekday weekday-1 weekday-sat'>1</span>
        </div>
        <div class='date-box date-box-2'>
            <span class='date-number date-number-2'>Sun</span>
            <span class='weekday weekday-2 weekday-sun'>2</span>
        </div>
        <div class='date-box date-box-3'>
            <span class='date-number date-number-3'>Mon</span>
            <span class='weekday weekday-3 weekday-mon'>3</span>
        </div>
        <div class='date-box date-box-4'>
            <span class='date-number date-number-4'>Tue</span>
            <span class='weekday weekday-4 weekday-tue'>4</span>
        </div>
        <div class='date-box date-box-5'>
            <span class='date-number date-number-5'>Wed</span>
            <span class='weekday weekday-5 weekday-wed'>5</span>
        </div>
        <div class='date-box date-box-6'>
            <span class='date-number date-number-6'>Thu</span>
            <span class='weekday weekday-6 weekday-thu'>6</span>
        </div>
        <div class='date-box date-box-7'>
            <span class='date-number date-number-7'>Fri</span>
            <span class='weekday weekday-7 weekday-fri'>7</span>
        </div>
        <div class='date-box date-box-8'>
            <span class='date-number date-number-8'>Sat</span>
            <span class='weekday weekday-8 weekday-sat'>8</span>
        </div>
        <div class='date-box date-box-9'>
            <span class='date-number date-number-9'>Sun</span>
            <span class='weekday weekday-9 weekday-sun'>9</span>
        </div>
        <div class='date-box date-box-10'>
        <span class='date-number date-number-10'>Mon</span>
        <span class='weekday weekday-10 weekday-mon'>10</span>
        </div>
        <div class='date-box date-box-11'>
            <span class='date-number date-number-11'>Tue</span>
            <span class='weekday weekday-11 weekday-tue'>11</span>
        </div>
        <div class='date-box date-box-12'>
            <span class='date-number date-number-12'>Wed</span>
            <span class='weekday weekday-12 weekday-wed'>12</span>
        </div>
        <div class='date-box date-box-13'>
            <span class='date-number date-number-13'>Thu</span>
            <span class='weekday weekday-13 weekday-thu'>13</span>
        </div>
        <div class='date-box date-box-14'>
            <span class='date-number date-number-14'>Fri</span>
            <span class='weekday weekday-14 weekday-fri'>14</span>
        </div>
        <div class='date-box date-box-15'>
            <span class='date-number date-number-15'>Sat</span>
            <span class='weekday weekday-15 weekday-sat'>15</span>
        </div>...

Hope this gives you (at least) a little clue...

Cheers ;-)

Comments