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
Share on other sites

Can you describe what does not work means please? Just glancing at the code it looks pretty close. If you can provide more insight as to what it's doing perhaps we can provide you with a better answer.

Link to comment
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');

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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'];
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.