1

I am currently working on a project and it is basically a scheduling web app. I have one page where users enter some text that will be save for the current day.

Users are also allowed to make entries for the next and previous day, or using a calendar.

At the bottom of my page I have two buttons: previous and next day.

I would like to be able to click, for example, in the next day and display the date of the next day at the top of the page. And once I am in the page of the next day, if I click next day again I should go to the next next day, i.e., two days a head. And so on. The same would be applied to the previous day.

So, I need a way to continuously go forward and backward in the calendar using the buttons.

So far, I can go to the imediately next and previous day, but I cannot continue.

These are the links at the bottom of the page:

<a href="builder.php?day=previous" id="btn_previous_day">Previous day</a>                              
<a href="builder.php?day=next" id="btn_next_day">Next day</a>

This is how I find which link was clicked. This code is at the top of the page

<?php 
$_SESSION['prev'] = 0;
$_SESSION['next'] = 0;

if(isset($_GET['day']))
{
    if( $_GET['day'] == 'previous' )
    {
        $_SESSION['prev']--;

        $currentDay = date('j m o', strtotime(' - ' . $_SESSION['prev'] . ' day'));
    }

    if($_GET['day'] == 'next')
    {
        $_SESSION['next']++;
        $currentDay = date('j m o', strtotime(' + ' . $_SESSION['next'] . ' day'));
    }

}   else {
    $currentDay =  date('j m o');

}
?>

And here is where a output the current day:

<p><input type="text" id="title" style="border: none;" name="title" size="20" value=" WOD <?php echo $currentDay; ?>"/></p>

Thank you!!!!

1
  • You might want to do this link
    – eMad
    Nov 29, 2014 at 6:54

5 Answers 5

1

try this:

@session_start();

if(isset($_GET['day']))
{
    if( $_GET['day'] == 'previous' )
    {

        $_SESSION['currentDay']=$currentDay = date('j m o', (strtotime($_SESSION['currentDay']) - (3600*24)));
    }

    if($_GET['day'] == 'next')
    {
        $_SESSION['currentDay']= $currentDay = date('j m o', (strtotime($_SESSION['currentDay']) + (3600*24)));
    }

}   else {
    $_SESSION['currentDay']=$currentDay =  date('j m o');
}
1

It is because you are setting session variables to zero each time page is loaded.

Try replacing following lines :

$_SESSION['prev'] = 0;
$_SESSION['next'] = 0;

with

if(!$_SESSION['prev']){
$_SESSION['prev'] = 0;
}

if(!$_SESSION['next']){
$_SESSION['next'] = 0;
}

It should allow you to go to further next and previous days.

1

You appear to be setting prev and next to 0 at the top of your code. So even though you increment/decrement the next/prev values, they just get reset to zero. So you can't get any further than +/- one day from today.

Obviously not all of your code is here, but you only need to set the session variables once per session.

Also, you don't really need a next AND prev -- you can do this with just a single variable. So you could do something like this at the top:

if (!isset($_SESSION['dayIndex']))
    $_SESSION['dayIndex'] = 0;

Then:

if( $_GET['day'] == 'previous' )
{
    $_SESSION['dayIndex']--;
    $currentDay = date('j m o', strtotime(' - ' . $_SESSION['dayIndex'] . ' day'));
}

else if($_GET['day'] == 'next')
{
    $_SESSION['dayIndex']++;
    $currentDay = date('j m o', strtotime(' + ' . $_SESSION['dayIndex'] . ' day'));
}

You could also do this by passing a specific date via the URL parameter, or a number of other ways as well.

1

Just wanted to post the solution that I found to my question, based on all the suggestions made here. Thank you all.

My solution to the question:

if(!$_SESSION['dayIndex']){
$_SESSION['dayIndex'] = 0; 
}
    if(isset($_GET['day'])){
        if( $_GET['day'] == 'previous' ){
            --$_SESSION['dayIndex'];
            $currentDay = date('j m o', strtotime( $_SESSION['dayIndex'] . 'day'));        
        }
        if($_GET['day'] == 'next'){
            ++$_SESSION['dayIndex'];    
            $currentDay = date('j m o', strtotime($_SESSION['dayIndex'] . ' day'));
        }
} else {
    $currentDay =  date('j m o');
}
0

You can easily do this by adding another parameter in the Url for example

 $_GET['n']

the same goes for $_SESSION

and keep increment this parameter in every page

// this will get the prev three days (for example)
<a href="builder.php?day=previous&n=3" id="btn_previous_day">Previous day</a> 
// this will get the next three days  (for example)                            
<a href="builder.php?day=next&n=3" id="btn_next_day">Next day</a>

when you click the next button increment this and it will show the next 4 day and so on

this is how you do it

if(isset($_GET['day']))
{
  if( $_GET['day'] == 'previous' )
  {

     $currentDay = date('j m o', strtotime(' - ' . $_GET['n'] . ' day'));
 }

if($_GET['day'] == 'next')
{
    $currentDay = date('j m o', strtotime(' + ' . $_GET['n'] . ' day'));
}

}


echo $currentDay;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.