Help needed making archive button

Hi guys, I searched the forum but couldn’t find anything hoping you might be able to help. I’m trying to create an archive button for my site, www.unthinkable.biz, so that when a visitor clicks the “older” button at the bottom of the page it displays results from the previous day, and that each time the older button is clicked it shows older results. I didn’t code the site and was given it recently but haven’t got a clue when it comes to php (i’m ok with HTML and CSS though). So far I can only get it to show me the previous day’s articles.

This code is at the top of the page -

$page_name = $_REQUEST[“page_name”];
if (!isset($page_name)) { $page_name=“home”;}
$article_id = $_REQUEST[“article_id”];
$today = date(“Y-m-d”);
$thepast = “2009-01-01”;

$yesterday = sql_date_shift($today,"-1 day");
$lastweek = sql_date_shift($today,"-7 day");
$one_day_ago = date(‘Y-m-d’,strtotime(’-1 day’));

And this is the code I have for the button

<a href="/home/<? echo dateformat($one_day_ago,"Y-m-d","d-m-Y");?>/" id=“nav3”>

if I understand what you need correctly and the structure is the user is on www.unthinkable.biz/home/2011-03-05 for the 5th or march 2011 and you want a button back to www.unthinkable.biz/home/2011-03-04 and then you want a button on that page back to www.unthinkable.biz/home/2011-03-03 and so on, then the following should work for you.

[php]

<?php //gets the current URL path e.g. /home/2011-03-05 $url = $_SERVER['PHP_SELF']; //strips the /home/ leaving 2011-03-05 $url2 = str_replace("/demo/", "", $url); //makes sure only the date is selected, for example if the $url2 variable is 2011-03-05/index.php then only the 2011-03-05 will be stored in the variable $date = substr($url2,0,10); //takes one day off the date of the archive the user is currently reading. so if they are reading the archive for 2010-01-02 then the date returned will be 2010-01-01 $newdate = strtotime ( '-1 day' , strtotime ( $date ) ) ; $newdate = date ( 'Y-m-d' , $newdate ); // using the example above the href will now be www.unthinkable.biz/home/2011-03-04 echo " "; ?>

[/php]

replace:
[php]
//strips the /home/ leaving 2011-03-05
$url2 = str_replace("/demo/", “”, $url);
[/php]

with:
[php]
//strips the /home/ leaving 2011-03-05
$url2 = str_replace("/home/", “”, $url);
[/php]

I used /demo/ to test the code

Hi maas, thanks for your quick response. That’s exactly what I want it to do, I’ve placed the code on my site but it doesn’t seem to work (it just returns an unusual date), any suggestions? (I take it I should place the whole piece of code where I want the button to go)

Can you tell me the unusual date that is displayed. Also can you paste the URL of the page the error occurs on, and double check the code you used is:

[php]

<?php //gets the current URL path e.g. /home/2011-03-05 $url = $_SERVER['PHP_SELF']; //strips the /home/ leaving 2011-03-05 $url2 = str_replace("/home/", "", $url); //makes sure only the date is selected, for example if the $url2 variable is 2011-03-05/index.php then only the 2011-03-05 will be stored in the variable $date = substr($url2,0,10); //takes one day off the date of the archive the user is currently reading. so if they are reading the archive for 2010-01-02 then the date returned will be 2010-01-01 $newdate = strtotime ( '-1 day' , strtotime ( $date ) ) ; $newdate = date ( 'Y-m-d' , $newdate ); // using the example above the href will now be www.unthinkable.biz/home/2011-03-04 echo " "; ?>

[/php]

When I click the button it returns the date 1969-12-31, the code I used is exactly what you posted. The url of the page is - http://www.unthinkable.biz/home/1969-12-31.

I went on your site to see what the issue might be, its simply that your dates are dd-mm-yyyy and the code I gave you were for yyyy-mm-dd. Therefore the following should work:

[php]

<?php //gets the current URL path e.g. /home/05-03-2011 $url = $_SERVER['PHP_SELF']; //strips the /home/ leaving 05-03-2011 $url2 = str_replace("/home/", "", $url); //makes sure only the date is selected, for example if the $url2 variable is 2011-03-05/index.php then only the 05-03-2011 will be stored in the variable $date = substr($url2,0,10); //takes one day off the date of the archive the user is currently reading. so if they are reading the archive for 02-01-2010 then the date returned will be 02101-2010 $newdate = strtotime ( '-1 day' , strtotime ( $date ) ) ; $newdate = date ( 'd-m-Y' , $newdate ); // using the example above the href will now be www.unthinkable.biz/home/04-03-2011 echo " "; ?>

[/php]

just an extra bit for you. If you would like a ‘Newer’ button on older pages then you can add this script

[php]
//gets todays date in the format you need
$todayNewer = date(‘d-m-Y’);

//if statement checks to see if the archive page you are on is for today, if it is for today then no ‘Newer’ button is shown, if the page is not todays then the ‘Newer’ button will be shown
if($todayNewer != $date) {
$newdateNewer = strtotime ( ‘+1 day’ , strtotime ( $date ) ) ;
$newdateNewer = date ( ‘d-m-Y’ , $newdateNewer );

//obviously you will need to make a ‘newer_btn.png’ image
echo “”;
}
[/php]

Sorry, still no luck now I get a page with the date 31-12-1969. Seems like its working but its not taking a day off of the right date.

ok, the issue was that the URL was http://www.unthinkable.biz/home/ for the first page, and there was no date in the URL for the code to use. Therefore this SHOULD work, assuming your page is called index.php…

[php]

<?php //gets the current URL path e.g. /home/05-03-2011 $url = $_SERVER['PHP_SELF']; //strips the /home/ leaving 05-03-2011 $url2 = str_replace("/home/", "", $url); //makes sure only the date is selected, for example if the $url2 variable is 2011-03-05/index.php then only the 05-03-2011 will be stored in the variable $date = substr($url2,0,10); //todays date in format you need $todayNewer = date('d-m-Y'); //checks to see if you are on an archive page or home page if($url == "/home/index.php") { $date = $todayNewer; } //takes one day off the date of the archive the user is currently reading. so if they are reading the archive for 02-01-2010 then the date returned will be 02101-2010 $newdate = strtotime ( '-1 day' , strtotime ( $date ) ) ; $newdate = date ( 'd-m-Y' , $newdate ); //if statement checks to see if the archive page you are on is for today, if it is for today then no 'Newer' button is shown, if the page is not todays then the 'Newer' button will be shown if($todayNewer != $date) { $newdateNewer = strtotime ( '+1 day' , strtotime ( $date ) ) ; $newdateNewer = date ( 'd-m-Y' , $newdateNewer ); //obviously you will need to make a 'newer_btn.png' image echo ""; } // using the example above the href will now be www.unthinkable.biz/home/04-03-2011 echo " "; ?>

[/php]

sorry for all the posts, but I swear this one will work.

[php]

<?php //gets the current URL path e.g. /home/05-03-2011 $url = $_SERVER['PHP_SELF']; //strips the /home/ leaving 05-03-2011 $url2 = str_replace("/home/", "", $url); //makes sure only the date is selected, for example if the $url2 variable is 2011-03-05/index.php then only the 05-03-2011 will be stored in the variable $date = substr($url2,0,10); //todays date in format you need $todayNewer = date('d-m-Y'); //takes one day off the date of the archive the user is currently reading. so if they are reading the archive for 02-01-2010 then the date returned will be 02101-2010 $newdate = strtotime ( '-1 day' , strtotime ( $date ) ) ; $newdate = date ( 'd-m-Y' , $newdate ); //checks to see if you are on an archive page or home page if($newdate == "31-12-1969") { $newdate = strtotime ( '-1 day' , strtotime ( $todayNewer ) ) ; $newdate = date ( 'd-m-Y' , $newdate ); $date = $todayNewer; } //if statement checks to see if the archive page you are on is for today, if it is for today then no 'Newer' button is shown, if the page is not todays then the 'Newer' button will be shown if($todayNewer != $date) { $newdateNewer = strtotime ( '+1 day' , strtotime ( $date ) ) ; $newdateNewer = date ( 'd-m-Y' , $newdateNewer ); //obviously you will need to make a 'newer_btn.png' image echo ""; } // using the example above the href will now be www.unthinkable.biz/home/04-03-2011 echo " "; ?>

[/php]

Thanks so much for your time, but there’s still a problem. It’s showing the correct date but no articles are displayed. Also by clicking the older button again it doesn’t change the date.

sorry mate im stumped. When testing it, it works for me, so I dont know what to suggest. If anyone else on here can have a crack at it fell free.

Sponsor our Newsletter | Privacy Policy | Terms of Service