Solved

click back to previous months in PHP

Posted on 2014-11-12
7
82 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 109

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 109

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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.
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…
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.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 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

10 Experts available now in Live!

Get 1:1 Help Now