Ashan Ashan - 6 months ago 8
MySQL Question

How to display weeks in a table with previous and next buttons

I got the below code to get the current week dates and display on the html table:

<table>
<tr>
<td><a href="#"> << </a> </td>
<td></td>
<td><a href="#"> >> </a> </td>
</tr>
<tr>
<?php
$ts = date(strtotime('last sunday'));
$dow = date('w' , $ts);
$offset = $dow - 1 ;

if($offset < 0 ){
$offset = 0 ;
}

$ts = $ts - $offset * 86400;
for ($x=0 ; $x<7 ; $x++,$ts += 86400) {
echo '<td>' . date("m-d-Y", $ts) . '</td>' ;
}
?>
</tr>
</table>


This is working fine starting with "Sunday" and hows the current week dates.
But i am using "Next" and "Previous" buttons/links to dis play the next week and the last week.

But i am unable to display the previous week details and next week dates. Also once the previous or next week dates are retrieved it should keep on continue without any error for X number of weeks.

How can i do this with PHP/mysql.
Can anyone help me on this.

Thanks,
Ashan.

Answer

You can try this one. Some sort of pagination:

<?php

    (int)$currentpage = (!empty($_GET["currentpage"]))?$_GET["currentpage"]:0;
    (int)$nextpage = $currentpage + 1;
    (int)$prevpage = $currentpage - 1;

?>

<table>
    <tr>
        <td><a href="<?php echo "{$_SERVER['PHP_SELF']}?currentpage=$prevpage"; ?>"> << </a> </td>
        <td></td>
        <td><a href="<?php echo "{$_SERVER['PHP_SELF']}?currentpage=$nextpage"; ?>"> >> </a> </td>
    </tr>
    <tr>
        <?php
            $ts = date(strtotime('last sunday'));
            $ts += $currentpage * 86400 * 7;
            $dow = date('w' , $ts);
            $offset = $dow;

            $ts = $ts - $offset * 86400;
            for ($x=0 ; $x<7 ; $x++,$ts += 86400) {
                echo '<td>' . date("m-d-Y", $ts) . '</td>' ;
            }
        ?>
    </tr>
</table>