2010-07-17 5 views
0

누군가 Zend Framework MVC를 사용하여이 기본 작업을 수행하는 방법을 보여줄 수 있습니까?MVC를 사용할 때 루프에서 html을 어떻게 처리합니까?

저는 타임 스탬프 데이터를 루핑하여 그런 식으로 표를 채 웁니다. 이 루프에서 프리젠 테이션 HTML을 가져 와서 뷰에 고정시키는 방법을 모르겠다. 어떤 도움이라도 대단히 감사하겠습니다!

<table> 
<?php 
    $day = date("j"); 
    $month = date("m"); 
    $year = date("Y");   
    $currentTimeStamp = strtotime("$year-$month-$day"); 
    $numDays = date("t", $currentTimeStamp); 
    $counter = 0; 

      for($i = 1; $i < $numDays+1; $i++, $counter++) 
      { 
       $timeStamp = strtotime("$year-$month-$i"); 
       if($i == 1) 
       { 
       // Workout when the first day of the month is 
       $firstDay = date("w", $timeStamp); 

       for($j = 0; $j < $firstDay; $j++, $counter++) 
       echo "<td>&nbsp;</td>"; 
       } 

       if($counter % 7 == 0) { 
        echo "</tr><tr>"; 
       } 

        echo "<td>" .$i . "</td>"; 

      } 
?> 
</table> 

위의 코드를 기능으로 바꾸고 싶지만 HTML이 나를 버리고 있습니다.

답변

2

는 ****** 편집 ****

처음부터 HTML 귀찮게 왜 등 불필요한 기능, 파셜와 코드를 어수선하게하지 마세요, 당신이 만들 수있을 때 (MVC 용액을 첨가) 귀하의 데이터, 다음 HTML 테이블로 변환합니까?

[목록 1] 응용 프로그램/컨트롤러/IndexController.php

class IndexController extends Zend_Controller_Action { 
    public function indexAction() { 
     $this->view->calData = new Default_Model_Calendar('2010-07-17'); 
    } 
} 
다음은 MVC 샘플입니다 (다음 코드는 '기본'이라는 하나 개의 모듈 프로젝트, 프로젝트가 모듈을 기반으로하는 경우 그에 따라 수정을 가정)

[리스팅 2 애플리케이션/모델/Calendar.php

class Default_Model_Calendar { 
    /* @var Zend_Date */ 
    private $_date; 
    /* @param Zend_Date|string|int $date */ 
    public function __construct($date) { 
     $this->_date = new Zend_Date($date); 
    } 
    /* @return Zend_Date */ 
    public function getTime() { 
     return $this->_date; 
    } 
    public function getData() { 
     // normally, fetch data from Db 
     // array(day_of_month => event_html, ...) 
     return array(
     1 => 'First day of month', 
     4 => '<span class="holiday">Independence Day</span>', 
     17 => '<img src="path/to/image.png" />' 
     //... 
    ); 
    } 
} 

[lisging 3 애플리케이션/

echo $this->calendarTable($this->calData); 

이 코드 응용 프로그램 /보기/헬퍼/CalendarTable.php

class Default_View_Helper_CalendarTable extends Zend_View_Helper_Abstract { 
    private $_calData; 
    public function calendarTable($calData = null) { 
     if (null != $calData) { 
     $this->_calData = $calData; 
     } 

     return $this; 
    } 

    public function toString() { 
     $curDate = $this->_calDate->getTime(); 
     $firstDay = clone $curDate(); // clone a copy to modify it safely 
     $firstDay->set(Zend_Date::DAY, 1); 

     $firstWeekDay = $firstDay->get(Zend_Date::WEEKDAY); 
     $numDays = $curDate->get(Zend_Date::MONTH_DAYS); 

     // start with an array of empty items for the first $firstweekDay of the month 
     $cal = array_fill(0, $firstweekDay, '&nbsp;'); 
     // fill the rest of the array with the day number of the month using some data if provided 
     $calData = $this->_calData->getData(); 
     for ($i=1; $i<=$numDays; $i++) { 
     $dayHtml = '<span class="day-of-month">' . $i . '</span>'; 
     if (isset($calData[$i])) { 
      $dayHtml .= $calData[$i]; 
     } 
     $cal[] = $dayHtml; 
    } 

    // pad the array with empty items for the remaining days of the month 
    //$cal = array_pad($cal, count($cal) + (count($cal) % 7) - 1, '&nbsp;'); 
    $cal = array_pad($cal, 42, '&nbsp;'); // OR a calendar has 42 cells in total... 

    // split the array in chunks (weeks) 
    $calTable = array_chunk($cal, 7); 
    // for each chunks, replace them with a string of cells 
    foreach ($calTable as & $row) { 
     $row = implode('</td><td>', $row); 
    } 
    // finalize $cal to create actual rows... 
    $calTable = implode('</td></tr><tr><td>', $calTable); 

    return '<table class="calendar"><tr><td>' . $calTable . '</td></tr></table>'; 
    } 
    public function __toString() { 
     return $this->__toString(); 
    } 
} 

[4 목록], 당신도 당신이 원하는 것을 정확하게 설정할 수 있습니다/index.phtml보기/스크립트/색인 array_chunk을 호출하기 전에 $cal 배열 내에 있어야합니다. 예를 들어, $cal[] = $dayHtml . '<a href="#">more</a>';

이 또한보기 도우미로 다른 모델을 사용하는 당신에게 자유를주고, 완전히 분리되어 (Default_View_Helper_CalendarTable에서) (Default_Model_Calendar에서) 데이터와보기로 진정한 MVC를 따르거나 간단히보기 도우미를 사용하지 않는 당신의 모델로!

+0

안녕하세요. 이것은 저를 위해 아주 잘 작동하고 있습니다. 방금 $ calTable을 반환하고보기로 보냈습니다. 다시 한번 감사드립니다. – Joel

+0

이 코드가 뷰 스크립트 외부에서 생성되거나 뷰 도우미 외부에서 생성 된 경우 MVC 패턴을 위반하게됩니다 (사용자가 디스패치 요청의 뷰 조각 외부에서 HTML 뷰 데이터를 생성하기 때문에 :)) 그러나, 다행히 도왔다! –

+0

젠드 프레임 워크를 사용하고 있으므로 MVC를 고수하고 배우고 싶습니다. 위의 $ calTable = implode 행 다음에 calTable을 반환했습니다. 컨트롤러에서 객체로 코드의 새 인스턴스를 만든 다음 뷰에 표시했습니다. 올바른 용어가 누락되면 죄송합니다. 이것이 올바른 방법입니까? 작동하지만 적절한 MVC를 따르고 싶습니다. 이것이이 질문의 기본이었습니다.이 상황을 어떻게 처리하고 HTML 만보기에 유지할 것인지 잘 모르겠습니다. – Joel

관련 문제