Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>******Edited**** (mvc solution added)</p> <p>Don't clutter your code with unnecessary functions, partials, etc. Why bother with HTML from the start, when you can create your data, <em>then</em> 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) :</p> <p><strong>[listing 1]</strong> application/controller/IndexController.php</p> <pre><code>class IndexController extends Zend_Controller_Action { public function indexAction() { $this-&gt;view-&gt;calData = new Default_Model_Calendar('2010-07-17'); } } </code></pre> <p><strong>[listing 2]</strong> application/models/Calendar.php</p> <pre><code>class Default_Model_Calendar { /* @var Zend_Date */ private $_date; /* @param Zend_Date|string|int $date */ public function __construct($date) { $this-&gt;_date = new Zend_Date($date); } /* @return Zend_Date */ public function getTime() { return $this-&gt;_date; } public function getData() { // normally, fetch data from Db // array( day_of_month =&gt; event_html, ... ) return array( 1 =&gt; 'First day of month', 4 =&gt; '&lt;span class="holiday"&gt;Independence Day&lt;/span&gt;', 17 =&gt; '&lt;img src="path/to/image.png" /&gt;' //... ); } } </code></pre> <p><strong>[lisging 3]</strong> application/view/scripts/index/index.phtml</p> <pre><code>echo $this-&gt;calendarTable($this-&gt;calData); </code></pre> <p><strong>[listing 4]</strong> application/view/helpers/CalendarTable.php</p> <pre><code>class Default_View_Helper_CalendarTable extends Zend_View_Helper_Abstract { private $_calData; public function calendarTable($calData = null) { if (null != $calData) { $this-&gt;_calData = $calData; } return $this; } public function toString() { $curDate = $this-&gt;_calDate-&gt;getTime(); $firstDay = clone $curDate(); // clone a copy to modify it safely $firstDay-&gt;set(Zend_Date::DAY, 1); $firstWeekDay = $firstDay-&gt;get(Zend_Date::WEEKDAY); $numDays = $curDate-&gt;get(Zend_Date::MONTH_DAYS); // start with an array of empty items for the first $firstweekDay of the month $cal = array_fill(0, $firstweekDay, '&amp;nbsp;'); // fill the rest of the array with the day number of the month using some data if provided $calData = $this-&gt;_calData-&gt;getData(); for ($i=1; $i&lt;=$numDays; $i++) { $dayHtml = '&lt;span class="day-of-month"&gt;' . $i . '&lt;/span&gt;'; 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, '&amp;nbsp;'); $cal = array_pad($cal, 42, '&amp;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 &amp; $row) { $row = implode('&lt;/td&gt;&lt;td&gt;', $row); } // finalize $cal to create actual rows... $calTable = implode('&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;', $calTable); return '&lt;table class="calendar"&gt;&lt;tr&gt;&lt;td&gt;' . $calTable . '&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;'; } public function __toString() { return $this-&gt;__toString(); } } </code></pre> <p>With this code, you can even set exactly what you want within the <code>$cal</code> array before calling <code>array_chunk</code> on it. For example, <code>$cal[] = $dayHtml . '&lt;a href="#"&gt;more&lt;/a&gt;';</code></p> <p>This also follow true MVC as data (in <code>Default_Model_Calendar</code>) and view (in <code>Default_View_Helper_CalendarTable</code>) 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!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload