Page 1 of 1

PHP & MySQL BLog

Posted: Sun Feb 22, 2009 10:09 am
by indiodoido
hi,

I'm thinking on developing a simple blog in PHP & MySQL. The only thing i want in the blog is the possibility to enter posts and archive them by date (month and year).
I have everything planed out, but i really don't know how to organize my posts in the blog by date :(

For example:
Archives: Dec 08 - Jan 09 - Feb 09

Can anyone tell me how to do this, or give me an example code?

Re: PHP & MySQL BLog

Posted: Sun Feb 22, 2009 10:20 am
by Citizen
I assume you have a timestamp in your database entry for each.

Just one example, you could use date("mY",$row['time']) as an array key

Re: PHP & MySQL BLog

Posted: Sun Feb 22, 2009 10:25 am
by indiodoido
hi Citizen

Yes, i have a date and time field in my post table.
I'm really new to PHP, how do i use date("mY",$row['time']) as an array key?

Re: PHP & MySQL BLog

Posted: Sun Feb 22, 2009 11:33 am
by watson516
I just made this sort of thing just yesterday.

I am not sure if this is the proper way of doing things but it works for me.
It displays the month and year along with a post count for that month like this:

October 2008 (14)
January 2009 (2)
Februrary 2009 (6)

Code: Select all

 
<?php
$months=array("January","February","March","April","May","June","July","August","September","October","November","December");
$q="SELECT threads_date FROM forum_threads WHERE threads_spcl = 2 ORDER BY threads_date DESC";
$result=$db->query($q);
while($row=$db->fetchArray($result))
{
    $year=substr($row['threads_date'],2,2);
    $month=substr($row['threads_date'],5,2);
    $month--;
    $count[$year][$month]++;
}
?>
<div class="block">
    <h2>Blog Archive</h2>
    <ul class="links">
        <?php
        foreach($count as $year => $month)
        {
            foreach($month as $key => $val)
            {
                if($key<10) $urlMonth="0".($key+1); else $urlMonth=$key+1;
                $urlDate=$year.$urlMonth;
                ?>
                <li><a href="index.php?p=blog&month=<?php echo $urlDate; ?>"><?php echo $months[$key]." 20".$year." (".$val.")"; ?></a></li>
                <?php
            }
        }
        ?>
    </ul>
</div>
 

Re: PHP & MySQL BLog

Posted: Sun Feb 22, 2009 12:18 pm
by indiodoido
hey watson516!

that's it! that's what i'm looking for...
thanks a lot :D