Solved

click back to previous months in PHP

Posted on 2014-11-12
7
84 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 110

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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