Loop problem

Hello, here is my problem: [php]for($i=1, $c=1, $t=date(‘j’), $m=date(‘m’), $y=date(‘Y’);$c<=$this->_daysInMonth; ++$i)
{
/*
* Apply a “fill” class to the boxes occurring before
* the first of the month
/
$class = $i<=$this->_startDay ? “fill” : NULL;
/

* Add a “today” class if the current date matches
* the current date
*/

		if ( $c==$t && $m==$this->_m && $y==$this->_y )
		{
		$class = "today";
		}
		/*
		* Build the opening and closing list item tags
		*/
		$ls = sprintf("\n\t\t<li class=\"%s\">", $class);
		$le = "\n\t\t</li>";
		
		/**
		*Add the day of the month to identify the calendar box
		*/
		
		if ($i>$this->_startDay && $this->_daysInMonth>=$c)
        {
			/*
             * Format events data
             */
            $event_info = NULL;
            if ( isset($events[$c]) )
            {
				foreach ( $events[$c] as $event )
                {
					$link = '<a href="view.php?event_id='
                            . $event->id . '">' . $event->title
                            . '</a>';
                    $event_info .= "\n\t\t\t$link";
				}	
            }
			$date = sprintf("\n\t\t\t<strong>%02d</strong>",$c++);
        }
		else { $date="&nbsp;"; }
		/**
		*IF the current day is saturday, wrap to the next row
		*/
		$wrap = $i!=0 && $i%7==0?"\n\t</ul>\n\t<ul>":NULL;
		
		/**
		*Assemple the pieces into finished items
		*/
		
		$html .= $ls . $date . $event_info . $le . $wrap;
		
	}[/php]

The problem is at some point $i is getting values from 3to32 in the loop where $event_info variable is,$c is getting 1-30 as it should be for the current month. I couldn’t find where $i is affected. Hope you can help me.TY.
P.S. The code is taken from the book “Pro php and jquery”

Hi, $i is not being effected obscurely, it is simply being used as a way to see if 7 days have passed.

I would not recommend using $i for anything as $c is actually the day counter.

If you need to see if $i is being affected, echo it out in the main loop, its starting from 1 and going all the way to 32 if I set a 31 calendar month.

[php]function lastMonth($date) {
$previous = new DateTime($date, “-1 Month”); // Grab last Month:
$previous->modify(“last day of the month”); // Grab the last day of the Month:
$prevMonth = $previous->format(“w”); // Numerical day of the week the last month’s day falls on:
/* Because day of the week in the “w” format starts with 0 for Sunday the if statements are needed /
if ($prevMonth != 6) {
for ($x = $prevMonth; $x >= 0; $x–) {
/
Subtract the number of days of last month from the current day position. /
$calendar[] = ‘

  • ’ . ($previous->format(“t”) - $x) . ‘
  • ’ . “\n”;
    }
    } else {
    for ($x = 6; $x >= 0; $x–) {
    /
    Subtract the number of days of last month from the current day position. */
    $calendar[] = ‘

  • ’ . ($previous->format(“t”) - $x) . ‘
  • ’ . “\n”;
    }
    }
    return $calendar;
    }[/php]

    [php]function currentMonth($date) {
    $today = new DateTime(“now”);
    $current = new DateTime($date);
    $days = $current->format(“t”); // Total number of days in current month:
    for($x=1; $x < $days; $x++) {
    if ($x < 10) {
    $urlDate = $current->format(“Y”) . ‘-’ . $current->format(“m”) . ‘-0’ . $x;
    } else {
    $urlDate = $current->format(“Y”) . ‘-’ . $current->format(“m”) . $x;
    }
    if ( $today->format(“Y-m-d”) === $urlDate) {
    $highlightToday = “highlightToday”;
    }
    $calendar[] = ‘

  • ’ . $x . “
  • \n”;
    } // End of loop
    return $calendar[];
    }[/php]

    [php]/* Filler Day Function */
    function addRow($dayPos = 1, $totalDays = 0) {
    for ($x = $dayPos; $x <= (7 + $totalDays); $x++) {
    $calendar[] = ‘

  • ’ . $x . ‘
  • ’ . “\n”;
    }
    return $calendar
    }

    /* Next month filler days /
    function nextMonth($date) {
    /
    Advance to the next month by adding +1 month to current date /
    $next = new DateTime($date . ‘+1 Month’);
    /
    Need to set the month to the first day of the month /
    $next->modify(‘first day of this Month’);
    /
    Grab how many day’s position in the week /
    $nextMonth = $next->format(‘w’);
    /
    Fill the week with days of next month if month ends on Saturday /
    if ($nextMonth != 0) {
    /
    Subtract 7 form days to get the correct day position /
    for ($x = 1; $x <= (7 - $nextMonth); $x++) {
    $calendar[] = ‘

  • ’ . $x . ‘
  • ’ . “\n”;
    }
    } else {
    $calendar = $addRow(); // Tack on an extra week if the month ends on a Saturday:
    }
    /
    If calendar month only has 5 weeks (35 days) tack on an extra week /
    if ( sizeof($calendar) === 35) {
    /
    $dayPos is really next month’s day position on Current Caleandar /
    $dayPos = 8 - $nextMonth;
    /
    Total days that need to be filled to make it a week */
    $totalDays = 7 - $nextMonth;
    $calendar = $addRow($dayPos, $totalDays);
    }
    return $calendar;
    }[/php]

    then to call it:

    [php]$date = “August 28, 2015”; // Just an example date:

    $lastMonth = lastMonth($date);
    $currentMonth = currentMonth($date);
    $nextMonth = nextMonth($date);

    $calendar = array_merge($lastMonth, $currentMonth, $nextMonth);[/php]

    Now I can’t say that it will work perfectly for I converted this over from OOP to procedural PHP. However this is how I would make a calendar. You could put calendar events in the currentMonth’s function.

    Many thanks, but I doesn’t answer my question: Where the code I provided crashes and gives me 3 and 32 values in the foreach loop?Regards.

    Well, they did answer your question, you did not have a well-formed IF clause! andrevanzuydam gave
    you the answer, Strider64 gave you a more robust solution…

    Your IF clause was bad… A standard form of one is: for($i=1; $i<=somevalue; ++$i) … It basically is set
    with a starting value, a condition to stop at and an increment (or decrement) value… Yours had the starting
    value of setting variable $i and then comparing a condition using the $c variable, but, incrementing the $i one.
    Does not make sense… It should be done something more like:
    for($i=1; $i<=$this->_daysInMonth; ++$i)
    $t=date(‘j’);
    $m=date(‘m’);
    $y=date(‘Y’);

    Or, even better set these three variables BEFORE you run the FOR loop…

    I just can NOT believe that someone wrote a book and placed this code into it… Or, maybe you copied it
    wrong… Either way bad format in the FOR clause…

    https://books.google.bg/books?id=U65WoAoPdX8C&pg=PA153&lpg=PA153&dq=pro+php+and+jquery+$event_info&source=bl&ots=mbr1iecx6E&sig=06hRrjtYeQaMlsOvrfvCGbon3Ps&hl=bg&sa=X&sqi=2&ved=0CCEQ6AEwAGoVChMIlfyQx-vuxwIVLSvbCh3mDAQV#v=onepage&q=pro%20php%20and%20jquery%20%24event_info&f=false
    Here it is from where I took the code.Regards.

    Sponsor our Newsletter | Privacy Policy | Terms of Service