如何在Zend Framework中从数据库创建动态链接?

时间:2021-12-14 11:08:48

Basically I'm a bit stuck,

基本上我有点卡住,

I've been following the quick start on Zend site, and wish to make a dynamic navigation to the framework, I've got layout.phtml with $this->render('navigation.phtml); this has static links, but I wish to make them pull from a database table could someone in plain english not geekcaneeze explain the correct way of doing this, IE page by page with simple step by step guide on what each page is doing, as I'm not a PHP FREAK or Zend Framework master but a web designer who wants to progress in to the world of framework development, I understand the concept the vaules of it's use.

我一直关注Zend网站上的快速入门,并希望对框架进行动态导航,我已经使用$ this-> render('navigation.phtml)获得layout.phtml;这有静态链接,但我希望让他们从数据库表中拉出来可以用普通英语的人来geekcaneeze解释这样做的正确方法,IE逐页逐步指导每个页面正在做什么,就像我一样我不是PHP FREAK或Zend Framework的主人,而是一个想要进入框架开发世界的网页设计师,我理解它的使用的概念。

I'm sure it will cure a lot of headaches for a lot of newbies to this. in other words after reading zend frame work referance I'm still none the wiser what they are going on about.

我相信它会为很多新手解决很多麻烦。换句话说,在读完zend frame work referance后,我仍然没有更明智的是他们正在发生什么。

I've got it all working though Xampp and the file structure represent the same as

虽然Xampp和文件结构代表相同,但我已经完成了所有工作

application/ 
config/
controllers/
layout/script/
models/
views/script/index/
views/script/error/
library/ 
public/ 

regards

Mal

2 个解决方案

#1


Pull them out in a controller, pass them along (eg. as an array) to the view:

将它们拉出控制器,将它们(例如,作为数组)传递给视图:

$this->view->yourListOfLinks = getListOfLinksFromDB();

In the view (the .phtml) example output them using foreach:

在视图(.phtml)示例中使用foreach输出它们:

foreach($this->yourListOfLinks as $link) {
   echo "<a href=\"$link\">$link</a>";
  }

#2


Assuming you set up a class for your your database table (ZF - Create a Model and Database Table), you should be able to do something like this in your navigation.phtml file:

假设您为数据库表(ZF - 创建模型和数据库表)设置了一个类,您应该能够在navigation.phtml文件中执行以下操作:

<?php
$table = new Links_Table();
$links = $table->fetchAll();
?>

<? foreach ($links as $link) { ?>
   <a href="<?= $link->url ?>"><?= $link->title ?></a>
<? ?>

If you're creating internal site links then you could also set up some router rewrite rules (ZF - The Standard Router).

如果您正在创建内部站点链接,那么您还可以设置一些路由器重写规则(ZF - 标准路由器)。

#1


Pull them out in a controller, pass them along (eg. as an array) to the view:

将它们拉出控制器,将它们(例如,作为数组)传递给视图:

$this->view->yourListOfLinks = getListOfLinksFromDB();

In the view (the .phtml) example output them using foreach:

在视图(.phtml)示例中使用foreach输出它们:

foreach($this->yourListOfLinks as $link) {
   echo "<a href=\"$link\">$link</a>";
  }

#2


Assuming you set up a class for your your database table (ZF - Create a Model and Database Table), you should be able to do something like this in your navigation.phtml file:

假设您为数据库表(ZF - 创建模型和数据库表)设置了一个类,您应该能够在navigation.phtml文件中执行以下操作:

<?php
$table = new Links_Table();
$links = $table->fetchAll();
?>

<? foreach ($links as $link) { ?>
   <a href="<?= $link->url ?>"><?= $link->title ?></a>
<? ?>

If you're creating internal site links then you could also set up some router rewrite rules (ZF - The Standard Router).

如果您正在创建内部站点链接,那么您还可以设置一些路由器重写规则(ZF - 标准路由器)。