/    Sign up×
Community /Pin to ProfileBookmark

Archiving a Blog?

Hello everyone,
As most of you know I’m currently developing a blog for myself. I have got allot done but now I have reached something that I’m not sure how to accomplish. I need to create an archive page for my blog entries that is sorted by year and month of the year but I’m not sure how to do that. Right now I just have entries in a table in a MySQL database. So as you can tell I have the table right but I’m not sure exactly how I would sort it on the archive page like this:

[b]2004[/b]
January
– Entry 1
– Entry 2
– Entry 3

October
– Entry 1
– Entry 2
– Entry 3

[b]2000[/b]
August
– Entry 1
– Entry 2
– Entry 3

I would greatly appreciate it if someone could help me get this working. Sorry if I’m an idiot, I can’t help it. :p

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Bootsman123Oct 19.2004 — I've created something like this for my own blog, but since my blog is in Dutch there will be some dutch things in it. And if you can't figure out how to do the css-styling say so then i'll post that too. This isn't the whole script I use for the archive, but the front (that what is below here) can be viewed here:

http://www.dukkie.nl/archives

[code=php]
/* Declare variables */
global $date_arr;

$month_arr = array (
"01" => "januari",
"02" => "februari",
"03" => "maart",
"04" => "april",
"05" => "mei",
"06" => "juni",
"07" => "juli",
"08" => "augustus",
"09" => "september",
"10" => "oktober",
"11" => "november",
"12" => "december"
);

/* Get data from db. */
$sql_query = "SELECT date, name, title FROM something ORDER BY date DESC";
$sql_result = mysql_query ($sql_query);

if ($sql_result)
{
echo "Archive";

while ($sql_rows = mysql_fetch_array ($sql_result, MYSQL_ASSOC))
{
$date_arr[date ("Y", $sql_rows['date'])][date ("m", $sql_rows['date'])][date ("j", $sql_rows['date'])][$sql_rows['name']] = $sql_rows['title'];
}

foreach ($date_arr as $year => $year_val)
{
foreach ($date_arr[$year] as $month => $month_val)
{
echo "<br />";
echo "<a href="/archives/$year/$month" title="Bekijk berichten voor " . $month_arr[$month] . " $year" rel="bookmark" class="standard_url_large">" . ucfirst ($month_arr[$month]) . "</a> <a href="/archives/$year" title="Bekijk berichten voor $year" rel="bookmark" class="standard_url_large">$year</a>";
echo "<ul class="archive_date_list">";

foreach ($date_arr[$year][$month] as $day => $day_val)
{
if ($day == 1 || $day == 8 || $day > 19)
{
echo "<li><a href="/archives/$year/$month/$day" title="Bekijk berichten voor $day " . $month_arr[$month] . " $year" rel="bookmark">$day" . "e</a>";
}
else
{
echo "<li><a href="/archives/$year/$month/$day" title="Bekijk berichten voor $day " . $month_arr[$month] . " $year" rel="bookmark">$day" . "de</a>";
}

echo "<ul class="archive_blog_list">";

foreach ($date_arr[$year][$month][$day] as $blog_name => $title)
{
echo "
<li>
<a href="/archives/$year/$month/$day/$blog_name" title="Bekijk het bericht $title" rel="bookmark">$title</a>
<span><a href="/comments/$blog_name" title="Bekijk berichten bij deze blog" rel="bookmark">(" . $msg_arr[$blog_name] . ")</a></span>
<!--<ul class="archive_msg_list">
<li>
<a href="/comments/$blog_name" title="Bekijk berichten bij deze blog" rel="bookmark">(" . $msg_arr[$blog_name] . ")</a>
</li>
</ul>-->
</li>
";
}

echo "</ul></li>";
}

echo "</ul>";
}
}
}
[/code]
Copy linkTweet thisAlerts:
@JickauthorOct 19.2004 — That's a little confusing... I've gotten closer to what I need with way less code. Does anybody else have any ideas? ?
Copy linkTweet thisAlerts:
@pyroOct 19.2004 — Why don't you post what you've got so far, and we'll go from there.
Copy linkTweet thisAlerts:
@yunaOct 19.2004 — Is the problem one of sorting textual dates? The solution is not to store the dates in a text format. Try storing 32-bit timestamps (the values returned by the time() function) which obviously sort correctly. Then use the date() function to format the timestamps as needed.
Copy linkTweet thisAlerts:
@JickauthorOct 20.2004 — Ok, well before I post it I should warn you that this may be totally off of what it should be. Like I said, this is just code that came close to what I need.[code=php]$results = mysql_query("SELECT * FROM entries ORDER BY year DESC");
while($r = mysql_fetch_array($results))
{
$month = str_replace(array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"), $r["month"]);
echo("<h1>" . $month . " " . $r["year"] . "</h1>n");

$results2 = mysql_query("SELECT * FROM entries WHERE month = "" . $r["month"] . "" AND year = "" . $r["year"] . """);
while($r2 = mysql_fetch_array($results2))
{
echo("<a title="" . $r2["title"] . "" href="/entry.php?id=" . $r2["id"] . "">" . $r2["title"] . "</a><br />n");
}
}[/code]
That works fine if there is only one entry in the month but if there are multiple it repeats the month and year and entries for as many times as there are entries with the same date. This is the output I get for this code:[b]JanuaryFebruary 2004[/b]
Welcome
Testing

[b]JanuaryFebruary 2004[/b]
Welcome
Testing

[b]January 2000[/b]
Another

[b]September 1999[/b]
First Entry!
I hope you guys can understand that and help. ?
×

Success!

Help @Jick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...