Link to home
Start Free TrialLog in
Avatar of designaire
designaireFlag for United States of America

asked on

click back to previous months in PHP

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

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
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".
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

ASKER CERTIFIED SOLUTION
Avatar of designaire
designaire
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of designaire

ASKER

I got somebody to help me with the problems