2010-09-21 54 views
0

我正在使用CodeIgniter創建博客帖子日程安排系統。我想要10個帖子來顯示一天。在名爲scheduled_dateposts表中有一個字段,我將獲取小於或等於當前日期的帖子。當管理員用戶向數據庫中添加新記錄時,我需要一個SQL語句,以某種方式幫助我計算數據庫中具有最新日期的記錄數。例如:MySQL - 博客帖子日程安排系統

 
// 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 
} 

我該如何有效地完成這項工作?

謝謝

回答

0

這將做到這一點。它簡單而高效。

<?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), ...)"; 

?>