2012-12-11 2 views
1

특정 달을 클릭하여 그 달의 모든 기사를 볼 수있는 작은 달력 페이지가 있습니다. 기사가없는 달이 몇 달 있으며 사용자가 해당 달을 클릭하면 비어 있음을 알 수 있습니다. 1 년 동안 매월 모든 기사를 볼 수 있고 후반 부분의 현재 데이터를보기 부분에 어떻게 집계 할 수 있습니까? 기사에 대한Codeigniter 2.1 - 매달 DB 데이터 계산

DB는 다음과 같이 진행됩니다

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

HTML 코드 :

<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> 
+0

테이블 구조를 게시 할 수 있습니까? = o) 우리는 어떤 유형의 필드가 다루어 졌는지 알기 때문에 MySQL 함수에서 빌드를 사용하여 월을 계산해야 할 필요가 있습니다. 또한 달력과 관련된 PHP 코드와 함께 제공되는 HTML 양식. 이 작업을 수행하려면 테이블 열 목록 이상이 필요합니다. –

+0

@cryptic HTML 코드를 추가했습니다. 위의 테이블 구조는 기사에 대한 모든 것입니다. – Sasha

+0

테이블 열의 필드 유형은 무엇입니까? –

답변

2

동적이 될 것 메뉴 작성하는 PHP :이 생성됩니다

$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"; 
} 

을 메뉴가 있고 한 달에 기사가 없으면 SPAN 태그 안에 월 이름이 인쇄됩니다. 기사에 인쇄하려면 해당 월의 기사 수와 함께 링크로 월별 정보를 제공합니다.

+0

두통과 독감이있는 멋진 솔루션;) –

+0

@ MD.SahibBinMahboob, 감사합니다. 그러나 대답에 만족하지 않고 다시 작성했습니다. 훨씬 좋네요 = O) –

+0

@cryptic 기다리게해서 죄송합니다. , 심지어 멍청한 프로그래머는 잠을 잘 필요가 있습니다. D. 해결책을 가져 주셔서 감사합니다 :) – Sasha