Solved

Week Calendar PREV and NEXT

Posted on 2013-12-10
6
1,061 Views
Last Modified: 2013-12-30
OK, I give up!  I'm not sure what I am doing wrong!

I have the following script that determines the date and shows a 1 week Calendar.
The Next Week Button and the PREV week button work 100%.  However, depending on the day, there is a query that pulls information from a DB and displays it on the correct date. This also works 100%.  However, when I click NEXT or PREVIOUS the Calendar itself does not change date. (At line 38)

function weekCalendar($date='Today')	{  //  METHOD
    $pdo = new PDO("");
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	
	
	
    $timestamp      = strtotime(date('Y-m-01', strtotime($date)));
    $caption     	= date("F Y", $timestamp);

	
	$date = !empty($_GET['d']) ? $_GET['d'] : 'Today';
	
	$ts = !empty($_GET['d']) ? strtotime($_GET['d']) : strtotime('Today');
	
	if (!$ts) $ts = strtotime('Today');
	$prev = date('Y-m-d', $ts - 60*60*24*7);
	$next = date('Y-m-d', $ts + 60*60*24*7);

	$prev_link = '<a href="' . $_SERVER['PHP_SELF'] . "?d=$prev" . '">Prev</a>' . PHP_EOL;
	$next_link = '<a href="' . $_SERVER['PHP_SELF'] . "?d=$next" . '">Next</a>' . PHP_EOL;
	
	$date_adv = date('Y-m', $ts);

?>

<table id="cal_nav">
	<tr>
		<td class="cal-nav-left"><h2><?php echo $prev_link; ?></h2></td>
		<td class="cal-nav-center"><h2><?php echo date('F Y', strtotime($date)); ?></h2></td>
		<td class="cal-nav-right"><h2><?php echo $next_link; ?></h2></td>
	</tr>
</table>

<?php	
    echo PHP_EOL;
	echo "<table class='full'><tr>";
	
	for($day=1; $day<=7; $day++) {
		echo '<td class="align-top"><strong>' .date('D M d', strtotime($date_adv.'-'.$day)).'</strong><br />';
		
		$this_date = date('Y-m-d', strtotime($date_adv.'-'.$day));
		echo $this_date;
		
		$sql = "SELECT schedule_id, first_name, last_name, asset_name, asset_type, start_date, workorder_id, color
				FROM schedule
				WHERE start_date = '$this_date'
				GROUP BY schedule_id DESC";

		$pdos = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
		
		try{
			$pdos->execute();
			
			if ($pdos) {
				while ($row = $pdos->fetch(PDO::FETCH_OBJ)) {
					$at = $row->asset_type;
					$fn = $row->first_name;
					$ln = $row->last_name;
					$sd = $row->start_date;
					$id = $row->schedule_id;
					$ac = $row->color;
 
					
					if ($sd == $this_date && $at == 'driver') {
						
						echo '<a href="view_workorder_print.php?id='.$id.'">'.$fn .''. $ln .'</a><br />';
						} 
					
				}  //  END WHILE
				
			}  //  END IF
			
			
		}  //  END TRY			          	
		catch(PDOException $e) {
   			echo 'ERROR: ' . $e->getMessage();
		}
		echo "<td>";	
	}  //  END FOR
	echo "</tr></table>";
		
	}
//  END WEEK CAKENDAR
}

Open in new window

0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39710262
Is this on a public-facing server where we can see the rendered HTML document?  If no, can you please put it in a test environment so we can see it in action?  Thanks, ~Ray
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39710281
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39710297
I think you want to follow the $this_date variable in the logic.  Not sure about that but it's getting late in the day here.  If you look at the PHP code and the generated HTML document closely, you may find that the HTML is not what you expected from running the PHP.  In particular, the HTML document starts with this:
</tr></table>


		
	}
//  END WEEK CAKENDAR
}
?><!DOCTYPE html>

Open in new window

0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39711523
See if this is helpful.  It should work correctly even across leap year and daylight saving time.  Please post back if you still have questions, thanks.

<?php // RAY_temp_rgranlund.php
error_reporting(E_ALL);


// DEMONSTRATE PREVIOUS AND NEXT WEEKS
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28315321.html


// SET THE TIMEZONE
date_default_timezone_set('America/Chicago');

// IF THERE IS A URL ARGUMENT, USE IT PREFERENTIALLY, OR USE TODAY
$ts = !empty($_GET['d']) ? strtotime($_GET['d']) : FALSE;
if (!$ts) $ts = strtotime('TODAY');

// NORMALIZE TO THE ISO-8601 DATE WITHOUT TIME
$ymd = date('Y-m-d', $ts);
$day = date('D', $ts);

// THE WEEKLY CALENDAR WILL START ON SUNDAY
if ($day == 'Sun')
{
    $alpha = $ymd;
}
else
{
    $alpha = date('Y-m-d', strtotime($ymd . ' LAST SUNDAY'));
}

// THE WEEKLY CALENDAR WILL END ON SATURDAY
$omega = date('Y-m-d', strtotime($alpha . ' SATURDAY'));

// THE PREV AND NEXT WEEKS WILL BE COMPUTED FROM THE CURRENT DAY
$prev  = date('Y-m-d', strtotime("$ymd - 1 WEEK"));
$next  = date('Y-m-d', strtotime("$ymd + 1 WEEK"));

// MAKE THE URLS
$prev_uri = $_SERVER['PHP_SELF'] . "?d=$prev";
$next_uri = $_SERVER['PHP_SELF'] . "?d=$next";

// MAKE THE LINK TEXTS
$prev_lnk = '<a href="' . $prev_uri . '">Prev</a>';
$next_lnk = '<a href="' . $next_uri . '">Next</a>';

// MAKE THE REPRESENTATION OF THE WEEK
$week['prev'] = $prev_lnk;
while ($alpha <= $omega)
{
    // A TEXT VERSION OF THE WEEKDAY
    $day = date('D', strtotime($alpha));

    // HIGHLIGHT THE CURRENT DAY
    if ($alpha == $ymd)
    {
        $week[$day] = "<b>$alpha</b>";
    }
    else $week[$day] = $alpha;
    $alpha = date('Y-m-d', strtotime("$alpha + 1 DAY"));
}
$week['next'] = $next_lnk;

// SHOW THE REPRESENTATION OF THE WEEK
echo '<pre>';
print_r($week);

Open in new window

HTH, ~Ray
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39713029
@Ray.  I was able to edit it into what I needed.  So strange.  I looked all over the internet for an example of how to do this, to no real avail.  Thanks again!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39713096
Glad to be able to help!  Thanks, ~Ray
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question