Why not?

I have made my own calendar which is very simple. Here is what works: It will display the current month. I do get the twelve links I want from the current month plus eleven more. What I don’t get is when I select a link a variable or actually two variables are not passed so that the selection of month and year that was made is the new calendar month while the twelvelinks stay the same. This is a snipet of my first protram.
<?php

if (!isset($_GET[‘$month’])) {
$month = isset($_GET[‘month’]) ? intval($_GET[‘month’]) : date(‘m’);
}

if (!isset($_GET[‘$year’])) {
$year = isset($_GET{‘year’}) ? intval($_GET[‘year’]) : date(‘Y’);
}

$month_choice = date(‘m’);
$year_choice = date(‘Y’);

for ($i=1;$i<=12;$i++) {
if ($month_choice > 12) {
$month_choice = 1;
$year_choice+=1;
}
print ‘<li><a class=“pick_month” href=“where_we_are.php?$month=$month_choice&$year=$year_choice”>’ . date(‘M Y’,mktime(0,0,0,$month_choice,1,$year_choice)) . ‘</a></li>’;

$month_choice++;
}
?>

</ul>
</div>

<div id=“cal_grid”>
<?php

print ‘<table class=“calendar”><caption class=“month”>’ . date(‘F Y’,mktime(0,0,0,$month,1,$year)) . ‘</caption>’;
print ‘<tr><th>SUNDAY</th><th>MONDAY</th><th>TUESDAY</th><th>WEDNESDAY</th><th>THURSDAY</th><th>FRIDAY</th><th>SATURDAY</th></tr>’;
$totaldays = date(‘t’,mktime(0,0,0,$month,1,$year));
$offset = date(‘w’,mktime(0,0,0,$month,1,$year));
print ‘<tr>’;

if ($offset > 0) {

for ($i = 0;$i < $offset;$i++) {
print ‘<td></td>’;
}
}

for ($day =1;$day <= $totaldays;$day++) {
print “<td>” . $day . “</td>”;
$offset++;
if ($offset == 7) {
$offset = 0;
if ($day < $totaldays) {
print “</tr>
<tr>”;
}
}
}

if ($offset > 0) {
$offset = 7 - $offset;
}
if ($offset > 0) {
for ($i = 0;$i < $offset;$i++) {
print ‘<td></td>’;
}
}
print ‘</tr></table>’;
?>
</div>

Please look at the modified code below and find the changes what i have made.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="Raju Gautam" />
	<title>Calendar</title>
</head>
<body>
    <div style="width: 700px; margin: auto 0px; border: 1px solid #ccc;">
    <div>
        <ul>
        <?php
        $month = isset($_GET['month']) ? intval($_GET['month']) : date('m');
        $year = isset($_GET{'year'}) ? intval($_GET['year']) : date('Y');
        
        $month_choice = date('m');
        $year_choice = date('Y');
        
        for ($i = 1; $i <= 12; $i++) {
            if ($month_choice > 12) {
                $month_choice = 1;
                $year_choice += 1;
            }
            print "<li>
                        <a class=\\"pick_month\\" href=\\"where_we_are.php?month=$month_choice&amp;year=$year_choice\\">" . date('M Y', mktime(0, 0, 0, $month_choice, 1, $year_choice)) . "</a>
                    </li>";
            $month_choice++;
        }
        ?>
        </ul>
    </div>
    <div id="cal_grid">
        <?php
        print '<table class="calendar"><caption class="month">' . date('F Y', mktime(0, 0, 0, $month, 1, $year)) . '</caption>';
        print '<tr><th>SUNDAY</th><th>MONDAY</th><th>TUESDAY</th><th>WEDNESDAY</th><th>THURSDAY</th><th>FRIDAY</th><th>SATURDAY</th></tr>';
        $totaldays = date('t',mktime(0,0,0,$month,1,$year));
        $offset = date('w',mktime(0,0,0,$month,1,$year));
        print '<tr>';
        if ($offset > 0) {
            for ($i = 0; $i < $offset; $i++) {
                print '<td></td>';
            }
        }
        
        for ($day = 1; $day <= $totaldays; $day++) {
            print "<td>" . $day . "</td>";
            $offset++;
            if ($offset == 7) {
                $offset = 0;
                if ($day < $totaldays) {
                    print "</tr>\
<tr>";
                }
            }
        }
        
        if ($offset > 0) {
            $offset = 7 - $offset;
        }
        if ($offset > 0) {
            for ($i = 0;$i < $offset;$i++) {
                print '<td></td>';
            }
        }
        print '</tr></table>';
        ?>
    </div>
    </div>
</body>
</html>

I have made few changes to fit in my system like the filename is different than yours.

Hi teletubby, welcome to the forums,

For the benefit of those that prefer the code examples inside bbtags

<?php

if (!isset($_GET['$month'])) {
$month = isset($_GET['month']) ? intval($_GET['month']) : date('m');
}

if (!isset($_GET['$year'])) {
$year = isset($_GET{'year'}) ? intval($_GET['year']) : date('Y');
}

$month_choice = date('m');
$year_choice = date('Y');

for ($i=1;$i<=12;$i++) {
if ($month_choice > 12) {
$month_choice = 1;
$year_choice+=1;
}
print '<li><a class="pick_month" href="where_we_are.php?$month=$month_choice&amp;$year=$year_choice">' . date('M Y',mktime(0,0,0,$month_choice,1,$year_choice)) . '</a></li>';



$month_choice++;
}
?>

</ul>
</div>

<div id="cal_grid">
<?php

print '<table class="calendar"><caption class="month">' . date('F Y',mktime(0,0,0,$month,1,$year)) . '</caption>';
print '<tr><th>SUNDAY</th><th>MONDAY</th><th>TUESDAY</th><th>WEDNESDAY</th><th>THURSDAY</th><th>FRIDAY</th><th>SATURDAY</th></tr>';
$totaldays = date('t',mktime(0,0,0,$month,1,$year));
$offset = date('w',mktime(0,0,0,$month,1,$year));
print '<tr>';

if ($offset > 0) {

for ($i = 0;$i < $offset;$i++) {
print '<td></td>';
}
}

for ($day =1;$day <= $totaldays;$day++) {
print "<td>" . $day . "</td>";
$offset++;
if ($offset == 7) {
$offset = 0;
if ($day < $totaldays) {
print "</tr>\
<tr>";
}
}
}

if ($offset > 0) {
$offset = 7 - $offset;
}
if ($offset > 0) {
for ($i = 0;$i < $offset;$i++) {
print '<td></td>';
}
}
print '</tr></table>';
?>
</div>

First, you can reduce these

if (!isset($_GET['$month'])) {
$month = isset($_GET['month']) ? intval($_GET['month']) : date('m');
}

to

$month = isset($_GET['month']) ? intval($_GET['month']) : date('m');

No need to test twice, and as it is now, if it is set, the value won’t get assigned to $month. Does that fix things any?

Yes one is what Mittineague spotted and see the following line and compare with my code above:


print '<li><a class="pick_month" href="where_we_are.php?$month=$month_choice&amp;$year=$year_choice">' . date('M Y',mktime(0,0,0,$month_choice,1,$year_choice)) . '</a></li>';


print "<li><a class=\\"pick_month\\" href=\\"where_we_are.php?month=$month_choice&amp;year=$year_choice\\">" . date('M Y', mktime(0, 0, 0, $month_choice, 1, $year_choice)) . "</a></li>";

I modified the code and got a very interesting response. The first time that file is called it still works perfectly as it should. The second time it is called or when a link is pressed this happens: The linked list still shows up ok but the calendar displayed is for December 1999. What is interesting besides the fact that the choices are not being passed through is that the calendar display is correct. December 1999 does start where it is supposed to. The only problem is that this is not a valid choice.
<?php

$month = isset($_GET[‘month’]) ? intval($_GET[‘month’]) : date(‘m’);
$year = isset($_GET{‘year’}) ? intval($_GET[‘year’]) : date(‘Y’);

$month_choice = date(‘m’);
$year_choice = date(‘Y’);

for ($i=1;$i<=12;$i++) {
if ($month_choice > 12) {
$month_choice = 1;
$year_choice+=1;
}
print ‘<li><a class=“pick_month” href=“where_we_are.php?month=$month_choice&year=$year_choice”>’ . date(‘M Y’,mktime(0,0,0,$month_choice,1,$year_choice)) . ‘</a></li>’;

$month_choice++;
}

I finally got a chance to look at your new code. PHP does not parse variables or control bytes inside single quotes (unless you use heredoc syntax). so if you had something like

$var = 'word';
echo 'I know a $var or 2\
Do you know a $var?';

It will render
I know a $var or 2
Do you know a $var?
and look the same in view-source.

But if you use double quotes like

$var = 'word';
echo "I know a $var or 2\
Do you know a $var?";

it will render
I know a word or 2Do you know a word?
and view-source will be
I know a word or 2
Do you know a word?

Looking at your code in bbtags


	print '<li><a class="pick_month" href="teletubby.php?month=$month_choice&year=$year_choice">' . date('M Y',mktime(0,0,0,$month_choice,1,$year_choice)) . '</a></li>';
.....
print '<table class="calendar"><caption class="month">' . date('F Y',mktime(0,0,0,$month,1,$year)) . '</caption>';

you can see the difference between what is considered “text string” - red - and what is considered “code” - blue + green. (HTML grey, ints purple, etc.)

The hint is, one good way to see what is going on is to look at view-source. Looking at view-source of your existing code shows the links href values as opposed to only seeing the rendered link text.

If you do the same as you did for date() I think you’ll be pleased.

I am pleased! It worked exactly as prescribed!!!