开发者

how do I treat html in a loop when using MVC?

开发者 https://www.devze.com 2023-01-07 08:13 出处:网络
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 presentat

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 
           开发者_StackOverflow中文版     $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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消