Jump to content

[SOLVED] while in a for loop or vice versa? this is kicking my butt :-(


datafan

Recommended Posts

I have a flat file with lines of room reservation entries like this... epochdate|classroom#|startTime|endTime|reservationFileName

I have all the script done that compares the entries and will only let you reserve a room/rooms if they are not already in use, then writes the reservation to the flat file above.

 

I am now moding a calendar script for my application: As each day of the calendar is "written" I am trying to compare it to my flat file entries and put a link to that reservation in that day of the calendar.

 

The thing is I am trying to loop through my flat file with "while" inside of the "for" that builds the calendar. I just can't seem to get it to work right. Maybe I am going at this all wrong. Here is my code. I am only working on the first week at the moment as once I get this week I can do the same thing for the rest of the days. Thanks for taking a look.

 

Just updated the code below thanks.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body bgcolor="white">
<basefont face="arial" size="4" color="black">

<?php
if($_POST['submit']){

function showCalendar($ChosenMonth){
    // Get key day informations. 
    // We need the first and last day of the month and the actual day
$today = getdate();
$pickmonth = getdate(mktime(0,0,0,$ChosenMonth,1,2008));
    $firstDay = getdate(mktime(0,0,0,$pickmonth['mon'],1,$pickmonth['year']));
    $lastDay  = getdate(mktime(0,0,0,$pickmonth['mon']+1,0,$pickmonth['year']));

//convert pickmonth to epoch time string to compare with flat file entries
$pickepoch = date("U", mktime(0, 0, 0,$pickmonth['mon'],$pickmonth['mday'],$pickmonth['year']));
$endmonth = date("U", mktime(11, 59, 59,$lastDay['mon'],$lastDay['mday'],$lastDay['year']));
//echo $pickepoch."<br/>";
//echo $endmonth."<br/>";

//Add 24 hours to give us the entire day to compare our flat file readings to
$pickepoch2 = $pickepoch + 86399;
//echo $pickepoch2."<br/>";
//echo date("gisnjy",$pickepoch2);

//open flat file in read mode and build an epoch time array to check our calendar days against
$fp = fopen("masterflat.txt",'r'); 
if (!$fp) {
echo 'ERROR: Unable to open file.';
exit;
}

    
// Create a table with the necessary header informations
    echo '<table align="center">';
    echo '  <tr><th colspan="7">'.$pickmonth['month']." - ".$pickmonth['year']."</th></tr>";
    echo '<tr class="days">';
    echo '  <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
    echo '  <td>Fr</td><td>Sa</td><td>Su</td></tr>';
    
    
    // Display the first calendar row with correct positioning
    echo '<tr>';
    for($i=1;$i<$firstDay['wday'];$i++){
        echo '<td> </td>';
    }
    $actday = 0;
    for($i=$firstDay['wday'];$i<=7;$i++){
        $actday++;

	$pickday = getdate(mktime(0,0,0,$ChosenMonth,$actday,2008));
	$pickdayep = date("U", mktime(0, 0, 0,$pickday['mon'],$pickday['mday'],$pickday['year']));
	$pickdayepend= $pickdayep + 86400;
	$pickit = date("j", $pickdayep);

        if ($actday == $today['mday'] && $pickmonth['mon'] == $today['mon'] && $pickmonth['year'] == $today['year']) {
            $class = ' class="actday"';
        } else {
            $class = '';
        }
	while (!feof($fp)) {
		$buffer = fgets($fp, 1024);
		$flatlist = list ($field1,$field2,$field3,$field4,$field5) = split ('\|', $buffer);
		if ($flatlist[0] >= $pickepoch && $flatlist[0] <= $endmonth){
		    $flatday = date("j", $flatlist[0]);

			if ($pickit == $flatday) {
			$bigstring = "<a href = reservations/".$flatlist[4].">".$flatlist[1]."-".$flatlist[2]."-".$flatlist[3]."</a><br/>";
		             }
                         }

	}
        echo "<td".$class."/>".$actday.$bigstring."</td>";

    }
    echo '</tr>';
    
    //Get how many complete weeks are in the actual month
    $fullWeeks = floor(($lastDay['mday']-$actday)/7);
    
    for ($i=0;$i<$fullWeeks;$i++){
        echo '<tr>';
        for ($j=0;$j<7;$j++){
            $actday++;
		$pickday = getdate(mktime(0,0,0,$ChosenMonth,$actday,2008));
		$pickdayep = date("U", mktime(0, 0, 0,$pickday['mon'],$pickday['mday'],$pickday['year']));
		$pickit = date("j", $pickdayep);
		            
		if ($actday == $today['mday'] && $pickmonth['mon'] == $today['mon'] && $pickmonth['year'] == $today['year']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            echo "<td$class>$actday</td>";
        }
        echo '</tr>';
    }
    
    //Now display the rest of the month
    if ($actday < $lastDay['mday']){
        echo '<tr>';
        
        for ($i=0; $i<7;$i++){
            $actday++;

	    if ($actday == $today['mday'] && $pickmonth['mon'] == $today['mon'] && $pickmonth['year'] == $today['year']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            
            if ($actday <= $lastDay['mday']){

			$pickday = getdate(mktime(0,0,0,$ChosenMonth,$actday,2008));
			$pickdayep = date("U", mktime(0, 0, 0,$pickday['mon'],$pickday['mday'],$pickday['year']));
			$pickit = date("j", $pickdayep);

			echo "<td$class>$actday</td>";
            }
            else {
                echo '<td> </td>';
            }
        }
        
        
        echo '</tr>';
    }


fclose($fp);


?>
</table>
<table align="center" width="910">
<tr height="30"><td align="center" width="455">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<select size="2" name="ChosenMonth">
<?php 
if ($ChosenMonth > 1){
?>
<option value="<?php echo $ChosenMonth - 1;?>">Previous Month</option>
<?php
}
if ($ChosenMonth < 12) {
?>
<option value="<?php echo $ChosenMonth + 1;?>">Next Month</option>
<?php
}
?>
<input type="submit" value="Go" name="submit"></form>
</td>
<td width="455">
<a href="calcreate.php"><font size="6">HOME</font></a>
</center>
</td></tr></table>
<?php
    //echo '</table>';
}

$ChosenMonth = $_POST['ChosenMonth'];
showCalendar($ChosenMonth);
}else{
?>
<center>
<p><font size="6">Welcome To Our 2008 Calendar Reservation System</font><br>
Courtesy of Jim Marquardt</p>
<p><font size="5">Please Select A Month From The Drop Down Menu Below</font></p>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<select size="12" name="ChosenMonth">
  <option selected value="1">January</option>
  <option value="2">February</option>
  <option value="3">March</option>
  <option value="4">April</option>
  <option value="5">May</option>
  <option value="6">June</option>
  <option value="7">July</option>
  <option value="8">August</option>
  <option value="9">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select>
<input type="submit" value="Open" name="submit">
</form>
</center>
<?php
}
?>

</body>
</html>


 

 

Link to comment
Share on other sites

I cant really tell where your problem is, but I can offer some debugging advice. Throw some echos in areas where things are supposed to happen. For example, before this line

 

if ($pickit == $flatday) {

 

do

 

echo $pickit ,' date from file: ', $flastday ,'<br />';

 

and see if the dates even line up

Link to comment
Share on other sites

Here is my latest attemp with a ton of echos.

I am not getting the $actday correctly inside the while loop to do my comparison too. Check it out.

 

http://www.tamstouch.com/calcreate.php

 

$actday = 0;
    for($i=$firstDay['wday'];$i<=7;$i++){
        $actday++;

	$pickday = getdate(mktime(0,0,0,$ChosenMonth,$actday,2008));
	$pickdayep = date("U", mktime(0, 0, 0,$pickday['mon'],$pickday['mday'],$pickday['year']));
	$pickdayepend= $pickdayep + 86400;
	$pickit = date("j", $pickdayep);
	echo $pickdayep. " --in for loop-- " .$pickdayepend. " --flat list zero-- ".$flatlist[0]."<br/>";

        if ($actday == $today['mday'] && $pickmonth['mon'] == $today['mon'] && $pickmonth['year'] == $today['year']) {
            $class = ' class="actday"';
        } else {
            $class = '';
        }
	while (!feof($fp)) {
		$buffer = fgets($fp, 1024);
		$flatlist = list ($field1,$field2,$field3,$field4,$field5) = split ('\|', $buffer);
		if ($flatlist[0] >= $pickepoch && $flatlist[0] <= $endmonth){
			$flatday = date("j", $flatlist[0]);
			echo $pickdayep. " --in while loop-- " .$pickdayepend. " --flat list zero-- " .$flatlist[0]. "<br/>";
			if ($flatlist[0] >= $pickdayep && $flatlist[0] <= $pickdayepend) { 
			$bigstring = "<a href = reservations/".$flatlist[4].">".$flatlist[1]."-".$flatlist[2]."-".$flatlist[3]."</a><br/>";
			}	
		}
	}
        echo "<td".$class."/>".$actday.$bigstring."</td>";

Link to comment
Share on other sites

>:( I guess it can't be done, I am so sick of trying this I am ready to scrap the idea... sorry just venting after wasting yet another day on this crap. This sucks.

I guess you can't compare multiple values from a flat file against the variable $actday as the calendar is being built. Latest attemp...

 

http://www.tamstouch.com/calcreate.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body bgcolor="white">
<basefont face="arial" size="4" color="black">

<?php
if($_POST['submit']){

function showCalendar($ChosenMonth){
    // Get key day informations. 
    // We need the first and last day of the month and the actual day
$today = getdate();
$pickmonth = getdate(mktime(0,0,0,$ChosenMonth,1,2008));
    $firstDay = getdate(mktime(0,0,0,$pickmonth['mon'],1,$pickmonth['year']));
    $lastDay  = getdate(mktime(0,0,0,$pickmonth['mon']+1,0,$pickmonth['year']));

//convert pickmonth to epoch time string to compare with flat file entries
$pickepoch = date("U", mktime(0, 0, 0,$pickmonth['mon'],$pickmonth['mday'],$pickmonth['year']));
$endmonth = date("U", mktime(11, 59, 59,$lastDay['mon'],$lastDay['mday'],$lastDay['year']));
//echo $pickepoch."<br/>";
//echo $endmonth."<br/>";

//Add 24 hours to give us the entire day to compare our flat file readings to
$pickepoch2 = $pickepoch + 86399;
//echo $pickepoch2."<br/>";
//echo date("gisnjy",$pickepoch2);

//open flat file in read mode and build an epoch time array to check our calendar days against
$fp = fopen("masterflat.txt",'r'); 
if (!$fp) {
echo 'ERROR: Unable to open file.';
exit;
}

while (!feof($fp)) {
$buffer = fgets($fp, 1024);
list ($field1,$field2,$field3,$field4,$field5) = split ('\|', $buffer);

//echo $pickepoch. "<br>";
//echo $field1. "<br>";
//echo $endmonth. "<br>";

if ($field1 >= $pickepoch && $field1 <= $endmonth){	
$flatlist = array($field1, $field2, $field3, $field4, $field5, $flatday); 
$flatday = date("j", $flatlist[0]);
//echo $flatday."<br/>";



}
}
foreach ($flatday as $key => $value){
echo $value;
//print_r(array_values($));
}

/*$arrays = count($flatlist[0]) - 1; 
$loop = -1; 
while ($loop < $arrays) { 
$loop++; 
$flatday = date("j", $flatlist[0]);
echo $flatday. "<br>";			
}*/

$r++;
if ($r < 2){
    
// Create a table with the necessary header informations
    echo '<table align="center">';
    echo '  <tr><th colspan="7">'.$pickmonth['month']." - ".$pickmonth['year']."</th></tr>";
    echo '<tr class="days">';
    echo '  <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
    echo '  <td>Fr</td><td>Sa</td><td>Su</td></tr>';
    
    
    // Display the first calendar row with correct positioning
    echo '<tr>';
    for($i=1;$i<$firstDay['wday'];$i++){
        echo '<td> </td>';
    }
    $actday = 0;
    for($i=$firstDay['wday'];$i<=7;$i++){
    $actday++;
$pickday = getdate(mktime(0,0,0,$ChosenMonth,$actday,2008));
$pickdayep = date("U", mktime(0, 0, 0,$pickday['mon'],$pickday['mday'],$pickday['year']));
$pickdayepend= $pickdayep + 86400;
$pickit = date("j", $pickdayep);

	if ($actday == $today['mday'] && $pickmonth['mon'] == $today['mon'] && $pickmonth['year'] == $today['year']) {
        $class = ' class="actday"';
        } else {
        $class = '';
        }

        echo "<td".$class."/>".$actday;


		if ($flatday == $pickit){
		$bigstring = "<a href = reservations/".$flatlist[4].">".$flatlist[1]." | ".$flatlist[2]."-".$flatlist[3]."</a><br/>";
		echo "<br>".$bigstring."</td>";	
		}else{

	echo "</td>";	

	}
    }
    echo '</tr>';
    
    //Get how many complete weeks are in the actual month
    $fullWeeks = floor(($lastDay['mday']-$actday)/7);
    
    for ($i=0;$i<$fullWeeks;$i++){
        echo '<tr>';
        for ($j=0;$j<7;$j++){
            $actday++;
		$pickday = getdate(mktime(0,0,0,$ChosenMonth,$actday,2008));
		$pickdayep = date("U", mktime(0, 0, 0,$pickday['mon'],$pickday['mday'],$pickday['year']));
		$pickit = date("j", $pickdayep);
		            
		if ($actday == $today['mday'] && $pickmonth['mon'] == $today['mon'] && $pickmonth['year'] == $today['year']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            echo "<td$class>$actday</td>";
        }
        echo '</tr>';
    }
    
    //Now display the rest of the month
    if ($actday < $lastDay['mday']){
        echo '<tr>';
        
        for ($i=0; $i<7;$i++){
            $actday++;

	    if ($actday == $today['mday'] && $pickmonth['mon'] == $today['mon'] && $pickmonth['year'] == $today['year']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            
            if ($actday <= $lastDay['mday']){

			$pickday = getdate(mktime(0,0,0,$ChosenMonth,$actday,2008));
			$pickdayep = date("U", mktime(0, 0, 0,$pickday['mon'],$pickday['mday'],$pickday['year']));
			$pickit = date("j", $pickdayep);

			echo "<td$class>$actday</td>";
            }
            else {
                echo '<td> </td>';
            }
        }
        
        
        echo '</tr>';
    }
}

fclose($fp);


?>
</table>
<table align="center" width="910">
<tr height="30"><td align="center" width="455">
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<select size="2" name="ChosenMonth">
<?php 
if ($ChosenMonth > 1){
?>
<option value="<?php echo $ChosenMonth - 1;?>">Previous Month</option>
<?php
}
if ($ChosenMonth < 12) {
?>
<option value="<?php echo $ChosenMonth + 1;?>">Next Month</option>
<?php
}
?>
<input type="submit" value="Go" name="submit"></form>
</td>
<td width="455">
<a href="calcreate.php"><font size="6">HOME</font></a>
</center>
</td></tr></table>
<?php
    //echo '</table>';
}

$ChosenMonth = $_POST['ChosenMonth'];
showCalendar($ChosenMonth);
}else{
?>
<center>
<p><font size="6">Welcome To Our 2008 Calendar Reservation System</font><br>
Courtesy of Jim Marquardt</p>
<p><font size="5">Please Select A Month From The Drop Down Menu Below</font></p>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<select size="12" name="ChosenMonth">
  <option selected value="1">January</option>
  <option value="2">February</option>
  <option value="3">March</option>
  <option value="4">April</option>
  <option value="5">May</option>
  <option value="6">June</option>
  <option value="7">July</option>
  <option value="8">August</option>
  <option value="9">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select>
<input type="submit" value="Open" name="submit">
</form>
</center>
<?php
}
?>

</body>
</html>

Link to comment
Share on other sites

I am going to go at this from another angle. Instead of building the calendar pages dynamically, I am going to build them manually with html, then set up each day field as a $variable. I think then then i can run my while loop on the flat file and do a if ($flatday === $calday) echo blah blah blah; type thing

Link to comment
Share on other sites

Disreagard my last post, just when I was about to throw in the towel on it, I figured it out. lol

 

Here is the portion of the code that did it for me

 

<?php
$actday = 0;
for($i=$firstDay['wday'];$i<=7;$i++){
    $actday++;

$pickday = getdate(mktime(0,0,0,$ChosenMonth,$actday,2008));
$pickdayep = date("U", mktime(0, 0, 0,$pickday['mon'],$pickday['mday'],$pickday['year']));
$pickdayepend= $pickdayep + 86400;

if ($actday == $today['mday'] && $pickmonth['mon'] == $today['mon'] && $pickmonth['year'] == $today['year']) {
	$class = ' class="actday"';
    }else{
        $class = '';
    }

echo "<td$class>$actday";
$flatlist = file("masterflat.txt");
foreach($flatlist as $row) {
	$line = explode("|", $row);
	$month = date ("n", $line[0]);
	if ($month == $ChosenMonth) {
		$day = date("j", $line[0]);
		if($day == $actday) {
			echo "<font size='1' color='white'><TABLE style='background-color:#cccc00; width:130px;'><tr><td style='width:130px; height:5px;'>";
			echo "<a href=reservations/".$line[4].">".$line[1]." ".$line[2]."-".$line[3]."</a><br>";
			echo "</td></tr></table></font>";
		}
	}
}
    	
echo "</td>";
}
    echo '</tr>';
?>

 

Now that the "engine" is working I can do all sorts of fun things to make it pretty.

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.