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

发布于 2024-09-10 06:31:35 字数 1047 浏览 6 评论 0原文

有人可以告诉我如何使用 Zend Framework MVC 做这个基本的事情吗?

我正在循环时间戳数据并以这种方式填充我的表。我不明白如何从这个循环中提取演示文稿 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 却让我失望了。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独行侠 2024-09-17 06:31:35

******已编辑****(已添加 mvc 解决方案)

不要用不必要的函数、部分等来扰乱您的代码。为什么要从头开始使用 HTML,当您可以创建数据时,然后< /em> 将其转换为 HTML 表格?这是 MVC 示例(以下代码假设有一个名为“default”的单模块项目,如果该项目是基于模块的,则进行相应修改):

[清单 1] application/controller/IndexController.php

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

[清单 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);

[清单 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, ' ');
      // 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, ' ');
     $cal = array_pad($cal, 42, ' ');   // 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();
   }
}

使用此代码,您甚至可以在调用 array_chunk 之前在 $cal 数组中准确设置您想要的内容。例如,$cal[] = $dayHtml 。 'more';

这也遵循真正的 MVC 作为数据(在 Default_Model_Calendar 中)和视图(在 Default_View_Helper_CalendarTable 中) code>) 完全分离,让您可以自由地将任何其他模型与视图助手一起使用,或者干脆不将任何视图助手与您的模型一起使用!

******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, ' ');
      // 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, ' ');
     $cal = array_pad($cal, 42, ' ');   // 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文