1

I have small calendar page where you can click on particular month and get all article from that month. There are some months with no article and I want to avoid part where user click on that month only to discover that it is empty. How can I count all articles for every month in one year and latter present data in view part?

DB for article goes like this:

articles
id_articles -> **number**
title -> **varchar**
text -> **text**
date_created -> timestamp (CURRENT_TIMESTAMP - MM/DD/YYYY HH:MM:SS ) 

HTML code:

<nav id="arcive" class="clearfix">
        <ul>
            <li>
                <a role=slide rel=2012 class="current" >2012</a>
                <ul rel=2012 class="month">
                    <li>
                        <a href="#">December</a>
                        <a href="#">November</a>
                        <a href="#">October</a>
                        <a href="#">September</a>
                        <a href="#">August</a>
                        <a href="#">July</a>
                        <a href="#">Jun</a>
                        <a href="#">May</a>
                        <a href="#">April</a>
                        <a href="#">March</a>
                        <a href="#">February</a>
                        <a href="#">January</a>
                    </li>
                </ul>
            </li>
    </ul>
    </nav>
4
  • Can you post the table structure? =o) Just so we know what types of fields were are dealing with since this will likely require using build in MySQL functions to calculate the month. Also the accompanying HTML form for the calendar and any PHP code that pertains to it. We need more than a list of table columns to be able to do this. Commented Dec 11, 2012 at 22:38
  • @cryptic I added the HTML code. Table structure from above is all that there is for the articles. Commented Dec 11, 2012 at 23:25
  • What are the field types for the table columns? Commented Dec 11, 2012 at 23:31
  • @cryptic added field types. Commented Dec 11, 2012 at 23:37

1 Answer 1

2

The PHP to dynamically create the menu would be this:

$year = '2012'; // set this to whatever the year to be queried is

$sql = 'SELECT GROUP_CONCAT(MONTH(`date_created`)) `date_created`  '
     . 'FROM `articles` '
     . 'WHERE YEAR(`date_created`) = ' . (int) $year; // typecast for security

// run query to return list of numeric months with articles
$query = $this->db->query($sql);  
$result = $query->row();
$months = explode(',', $result->date_created);

$articles_per_month = array_count_values($months); // get count of articles per each month    

for ($i = 1; $i <= 12; $i++) // Loop Through 12-months
{
    $month_name = date('F', mktime(0, 0, 0, $i)); // Get the name of the month from the numeric month

    echo (in_array($i, $months)) // Check to see if articles for month, if so create link otherwise SPAN
        ? "<a href='/calendar/month/$i'>$month_name (" . $articles_per_month[$i] . ")</a>\n" // print link with number of articles
        : "<span>$month_name</span>\n";
}

This Will create the menu and if a month does not have articles will print out the month name within SPAN tags, if it does have articles will print out the month as a link along with the number of articles for that month.

Sign up to request clarification or add additional context in comments.

7 Comments

nifty solution with headaches and flu ;)
@MD.SahibBinMahboob thank you, however I was not satisfied with the answer and redid it, Much nicer now =o)
@cryptic Sorry to keep you waiting, even noob programmers need to sleep :D. Thank you for the solution :)
@cryptic One quick question. Is there any way to count number of articles per month?
@cryptic Thank you a lot :). You are a real life saver. If you ever come to Pirot, call me for a drink :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.