使用 MVC 时如何在循环中处理 html?
有人可以告诉我如何使用 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> </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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
******已编辑****(已添加 mvc 解决方案)
不要用不必要的函数、部分等来扰乱您的代码。为什么要从头开始使用 HTML,当您可以创建数据时,然后< /em> 将其转换为 HTML 表格?这是 MVC 示例(以下代码假设有一个名为“default”的单模块项目,如果该项目是基于模块的,则进行相应修改):
[清单 1] application/controller/IndexController.php
[清单 2] application/models/Calendar.php
[lisging 3] application/view/scripts/index/index.phtml
[清单 4] application/view /helpers/CalendarTable.php
使用此代码,您甚至可以在调用
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
[listing 2] application/models/Calendar.php
[lisging 3] application/view/scripts/index/index.phtml
[listing 4] application/view/helpers/CalendarTable.php
With this code, you can even set exactly what you want within the
$cal
array before callingarray_chunk
on it. For example,$cal[] = $dayHtml . '<a href="#">more</a>';
This also follow true MVC as data (in
Default_Model_Calendar
) and view (inDefault_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!