-
I'm trying to code up some archives for my blog so I can release it soon, but I'm having some problems with it.
Here's what I want to do, but I'm lost on actually doing it: make a list of each distinct month of each distinct year (ie: December 2004, January 2005, etc.) and make a link to a page that will list all the posts of that month.
So far I've gotten each distinct year into an array, though I'm not sure if I should use an array for this (or if I really have any other choice). But where do I go from there? I'm not experienced with grabbing information from a database depending on an array item.
-
If you want to do it the simple way, you'd pass variables on through the URL.
You'd need columns in your database called 'year' and 'month' for each item.
You'd have the links like so,
Code:
<a href="somepage.php?year=2004&month=12">December 2004</a>
<a href="somepage.php?year=2005&month=1">January 2005</a>
Then, on somepage.php you'd have a code getting the information into variables:
PHP Code:
$year = $_GET['year'];
$month = $_GET['month'];
// For some reason though, you can use $year as though it were a regular variable,
// so I don't know why i get it through the $_GET variables, but it seems safer.
Then you'd tell it to get all of the entries with the corresponding year and month:
PHP Code:
$result = mysql_query("SELECT your, variables FROM blog WHERE year = '$year' AND month = '$month'");
Then you'd get how many entries that got, (i.e. how many posts are in $month in $year), and make a loop around and around and print them all out.
PHP Code:
$rows = mysql_affected_rows();
$num = 0;
while($rows > $num) {
// All the stuff for each item. $row is an array containing all the columns,
// e.g $row[0] might be 'id', and $row[1] might be 'title', depending on
// the layout of your database.
$num--;
}
This is the simplest method I can think of. But it poses the ugly URLs of http://www.yoursite.com/somepage.php?year=2005&month=12... eugh.
There is a method using .htaccess to have urls like /blog/2005/12 not /blog/?year=2005&month=12 but I can't help you there because I am currently doing this and am waiting for help from this forum!
Hope I helped. :p
-
Thanks for that! :)
I'll have to restructure my database a bit then perhaps.
I was hoping I could somehow gather the month and year of each post via a single timestamp in each row. Do have any idea if I could still do that?
(I should have thought this through more clearly before I started building the database...)
-
Yeah, you can get the timestamp into a $variable and then do:
PHP Code:
$date = date("F Y",$variable);
This is the date function, of which I'm sure you're acknowledged. But you can also use it not only to get the current date, but to get the date from a timestamp.
The 'F' is the month as a full word, e.g June. The Y is a full four-digit year, e.g. 2005.
---
Also, used in conjunction with mktime(), you don't even need a timestamp, just the month and year (numerical) values:
PHP Code:
$date = date("F Y",mktime(0,0,0,$month,0,$year));
Note: mktime() is a function that creates a timestamp as a string from the variables it's given. They are as follows:
PHP Code:
mktime(hours, minutes, seconds, months, days, years)
(I think, correct me on the first three if I'm wrong anybody).
You don't need all of those values, and if you don't have them just put a '0', but you need the ones you are extracting in this case, obviously. (I.e. Month and Year).
-
Thanks for the help. I think I have a handle on how to make my archives now. I'll do so tonight, then I think my site should be ready to upload.
-
Cool :)
PM me when it's up ;)