dongtang4954 2010-07-17 06:08
浏览 23
已采纳

在使用MVC时如何处理循环中的html?

Can someone please show me how to do this basic thing using Zend Framework MVC?

I'm looping over the timestamp data and populating my table that way. i don't understand how I would pull my presentation HTML from this loop and stick it in the view? Any help would be greatly appreciated!

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

I'm wanting to turn the above code into functions, but the HTML is throwing me off.

  • 写回答

1条回答 默认 最新

  • dongpu9481 2010-07-17 06:39
    关注

    ******Edited**** (mvc solution added)

    Don't clutter your code with unnecessary functions, partials, etc. Why bother with HTML from the start, when you can create your data, then transform it into an HTML table? Here's the MVC sample (the following code suppose a one module project called 'default', modify accordingly if the project is module based) :

    [listing 1] application/controller/IndexController.php

    class IndexController extends Zend_Controller_Action {
       public function indexAction() {
          $this->view->calData = new Default_Model_Calendar('2010-07-17');
       }
    }
    

    [listing 2] application/models/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] application/view/scripts/index/index.phtml

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

    [listing 4] application/view/helpers/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();
       }
    }
    

    With this code, you can even set exactly what you want within the $cal array before calling array_chunk on it. For example, $cal[] = $dayHtml . '<a href="#">more</a>';

    This also follow true MVC as data (in Default_Model_Calendar) and view (in Default_View_Helper_CalendarTable) are completely separated, giving you the freedom to use any other model with the view helper, or simply not using any view helper with your model!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么apriori的运行时间会比fp growth的运行时间短呢
  • ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"