Php Zend框架在循环中生成HTML

Php Zend框架在循环中生成HTML,php,model-view-controller,zend-framework,zend-db,zend-view,Php,Model View Controller,Zend Framework,Zend Db,Zend View,我是Zend框架的新手,希望获得有关如何最好地循环数据库数据以生成链接列表的建议。我的理解是,模型应该包含大多数应用程序逻辑,控制器和视图应该尽可能轻 我正在查询数据库以获得一组记录,我想循环遍历它们并生成HTML链接。下面是密码 控制器: $this->view->myList = MODEL->generateHtml(); function generateHtml() { query db loop through record set b

我是Zend框架的新手,希望获得有关如何最好地循环数据库数据以生成链接列表的建议。我的理解是,模型应该包含大多数应用程序逻辑,控制器和视图应该尽可能轻

我正在查询数据库以获得一组记录,我想循环遍历它们并生成HTML链接。下面是密码

控制器:

$this->view->myList = MODEL->generateHtml();
function generateHtml() {
    query db

    loop through record set

    build a string of html within loop including links

    return string to controller

}
echo $this->myList;
$this->view->myList = MODEL->getListOfItems();  // return an array of data
function getListOfItems() {
    $results = array(); // array of data to return

    // query db

    // loop over result set
    foreach($result as $row) {
        $results[] = $row;
    }

    return $results;
}
<?php echo $this->partialLoop('myList.phtml', $this->myList);
<tr>
    <td><a href="<?php echo $this->url(array('id' => $this->id))"><?php echo $this->username ?></a></td>
    <td><?php echo $this->firstName ?> <?php echo $this->lastName ?></td>
    <td><?php echo $this->email ?></td>
</tr>
型号:

$this->view->myList = MODEL->generateHtml();
function generateHtml() {
    query db

    loop through record set

    build a string of html within loop including links

    return string to controller

}
echo $this->myList;
$this->view->myList = MODEL->getListOfItems();  // return an array of data
function getListOfItems() {
    $results = array(); // array of data to return

    // query db

    // loop over result set
    foreach($result as $row) {
        $results[] = $row;
    }

    return $results;
}
<?php echo $this->partialLoop('myList.phtml', $this->myList);
<tr>
    <td><a href="<?php echo $this->url(array('id' => $this->id))"><?php echo $this->username ?></a></td>
    <td><?php echo $this->firstName ?> <?php echo $this->lastName ?></td>
    <td><?php echo $this->email ?></td>
</tr>
查看:

$this->view->myList = MODEL->generateHtml();
function generateHtml() {
    query db

    loop through record set

    build a string of html within loop including links

    return string to controller

}
echo $this->myList;
$this->view->myList = MODEL->getListOfItems();  // return an array of data
function getListOfItems() {
    $results = array(); // array of data to return

    // query db

    // loop over result set
    foreach($result as $row) {
        $results[] = $row;
    }

    return $results;
}
<?php echo $this->partialLoop('myList.phtml', $this->myList);
<tr>
    <td><a href="<?php echo $this->url(array('id' => $this->id))"><?php echo $this->username ?></a></td>
    <td><?php echo $this->firstName ?> <?php echo $this->lastName ?></td>
    <td><?php echo $this->email ?></td>
</tr>
这似乎将逻辑放在模型中,并将控制器灯光和视图仅用于渲染

我遇到的一个问题是,我想使用
$this->view->url
在我输出的html中生成路由链接,但在模型中无法这样做。我在网上的阅读表明,您不应该在模型中构建html。我可以在模型中生成所需数据的数组,并返回该数组,然后在控制器或视图中循环该数组以生成html,但我不确定正确的方法是什么,希望得到一些建议

谢谢你的帮助


新问题-更新代码:

您好…我有下面的建议,但现在有一个不同的问题

我的代码是:

型号:

不用于此测试。将返回与控制器中创建的数组相似的数组

控制器:

    $aStoryList = array( 
        array(
           'headline' => 'Headline 1', 
           'story' => 'Story 1' 
        ), 
        array(
           'headline' => 'Headline 2',
           'story' => 'Story 2'
        )
    );
    $this->view->aStoryList = $aStoryList;
视图:

storyList.phtml:

echo "<br />" . $this->headline . $this->story;
echo“
”$这是标题$这个故事;
我已经把这个部分放在这里了

视图/部分/storyList.phtml

视图中使用的位置和路径来源于此stackoverflow问题的答案-

当我运行此命令时,我得到以下错误

消息:在路径(/home/sites/xxxxx.com/public\u html/xxxxxxx/application/views/scripts/)中未找到脚本“partials/storyList.phtml”


现在把我的头发拔出来

该模型应用于从数据源提取数据,但不应生成任何HTML标记。保存视图的HTML生成。控制器将是模型和视图之间的粘合剂;也就是说,控制器将执行获取数据的工作,并将其交给生成输出的视图

在您的特定情况下,在循环中创建标记时应该使用

我建议使用以下伪代码,而不是您在上面发布的代码:

控制器:

$this->view->myList = MODEL->generateHtml();
function generateHtml() {
    query db

    loop through record set

    build a string of html within loop including links

    return string to controller

}
echo $this->myList;
$this->view->myList = MODEL->getListOfItems();  // return an array of data
function getListOfItems() {
    $results = array(); // array of data to return

    // query db

    // loop over result set
    foreach($result as $row) {
        $results[] = $row;
    }

    return $results;
}
<?php echo $this->partialLoop('myList.phtml', $this->myList);
<tr>
    <td><a href="<?php echo $this->url(array('id' => $this->id))"><?php echo $this->username ?></a></td>
    <td><?php echo $this->firstName ?> <?php echo $this->lastName ?></td>
    <td><?php echo $this->email ?></td>
</tr>
型号:

$this->view->myList = MODEL->generateHtml();
function generateHtml() {
    query db

    loop through record set

    build a string of html within loop including links

    return string to controller

}
echo $this->myList;
$this->view->myList = MODEL->getListOfItems();  // return an array of data
function getListOfItems() {
    $results = array(); // array of data to return

    // query db

    // loop over result set
    foreach($result as $row) {
        $results[] = $row;
    }

    return $results;
}
<?php echo $this->partialLoop('myList.phtml', $this->myList);
<tr>
    <td><a href="<?php echo $this->url(array('id' => $this->id))"><?php echo $this->username ?></a></td>
    <td><?php echo $this->firstName ?> <?php echo $this->lastName ?></td>
    <td><?php echo $this->email ?></td>
</tr>
查看:

$this->view->myList = MODEL->generateHtml();
function generateHtml() {
    query db

    loop through record set

    build a string of html within loop including links

    return string to controller

}
echo $this->myList;
$this->view->myList = MODEL->getListOfItems();  // return an array of data
function getListOfItems() {
    $results = array(); // array of data to return

    // query db

    // loop over result set
    foreach($result as $row) {
        $results[] = $row;
    }

    return $results;
}
<?php echo $this->partialLoop('myList.phtml', $this->myList);
<tr>
    <td><a href="<?php echo $this->url(array('id' => $this->id))"><?php echo $this->username ?></a></td>
    <td><?php echo $this->firstName ?> <?php echo $this->lastName ?></td>
    <td><?php echo $this->email ?></td>
</tr>

总结如下:

  • 控制器查询模型中的数据
  • 模型返回一个结果数组
  • 控制器将数组直接传递给视图
  • 视图调用
    partiallop
    helper并从模型中传递数组
  • partiallop
    helper迭代所有结果,一次将它们传递给
    myList.phtml
    (注意变量范围如何成为局部视图)
我的示例假设模型返回的数组包含键
id
username
firstName
lastName


希望对您有所帮助,如果您有任何问题,请随时发表意见。

您可以在另一个
partialoop
中调用
partialoop
?另一件事(虽然我不确定)是使用局部视图辅助程序不会对性能造成不良影响,应该使用
render
?您好,drew-非常感谢您花时间编写详细且信息丰富的响应。我还没有听说过partialLoop视图帮助程序,但我会看一看。看看你的伪代码,它看起来正是我想要的。非常感谢您的努力-干杯(stackoverflow不会让我投票支持您的答案,因为我是新手,但非常感谢您的努力)@Songo从查看
partialoop
partialoop
的代码中,我看不出您为什么不能从另一个
partialoop
中调用
partialoop
(只要您不通过传递相同的数据而陷入递归漏洞)。可能会有一些性能问题,因为
partiallop
调用
partial
,它
克隆
s您的视图对象,清除其变量,并将部分变量分配给它,然后调用
render()
在克隆视图上,并返回内容。看到一些比较partialLoop与loop和view render的基准测试可能会很有趣。只是一点小小的改进。对数据库的查询最好已经返回一个数组,因此创建数组时不需要PHP内部的额外循环。您好-我已经编辑了我的qu上面的估计说明了我遇到的一个新的路径相关问题。我确信PartialLop答案将在我只能运行它的情况下起作用。感谢所有能给我提供建议的人。