Jump to content

[Newbie's trouble] Variable does not work


angelodev

Recommended Posts

	If ((!$_GET['month']) && (!$_GET['year'])) {
	$month = Date("n");
	$year = Date("Y");
} else {
	$month = $_GET['month'];
	$year = $_GET['year'];
}

//Month calculation calculated
$timestamp = mktime(0, 0, 0, $month, 1, $year);
$monthname = date("F", $timestamp);

?>
<table class="calendar">
<tr>
	<td colspan="7"><?php echo $month ?></td>
</tr>
<tr>
	<td>Su</td>
	<td>M</td>
	<td>Tu</td>
	<td>W</td>
	<td>Th</td>
	<td>F</td>
	<td>Sa</td>
</tr>
</table>

 

I would like to create a calendar top header with this ode. But it does not work!!

 

Have you any advice?

 

PS: these are my first PHP code lines :)

Link to comment
https://forums.phpfreaks.com/topic/50043-newbies-trouble-variable-does-not-work/
Share on other sites

I see one recommendation to make with your code that may help...

 

If ((!$_GET['month']) && (!$_GET['year'])) 
{
      $month = Date("n");
      $year = Date("Y");
} 
else 
{
    $month = $_GET['month'];
    $year = $_GET['year'];
}

 

The IF line checking for the availability of Month and Year is actually just checking for a TRUE value, which is faulty if you are trying to see if the variable has been set.

 

Use the following code for clarity and probably better results on your IF statement.

 

if(isset($_GET['month']) == FALSE && isset($_GET['year']) == FALSE)
{
    $month = date('n');
    $year = date('y');
}
else
{
    $month = $_GET['month'];
    $year = $_GET['year'];
}

 

Now, if you want to be really fancy, you can use the conditional operators ? and : This really cleans up your code lines, but sometimes isn't as clear to the reader of your code.

 

$month = isset($_GET['month']) ? $_GET['month'] : date('n');
$year = isset($_GET['year']) ? $_GET['year'] : date('y');

 

To help you understand the last code snippet, it is a single line IF.

 

$RETURN_VALUE = (CONDITION) ? IF_TRUE RETURN VALUE : IF_FALSE RETURN VALUE;

 

So,

 

$month = (IF THE MONTH VALUE IS SET) ? $_GET['month']: ELSE RETURN date('n');

Ok, I agree wth you. My last post wasn't clear....

 

<table class="calendar">
<tr>
	<td colspan="7"><?php echo $month ?></td>
</tr>
<tr>
	<td>Su</td>
	<td>M</td>
	<td>Tu</td>
	<td>W</td>
	<td>Th</td>
	<td>F</td>
	<td>Sa</td>
</tr>

 

The echo line doesn't print the month name at the table's top.

I think that the variale is correcly load.

 

A litte ajax script call this php functionfor load a dynamic calendar i the home page.

 

 

The ';' isn't completely necassary in that situation.

 

Best to check if the variable isn't defined:

 

if (!$_GET['month']) && !$_GET['year'])) 
{
      $month = date('n');
      $year = date('Y');
} else{
    $month = $_GET['month'];
    $year = $_GET['year'];
}

Also with the variables definet it does not wor.

 

The strange think is that the echo function insite the tale doesn't print anything: either with the $month var and a fixed string.

 

I thinj that the trouble is located there.

 

echo "Ciao!" -> doesn't print anything in the first row

If yo cut ad past this code

<?php

//calendar.php

//Check if the month and year values exist
if ((!$_GET['month']) && (!$_GET['year'])) {
	$month = date ("n");
	$year = date ("Y");
} else {
	$month = $_GET['month'];
	$year = $_GET['year'];
}

//Calculate the viewed month
$timestamp = mktime (0, 0, 0, $month, 1, $year);
$monthname = date("F", $timestamp);
echo $monthname;
?>

 

into your script: $monthname is displayed?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.