Solved

click back to previous months in PHP

Posted on 2014-11-12
7
81 Views
Last Modified: 2014-11-23
I'm trying to get this data to display from month to month in a database. Right now the data is called $month and there is also a date stored in the database by month and also complete date. This is the code that somebody was helping me with. I am very new to PHP. Right now it goes forwards and backwards but doesn't display the current month.

if (!isset($_REQUEST['next']) || !isset($_REQUEST['prev'])) {
	 	$month= ltrim(date('m'),0) ;
	 }
	 $direction = '';
	 if (isset($_REQUEST['next'])){ 
	    $direction = 'next';
	 }
	if (isset($_REQUEST['prev'])){ 
		$direction = 'prev';
	}
	if ($direction == 'next'){
	 	$month = $month + 1;
		print_r($month);
	 } else {
	 	$month = $month - 1;
		print_r($month);
	 }
 	 date_default_timezone_set('America/New_York');




<form method="post" action="/reports.php">
<input type="submit"/>
<input type="hidden" name="prev" val="next"/>
</form>


<form method="post" action="/reports.php">

<input type="submit"/>
<input type="hidden" name="next" val="next"/>
</form>

Open in new window

0
Comment
Question by:designaire
  • 2
  • 2
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40438992
This article describes how to handle date/time values in PHP and MySQL.  Please read it over and post back if you still have questions about the concepts.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 40448082
I don't see how you are keeping track of the current month with the code you supplied.  Unless it is handled in some code that you're not showing here your $month will always be the current month.

In line 1 of your code you are checking to see if Next OR Previous are Not selected.  This will always be True, so I think you want to check if Next AND (&&) Previous are Not selected.
if (!isset($_REQUEST['next']) && !isset($_REQUEST['prev'])) {

Open in new window

Also your "prev" hidden field has a value of "next" which I'm guessing should be a value of "prev".
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40448173
See if this helps.  I may be misunderstanding the question, but as I interpret it the question would be "How do I get the values for the previous or next month?"  If that's not what you want, please give us a little more to go on.
http://iconoun.com/demo/temp_designaire.php

<?php // demo/temp_designaire.php
error_reporting(E_ALL);

// SET THE LOCAL TIMEZONE
date_default_timezone_set('America/New_York');

// ACQUIRE AND NORMALIZE THE REQUEST VARIABLE FROM THE URL
$get = !empty($_GET['q']) ? trim(strtolower(substr($_GET['q'],0,1))) : FALSE;
if ($get)
{
    // TO FIND NEXT MONTH
    if ($get == 'n')
    {
        $next = mktime( 0,0,0, date('m') + 1, date('d'), date('Y') );
        echo PHP_EOL . "NEXT MONTH: " . date('F, Y', $next);
    }
    // TO FIND PREVIOUS MONTH
    elseif ($get == 'p')
    {
        $prev = mktime( 0,0,0, date('m') - 1, date('d'), date('Y') );
        echo PHP_EOL . "PREV MONTH: " . date('F, Y', $prev);
    }
    // IF THERE IS AN UNKNOWN REQUEST
    else
    {
        echo PHP_EOL . "UNRECOGNIZED INPUT: " . $_GET['q'];
    }
}

// CREATE THE REQUEST FORM USING HEREDOC NOTATION
$form = <<<EOD
<form>
What month?
<br>
<input name="q" type="radio" value="Next" /> Next
<br>
<input name="q" type="radio" value="Prev" /> Previous
<br>
<input type="submit" value="Go!" />
</form>
EOD;

echo $form;

Open in new window

0
 

Accepted Solution

by:
designaire earned 0 total points
ID: 40450369
Thanks, I got somebody to help me with it...It doesn't go through the years but it will work for what I need it for...

I'll try to look at your code and use it on a later date


$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));

if ($month=="")
{  
	date_default_timezone_set('America/New_York');
	$dateTime = date(DATE_RSS);      
	echo $dateTime . "<br>" . "<br>";
	echo "date('m') =" . date('m') . "<br>" . "<br>";  	 
	$month = ltrim(date('m'),0) ;  	
	echo "ltrim(date('m'),0) =" . $month . "<br>" . "<br>";  // I think that all 
}

$prevmonthtopost = ((($month+11)-1) % 12) + 1;  
$nextmonthtopost = ((($month+11)+1) % 12) + 1;  
?>

<form method="post" action="/next3.php?month=<?php echo $prevmonthtopost ?>">
  <input type="submit" value="previous" name="prev"/>
</form>
<p></p>
<form method="post" action="/next3.php?month=<?php echo $nextmonthtopost ?>">
  <input type="submit" value="nextious month" name="next"/>
</form>

<h1>Month <?php echo $month ?></h1>

<!-- Or you could do it this way -->
    <h1>Days in <?php echo $month ?> you meditated.</h1></div>

Open in new window

0
 

Author Closing Comment

by:designaire
ID: 40460408
I got somebody to help me with the problems
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will learn how to dynamically set the form action using jQuery.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now