Making some variables refer to Session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lucadg
Forum Newbie
Posts: 7
Joined: Fri Aug 04, 2006 11:48 am

Making some variables refer to Session

Post by lucadg »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, 
I am a php beginner, really working hard to learn, but this time it looks like I am trying to do something beyond my means 
This page shows an availability calendar in apartment pages.
It starts showing  the current month (so, november now) but if a visitor is interested in March he'll have to click until it reaches March in every new apartment he goes.
I want that once a new month has been selected (so, March in this example), it stays in a Session variable, so that every apartment visited will show the same month.

This is the original code:

Code: Select all

<?php 
require_once('../include/IncFiles/functions.php');
require_once('../include/IncFiles/Variables.php');
require_once('../include/class.db.php');

//get initial date
$mnow=(isset($HTTP_GET_VARS['month']) && intval($HTTP_GET_VARS['month'])>0 && intval($HTTP_GET_VARS['month'])<13)?intval($HTTP_GET_VARS['month']):date("m");
$ynow=(isset($HTTP_GET_VARS['year']) && intval($HTTP_GET_VARS['year'])>1969 && intval($HTTP_GET_VARS['year'])<2038)?intval($HTTP_GET_VARS['year']):date("Y");
$mtime=mktime(0,0,0,$mnow,date("d"),$ynow);  //int mktime(int hour, int minute, int second, int month, int day, int year);
$f=date("w",mktime(0,0,0,$mnow,1,$ynow))-1;  //set start date column
// if ($mnow < "10") $mnow="0".$mnow;

$apartmentid=$_GET['apartmentid'];

//query selected month reservations

$datearray1=array();
$datearray2=array();
$arraycount=0;
$startdefault=date("Y-m-d", mktime(0,0,0,$mnow,1,$ynow));
$enddefault=date("Y-m-d", mktime(0,0,0,$mnow,31,$ynow));
$sql="select abtype, intotime, endtime from t_order where apartmentid=$apartmentid order by intotime";
$db->query($sql);
	if($db->num_rows())
	{
		while (($db->next_record()))
		{
// Start  Do not show requested dates (abtype 2) Luca 21/05/2006 18.29.30

	  	$abtype=$db->f('abtype'); 
if ($abtype !="2") {


// End Do not show requested dates (abtype 2) 

        	$startdate=$db->f('intotime');
			$enddate=$db->f('endtime');
			$starty=substr($startdate,0,4);
			$startm=substr($startdate,5,2);
			$startd=substr($startdate,8,2);
			$endy=substr($enddate,0,4);
			$endm=substr($enddate,5,2);
			$endd=(substr($enddate,8,2));  

} // Belongs Do not show requested dates 
			if (($mnow == $startm) && ($ynow == $starty)) 
			{
				$datearray1[$arraycount]=$startd;
				if ($endm == $startm) 
				{
					$datearray2[$arraycount]=$endd;
				}else
				{
					$datearray2[$arraycount]=31;
				}
				$arraycount++;				
			}else if (($mnow == $endm) && ($ynow == $endy))
			{
				if ($endm == $startm) 
				{
					$datearray1[$arraycount]=$startd;
				}else
				{
					$datearray1[$arraycount]=1;
				}
				$datearray2[$arraycount]=$endd;
				$arraycount++;
			}
		}
	}
	
for ($x=0;$x<$arraycount;$x++) 
{
    
	//echo "list, from ".$datearray1[$x]." to ".$datearray2[$x]."<br>";
}


$tt="";
//set table
//$tt.="<table id=\"calendar\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\">";
//set title
$tt.="<tr bgcolor=\"CC0000\">
<td colspan=\"4\" align=\"center\" class=\"calendartitle\">
<a href=\"?".($mnow!=date("m")?"month=".intval($mnow)."&":"")."year=".(intval($ynow)==1970?"1970":intval($ynow)-1)."&apartmentid=".$apartmentid."\" class=\"calendartitle\"><-&nbsp;</a>".$ynow."
<a href=\"?".($mnow!=date("m")?"month=".intval($mnow)."&":"")."year=".(intval($ynow)==2037?"2037":intval($ynow)+1)."&apartmentid=".$apartmentid."\" class=\"calendartitle\">&nbsp;-></a></td>
<td colspan=\"3\" align=\"center\" class=\"calendartitle\">

<a href=\"?month=".(intval($mnow)==1?"12":intval($mnow)-1).($ynow!=date("Y")?"&year=".intval($ynow):"")."&apartmentid=".$apartmentid."\" class=\"calendartitle\"><-&nbsp;</a>".$mnow."
<a href=\"?month=".(intval($mnow)==12?"1":intval($mnow)+1).($ynow!=date("Y")?"&year=".intval($ynow):"")."&apartmentid=".$apartmentid."\" class=\"calendartitle\">&nbsp;-></a>
</td>
</tr>";
$tt.="<tr bgcolor=\"FF6666\"><td class=\"calendartop\">Sun</td><td class=\"calendartop\">Mon</td><td class=\"calendartop\">Tue</td><td class=\"calendartop\">Wed</td><td class=\"calendartop\">Thu</td><td class=\"calendartop\">Fri</td><td class=\"calendartop\">Sat</td></tr>";
//set date
for($i=0;$i<date("t",$mtime)+$f+1;$i++){
if($i%7==0)echo "<tr>";
$tt.="<td align=center ".(($i-$f==intval(date("d")) && $mnow==date("m") && $ynow==date("Y"))?" id=\"calendartoday\"":"");
$count=0;
for ($x=0;$x<$arraycount;$x++)
{
	$cdate=$i-$f;
	if ((($cdate>=$datearray1[$x]) && ($cdate<=$datearray2[$x])) && ($count == 0))
	{
		$tt.=" bgcolor=\"#FFCCCC\">"."<span class=\"system_abtable_reserved\">";
		$count=1;
	}
	//$x=$arraycount;
}
if ($count == 0) $tt.=">";
if($i>$f) 
{
	$cdate=$i-$f; //hide former blank fields
	if ($cdate < 10) $cdate="0".$cdate;
	$tt.=$cdate;
	if ($count == 1) $tt.="</span>";
}
$tt.="</td>";
if($i%7==6) $tt.="</tr>";
}
//set rest blank fields
if($i%7<6 && $i%7>0)$tt.="<td colspan=\"".(7-$i%7)."\"></td></tr>";
if($i%7==6) $tt.="<td></td></tr>";
//$tt.="</table>";
?>

<html>
<head>
<link href="../css/system.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style></head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td><div align="center">
      <table width="240" border="2" cellpadding="0" cellspacing="2" bordercolor="#CCCCCC" bgcolor="#F4F4F4">
        <tr>
          <td bgcolor="#CCCCCC"><div align="center" class="article_title">Apartment Availabilities</div></td>
        </tr>
        <tr>
          <td><table width="230" height="200" border="1" align="center" cellpadding="2" cellspacing="1" bordercolor="#EEEEEE" bgcolor="#FFFFFF" class="system_abtable" id="calendar">
            <?
			echo $tt;
		?>
          </table></td>
        </tr>
      </table>
    </div></td>
  </tr>
</table>
</body>
</html>


Thank you !

Luca


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
lucadg
Forum Newbie
Posts: 7
Joined: Fri Aug 04, 2006 11:48 am

Post by lucadg »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I had to post twice as at the moment I have a very slow connection.
With the modifications I have done, when trying to change month, the month does not change.
Here is my sorry trial:

Code: Select all

<?php 
require_once('../include/IncFiles/functions.php');
require_once('../include/IncFiles/Variables.php');
require_once('../include/class.db.php');
session_start();

if (!IsSet($_SESSION['mnow'])) {
//get initial date
$mnow=(isset($HTTP_GET_VARS['month']) && intval($HTTP_GET_VARS['month'])>0 && intval($HTTP_GET_VARS['month'])<13)?intval($HTTP_GET_VARS['month']):date("m");
$ynow=(isset($HTTP_GET_VARS['year']) && intval($HTTP_GET_VARS['year'])>1969 && intval($HTTP_GET_VARS['year'])<2038)?intval($HTTP_GET_VARS['year']):date("Y");
$mtime=mktime(0,0,0,$mnow,date("d"),$ynow);  //int mktime(int hour, int minute, int second, int month, int day, int year);
$f=date("w",mktime(0,0,0,$mnow,1,$ynow))-1;  //set start date column
// if ($mnow < "10") $mnow="0".$mnow;

$_SESSION['mnow'] = $mnow;
$_SESSION['ynow'] = $ynow;
$_SESSION['mtime'] = $mtime;
$_SESSION['f'] = $f;

}

else {

$mnow = $_SESSION['mnow'] ;
$ynow = $_SESSION['ynow'];
$mtime = $_SESSION['mtime'];
$f = $_SESSION['f'];

}

$apartmentid=$_GET['apartmentid'];

//query selected month reservations

$datearray1=array();
$datearray2=array();
$arraycount=0;
$startdefault=date("Y-m-d", mktime(0,0,0,$mnow,1,$ynow));
$enddefault=date("Y-m-d", mktime(0,0,0,$mnow,31,$ynow));
$sql="select abtype, intotime, endtime from t_order where apartmentid=$apartmentid order by intotime";
$db->query($sql);
	if($db->num_rows())
	{
		while (($db->next_record()))
		{
// Start  Do not show requested dates (abtype 2) Luca 21/05/2006 18.29.30

	  	$abtype=$db->f('abtype'); 
if ($abtype !="2") {


// End Do not show requested dates (abtype 2) 

        	$startdate=$db->f('intotime');
			$enddate=$db->f('endtime');
			$starty=substr($startdate,0,4);
			$startm=substr($startdate,5,2);
			$startd=substr($startdate,8,2);
			$endy=substr($enddate,0,4);
			$endm=substr($enddate,5,2);
			$endd=(substr($enddate,8,2));  

} // Belongs Do not show requested dates 
			if (($mnow == $startm) && ($ynow == $starty)) 
			{
				$datearray1[$arraycount]=$startd;
				if ($endm == $startm) 
				{
					$datearray2[$arraycount]=$endd;
				}else
				{
					$datearray2[$arraycount]=31;
				}
				$arraycount++;				
			}else if (($mnow == $endm) && ($ynow == $endy))
			{
				if ($endm == $startm) 
				{
					$datearray1[$arraycount]=$startd;
				}else
				{
					$datearray1[$arraycount]=1;
				}
				$datearray2[$arraycount]=$endd;
				$arraycount++;
			}
		}
	}
	
for ($x=0;$x<$arraycount;$x++) 
{
    
	//echo "list, from ".$datearray1[$x]." to ".$datearray2[$x]."<br>";
}


$tt="";
//set table
//$tt.="<table id=\"calendar\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\">";
//set title
$tt.="<tr bgcolor=\"CC0000\">
<td colspan=\"4\" align=\"center\" class=\"calendartitle\">
<a href=\"?".($mnow!=date("m")?"month=".intval($mnow)."&":"")."year=".(intval($ynow)==1970?"1970":intval($ynow)-1)."&apartmentid=".$apartmentid."\" class=\"calendartitle\"><-&nbsp;</a>".$ynow."
<a href=\"?".($mnow!=date("m")?"month=".intval($mnow)."&":"")."year=".(intval($ynow)==2037?"2037":intval($ynow)+1)."&apartmentid=".$apartmentid."\" class=\"calendartitle\">&nbsp;-></a></td>
<td colspan=\"3\" align=\"center\" class=\"calendartitle\">

<a href=\"?month=".(intval($mnow)==1?"12":intval($mnow)-1).($ynow!=date("Y")?"&year=".intval($ynow):"")."&apartmentid=".$apartmentid."\" class=\"calendartitle\"><-&nbsp;</a>".$mnow."
<a href=\"?month=".(intval($mnow)==12?"1":intval($mnow)+1).($ynow!=date("Y")?"&year=".intval($ynow):"")."&apartmentid=".$apartmentid."\" class=\"calendartitle\">&nbsp;-></a>


</td>
</tr>";

$_SESSION['mnow'] = $mnow;
$_SESSION['ynow'] = $ynow;
$_SESSION['mtime'] = $mtime;
$_SESSION['f'] = $f;

$tt.="<tr bgcolor=\"FF6666\"><td class=\"calendartop\">Sun</td><td class=\"calendartop\">Mon</td><td class=\"calendartop\">Tue</td><td class=\"calendartop\">Wed</td><td class=\"calendartop\">Thu</td><td class=\"calendartop\">Fri</td><td class=\"calendartop\">Sat</td></tr>";
//set date
for($i=0;$i<date("t",$mtime)+$f+1;$i++){
if($i%7==0)echo "<tr>";
$tt.="<td align=center ".(($i-$f==intval(date("d")) && $mnow==date("m") && $ynow==date("Y"))?" id=\"calendartoday\"":"");
$count=0;
for ($x=0;$x<$arraycount;$x++)
{
	$cdate=$i-$f;
	if ((($cdate>=$datearray1[$x]) && ($cdate<=$datearray2[$x])) && ($count == 0))
	{
		$tt.=" bgcolor=\"#FFCCCC\">"."<span class=\"system_abtable_reserved\">";
		$count=1;
	}
	//$x=$arraycount;
}
if ($count == 0) $tt.=">";
if($i>$f) 
{
	$cdate=$i-$f; //hide former blank fields
	if ($cdate < 10) $cdate="0".$cdate;
	$tt.=$cdate;
	if ($count == 1) $tt.="</span>";
}
$tt.="</td>";
if($i%7==6) $tt.="</tr>";
}
//set rest blank fields
if($i%7<6 && $i%7>0)$tt.="<td colspan=\"".(7-$i%7)."\"></td></tr>";
if($i%7==6) $tt.="<td></td></tr>";
//$tt.="</table>";
?>

<html>
<head>
<link href="../css/system.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style></head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td><div align="center">
      <table width="240" border="2" cellpadding="0" cellspacing="2" bordercolor="#CCCCCC" bgcolor="#F4F4F4">
        <tr>
          <td bgcolor="#CCCCCC"><div align="center" class="article_title">Apartment Availabilities</div></td>
        </tr>
        <tr>
          <td><table width="230" height="200" border="1" align="center" cellpadding="2" cellspacing="1" bordercolor="#EEEEEE" bgcolor="#FFFFFF" class="system_abtable" id="calendar">
            <?
			echo $tt;
		?>
          </table></td>
        </tr>
      </table>
    </div></td>
  </tr>
</table>
</body>
</html>
I know it's a lot of code, and I am posting not actually expecting a fixed code, just some hints if somebody has time.
As I am working locally I can't show the output.

Thank you !

Luca


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Making some variables refer to Session

Post by timvw »

lucadg wrote:H
I want that once a new month has been selected (so, March in this example), it stays in a Session variable, so that every apartment visited will show the same month.
Here's the basic logic, wrapping all your html (and whatever around it) is up to you:

Code: Select all

// only start a session if it hasn't been started yet
if (!isset($_SESSION)) {
 session_start();
}

// check if the user has submitted a month
if (isset($_POST['month'])) {
 // i left it out, but it's important to verify if the value is really an integer between 1 and 12. 
 $month = $_POST['month'];
 $_SESSION['month'] = $month;
} else {
 // check if the user had already choosen a month
 if (isset($_SESSION['month'])) {
  $month = $_SESSION['month'];
 } else {
  // use the default value
  $month = date('m');
 }
}
lucadg
Forum Newbie
Posts: 7
Joined: Fri Aug 04, 2006 11:48 am

Post by lucadg »

Hi timvw,
Thanks for the help. It took me 5 days but I did it.
You suggestion helped.
Bye
Luca
Post Reply