Monthly Archives using PHP/MySQL?

This is a discussion on "Monthly Archives using PHP/MySQL?" within the PHP Forum section. This forum, and the thread "Monthly Archives using PHP/MySQL? are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 12th, 2008, 18:36
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Monthly Archives using PHP/MySQL?

In a similar way to WP, I would like to display archives by monthly. Each post has a date in the form yyyy-mm-dd. I figured for each month it would be something like this for Jan:
PHP: Select all

$display_posts = @mysql_query("SELECT contentid, contenttitle, contentintro, content, author, category, date FROM content WHERE date < 2008-02-01 AND date >= 2008-01-01"); 

But is there away of, instead of manually typing that for each month, for it do create each month automatically when it enters the month? So, it would only display the link for January, but on 2008-02-01, display February as well.

Is it possible? It must be, because I've seen it done on WordPress

Thanks,

Jack
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote

  #2 (permalink)  
Old Jan 12th, 2008, 18:55
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: Monthly Archives using PHP/MySQL?

Something like this: (untested...)
PHP: Select all

$month=mysql_escape_string($_GET['month']); // or wherever else the month is comin frum
$month=((($month>=12)||($month<1))?1:$month); // check value. and wrap it back to 1 if it's at 12
$month_plus_one=$month++;
$month_plus_one=($month_plus_one>=12?1:$month_plus_one); // check and wrap
$display_posts = @mysql_query("SELECT contentid, contenttitle, contentintro, content, author, category, date FROM content WHERE date < 2008-$month_plus_one-01 AND date >= 2008-$month-01"); 
Reply With Quote
  #3 (permalink)  
Old Jan 12th, 2008, 19:06
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Monthly Archives using PHP/MySQL?

Thanks c010!

I'll test it and report any errors I might find.
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
  #4 (permalink)  
Old Jan 12th, 2008, 19:14
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Monthly Archives using PHP/MySQL?

That code would work on the actual page archive.php.

But I need code that will list the months in the sidebar that have posts in. Can that be done?

Also - is the Month in your code Number(1-12) or name?
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
  #5 (permalink)  
Old Jan 12th, 2008, 19:22
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Monthly Archives using PHP/MySQL?

Code did not work I used this code:
PHP: Select all

<?php
$month
=mysql_escape_string($_GET['month']); // or wherever else the month is comin frum
$month=((($month>=12)||($month<1))?1:$month); // check value. and wrap it back to 1 if it's at 12
$month_plus_one=$month++;$month_plus_one=($month_plus_one>=12?1:$month_plus_one); // check and wrap
$display_posts = @mysql_query("SELECT contentid, contenttitle, contentintro, content, author, category, date FROM content WHERE date < 2008-$month_plus_one-01 AND date >= 2008-$month-01"); 
while (
$postsm mysql_fetch_array($display_posts)) {
$contentidm $postsm['contentid'];
$contenttitlem $postsm['contenttitle'];
$contentintrom $postsm['contentintro'];
$contentm $postsm['content'];
$authorm $postsm['author'];
$categorym $postsm['category'];
$datem $postsm['date'];
//display
echo '<div class="post">';
echo 
'<h4 class="catpageposttitle"><a href="single.php?contentid=' $contentidm '">' $contenttitlem '</a></h4>';
echo 
'<h5 class="catpagepostinfo">#' $contentidm ' | by ' $authorm ' On: ' $datem '</h5>';
echo 
'<p class="catpagepostintro">' $contentintrom '</p></div>';
}
?>
And went to the address archive.php?month=1

Just get a blank page. It's as if it is working, but not using month=1.
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
  #6 (permalink)  
Old Jan 12th, 2008, 19:27
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Monthly Archives using PHP/MySQL?

I managed to get them to display on the page using this code:
PHP: Select all

$month $_GET['month'];
$display_posts = @mysql_query("SELECT contentid, contenttitle, contentintro, content, author, category, date FROM content WHERE MONTH(date)='$month'"); 
Surely I can use if else statements? I'm gonna try.

Phew! Ok, I have a way of doing it, but there must be an easier way, because this is almost ridiculous!
PHP: Select all

$currentmonth date("m");
if (
$currentmonth==12) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
echo 
'<li><a href="archive.php?month=5">May</a></li>';
echo 
'<li><a href="archive.php?month=6">June</a></li>';
echo 
'<li><a href="archive.php?month=7">July</a></li>';
echo 
'<li><a href="archive.php?month=8">August</a></li>';
echo 
'<li><a href="archive.php?month=9">September</a></li>';
echo 
'<li><a href="archive.php?month=10">October</a></li>';
echo 
'<li><a href="archive.php?month=11">November</a></li>';
echo 
'<li><a href="archive.php?month=12">December</a></li>';
} else {
if (
$currentmonth==11) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
echo 
'<li><a href="archive.php?month=5">May</a></li>';
echo 
'<li><a href="archive.php?month=6">June</a></li>';
echo 
'<li><a href="archive.php?month=7">July</a></li>';
echo 
'<li><a href="archive.php?month=8">August</a></li>';
echo 
'<li><a href="archive.php?month=9">September</a></li>';
echo 
'<li><a href="archive.php?month=10">October</a></li>';
echo 
'<li><a href="archive.php?month=11">November</a></li>';
} else {
if (
$currentmonth==10) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
echo 
'<li><a href="archive.php?month=5">May</a></li>';
echo 
'<li><a href="archive.php?month=6">June</a></li>';
echo 
'<li><a href="archive.php?month=7">July</a></li>';
echo 
'<li><a href="archive.php?month=8">August</a></li>';
echo 
'<li><a href="archive.php?month=9">September</a></li>';
echo 
'<li><a href="archive.php?month=10">October</a></li>';
} else {
if (
$currentmonth==9) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
echo 
'<li><a href="archive.php?month=5">May</a></li>';
echo 
'<li><a href="archive.php?month=6">June</a></li>';
echo 
'<li><a href="archive.php?month=7">July</a></li>';
echo 
'<li><a href="archive.php?month=8">August</a></li>';
echo 
'<li><a href="archive.php?month=9">September</a></li>';
} else {
if (
$currentmonth==8) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
echo 
'<li><a href="archive.php?month=5">May</a></li>';
echo 
'<li><a href="archive.php?month=6">June</a></li>';
echo 
'<li><a href="archive.php?month=7">July</a></li>';
echo 
'<li><a href="archive.php?month=8">August</a></li>';
} else {
if (
$currentmonth==7) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
echo 
'<li><a href="archive.php?month=5">May</a></li>';
echo 
'<li><a href="archive.php?month=6">June</a></li>';
echo 
'<li><a href="archive.php?month=7">July</a></li>';
} else {
if (
$currentmonth==6) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
echo 
'<li><a href="archive.php?month=5">May</a></li>';
echo 
'<li><a href="archive.php?month=6">June</a></li>';
} else {
if (
$currentmonth==5) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
echo 
'<li><a href="archive.php?month=5">May</a></li>';
} else {
if (
$currentmonth==4) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
echo 
'<li><a href="archive.php?month=4">April</a></li>';
} else {
if (
$currentmonth==3) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
echo 
'<li><a href="archive.php?month=3">March</a></li>';
} else {
if (
$currentmonth==2) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
echo 
'<li><a href="archive.php?month=2">February</a></li>';
} else {
if (
$currentmonth==1) {
echo 
'<li><a href="archive.php?month=1">January</a></li>';
}
}
}
}
}
}
}
}
}
}
}

It basically checks what the month is and, depending on that, displays x amount of links.

But there is so much code! Surely there is an easier way?
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)

Last edited by Jack Franklin; Jan 12th, 2008 at 19:42.
Reply With Quote
  #7 (permalink)  
Old Jan 12th, 2008, 19:58
SuperMember

SuperMember
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Monthly Archives using PHP/MySQL?

try adapting this code.

PHP: Select all

<?php  
$curr_month 
date("m");  
$month = array (1=>"January",  "February""March""April""May""June""July""August",  "September""October",  "November""December");  
$select "<select  name=\"month\">\n"
foreach  (
$month as $key => $val) { 
     
$select .= "\t<option  val=\"".$key."\""
    if  (
$key ==  $curr_month) { 
        
$select .=  " selected>".$val."\n"
    } else { 
        
$select .=  ">".$val."\n"
    } 

$select .=  "</select>"
echo 
$select;  
?>
hope it helps
Reply With Quote
  #8 (permalink)  
Old Jan 12th, 2008, 20:48
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Monthly Archives using PHP/MySQL?

You mean use that code instead of my tremendously long code?
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
  #9 (permalink)  
Old Jan 12th, 2008, 20:54
SuperMember

SuperMember
Join Date: Jun 2007
Location: uk
Posts: 459
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Monthly Archives using PHP/MySQL?

yes

But as i said before im no expert
Reply With Quote
  #10 (permalink)  
Old Jan 12th, 2008, 21:15
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: Monthly Archives using PHP/MySQL?

Hey, I get the drop down, but nothing happens when I click on them. Would I need to use javascript?
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pay monthly hosting bhurruth Hosting & Domains 10 Sep 25th, 2007 17:44
Mysql/php marie2007 PHP Forum 7 Jul 10th, 2007 13:20
Using PHP/MYSQL on Mac OSX jonnymorris Starting Out 0 Jul 5th, 2007 14:55
Palace Archives thepalace1 Free Web Site Critique 0 Apr 4th, 2007 17:20


All times are GMT. The time now is 20:43.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43