Reputation: 11
I am creating a calendar function in php. Along with the function I need a "previous" and "next" link that show the previous or next month using the GET method. The links don't work the way I expect them to. From what I've found through debugging it doesn't look like it's actually adding or subtracting 1 from month.
This is currently what I have:
$month=$_GET["month"];//should initially set them to null?
$year=$_GET["year"];
//previous and next links
echo "<a href='calendar.php?month=<?php echo ($month-1)?>'>Previous</a>";
echo "<a href='calendar.php?month=<?php echo ($month+1)?>'>Next</a>";
//Calls calendar method that returns the calendar in a string
$calDisplay=calendar($month,$year);
echo $calDisplay;
Upvotes: 0
Views: 22196
Reputation: 1431
The $_SERVER REQUEST METHOD when you click on a link is 'GET'.
use the parse_str function and place your $_GET keys and variables into an array.
parse_str($_SERVER['QUERY_STRING'],$output);
print_r($output);
Maybe you can test for that. Is this what you are looking for?
Upvotes: 0
Reputation: 10643
Try doing something like the following:
echo sprintf('<a href="calendar.php?%s">Previous</a>', http_build_query(array('month' => $month - 1)));
echo sprintf('<a href="calendar.php?%s">Next</a>', http_build_query(array('month' => $month + 1)));
While it seems more convoluted, it serves two purposes.
sprintf
and http_build_query
functions which can be very useful in certain situations. sprintf
is a string formatting function which basically takes a string as its first parameter and substitutes certain tokens with the next n
parameters. In this example, %s
is a token which means replace it with a string, which is passed as the second function. http_build_query
takes an associative array and builds a http query from it. So in the code above, the http_build_query
function will return month=11
(assuming the month was 12) for the Previous
link. You can also add multiple array parameters and it will build the query string with the ampersands.While the other answers do what you need, its always wise to look at and understand other PHP functions.
Upvotes: 0
Reputation: 116110
PHP doesn't do calculations inside strings and doesn't parse PHP tags inside strings. You are already in 'PHP mode', and opening another PHP tag inside the string just outputs that tag as you may have noticed when you inspected the link in your browser.
Instead, try closing the string, concatenating the next/previous month (using the dot operator), and concatenating the last part of the link:
//previous and next links
echo "<a href='calendar.php?month=" . ($month-1) . "'>Previous</a>";
echo "<a href='calendar.php?month=" . ($month+1) . "'>Next</a>";
You can also calculate the values into variables first, because simple variables can be used inside double-quoted strings:
//previous and next links
$previousMonth = $month-1;
$nextMonth = $month+1;
echo "<a href='calendar.php?month=$previousMonth'>Previous</a>";
echo "<a href='calendar.php?month=$nextMonth'>Next</a>";
On the first request, you may not have a month at all, so you may want to check for that too, for instance using isset.
$month = 1;
if (isset($_GET['month'])) {
$month = (int)$_GET['month'];
}
As you can see, I already did an (int) typecast there too. Combining this with the variables version, allows you to make the code a little more solid by performing some checks on the input, and only output the previous/next links if they make sense.
$month = 1;
if (isset($_GET['month'])) {
$month = (int)$_GET['month'];
}
if ($month < 1 || $month > 12) {
// Invalid month. You can choose to throw an exception, or just
// ignore it and use a default, like this;
$month = 1;
}
//previous and next links, if necessary.
$previousMonth = $month-1;
$nextMonth = $month+1;
if ($previousMonth >= 0) {
echo "<a href='calendar.php?month=$previousMonth'>Previous</a>";
}
if ($nextMonth <= 12) {
echo "<a href='calendar.php?month=$nextMonth'>Next</a>";
}
Oh, and a minor detail. Personally I don't like to put 'big' chunks of HTML inside a string, so I'd rather use some template, or at least write it like this. As you can see, you can close and open PHP tags (just not inside strings), so you can output plain HTML from within your PHP code. The <?= $x ?>
notation is a shorthand for <? echo $x; ?>
.
//previous and next links, if necessary.
$previousMonth = $month-1;
$nextMonth = $month+1;
if ($previousMonth >= 0) {?>
<a href='calendar.php?month=<?=$previousMonth?>'>Previous</a>
<?php}
if ($nextMonth <= 12) {?>
<a href='calendar.php?month=<?=$nextMonth?>'>Next</a>
<?}
Upvotes: 3
Reputation: 1853
Since everything you send via request is considered a String, try this casting integer. HTML
<!-- previous and next links -->
<a href='calendar.php?month=<?php echo ((int)$month - 1)?>'>Previous</a>
<a href='calendar.php?month=<?php echo ((int)$month + 1)?>'>Next</a>
Upvotes: -1