views:

31

answers:

1

I am creating a blog post scheduling system using CodeIgniter. I want 10 posts to show up a day. There is a field in the posts table named scheduled_date which I will get the posts that are less than or equal to the current date. When an admin user adds a new record to the database, I need an SQL statement that somehow will help me COUNT the number of records with the latest date in the database. For example:

// 9 records returned for the date 2011-01-01
$numbers_of_records == 9;
if($numbers_of_records == 10){
    // inserts record with `scheduled_date`='2011-01-01'
}else{
    // inserts record with the date latest date +1 day
}

How would I efficiently accomplish this?

Thanks

A: 

This will do the trick. It is simple and efficient.

<?php

//  It is very bad to have floating values, especially for settings
//  it is good to use some sort of factory or settings class
$maxDailyPosts = (int) SettingsFactory::getSettings()->get('maxDailyPosts');
$date = '2011-01-01';

//  Load # of post for data
$numberOfRecords = (int) getNumberOfPostPerDate($date);

//  Figure out the interval for increment
$dayInterval = ($numberOfRecords >= $maxDailyPosts ) ? 1 : 0;

//  
$query = "INSERT INTO tbl (publish_date, ...) VALUES (DATE_ADD('$date', INTERVAL $dayInterval DAY), ...)";

?>
Alex