如何获取并显示多行?
How To Fetch And Display Multiple Rows?
我正在使用 zend 框架上的 Magento,以下代码当前输出匹配条件 is_read != 1', 'is_remove != 1'
的第一行。我需要修改此代码以输出符合上述条件的最后 4 table 行。我尝试了一些方法,但 none 奏效了。请帮忙!
ModuleName/Model/Resource/
public function loadLatestNotice(Mage_AdminNotification_Model_Inbox $object)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from($this->getMainTable())
->order($this->getIdFieldName() . ' DESC')
->where('is_read != 1')
->where('is_remove != 1')
->limit(1);
$data = $adapter->fetchRow($select);
if ($data) {
$object->setData($data);
}
$this->_afterLoad($object);
return $this;
}
这里有一些其他使用的代码...
ModuleName/Model/
public function loadLatestNotice()
{
$this->setData(array());
$this->getResource()->loadLatestNotice($this);
return $this;
}
ModuleName/Block/
public function getLatestNotice()
{
return $this->_getHelper()
->getLatestNotice()->getTitle();
}
Template/
href="<?php echo $latestNoticeUrl ?>" onclick="this.target='_blank';"><?php echo $this->getLatestNotice() ?>
我能够使用以下方法自行解决问题。
我尝试生成的第一件事是 4 条通知 table 行而不是 1 行,是将 ->limit(1);
更改为 ->limit(4);
并将 $adapter->fetchRow($select);
更改为 $adapter->fetchAll($select);
。问题是,解决方案需要的不仅仅是更改这 2 个值。
ModuleName/Model/Resource/
public function loadLatestNotice(Mage_AdminNotification_Model_Inbox $object)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from($this->getMainTable())
->order($this->getIdFieldName() . ' DESC')
->where('is_read != 1')
->where('is_remove != 1')
->limit(4);
$data = $adapter->fetchAll($select);
if ($data) {
$object->setData($data);
}
$this->_afterLoad($object);
return $this;
}
更改后,模板将停止输出信息,为了使模板输出新数组,必须复制一些代码并删除块文件中的->getTitle()
行,然后更改几行模板.phtml文件中的代码如下。
ModuleName/Block/
public function getNewFuncName()
{
return $this->_getHelper()
->getLatestNotice();
}
Template/
<?php
$notice = $this->getNewFuncName();
foreach ($notice as $item) {
foreach ($item as $value) {
echo '<div class="notemssg"><p id="notetitle" href='.$value['url'].' >'.$value['title'].'</p><p id="notedate">'.$value['date_added'].'</p></div>';
}
}
?>
更改代码以正确调用和显示数组将导致显示 4 table 行。可以修改代码以供使用以及您希望在前面显示信息的任何方式。
希望这对某人有所帮助!
我正在使用 zend 框架上的 Magento,以下代码当前输出匹配条件 is_read != 1', 'is_remove != 1'
的第一行。我需要修改此代码以输出符合上述条件的最后 4 table 行。我尝试了一些方法,但 none 奏效了。请帮忙!
ModuleName/Model/Resource/
public function loadLatestNotice(Mage_AdminNotification_Model_Inbox $object)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from($this->getMainTable())
->order($this->getIdFieldName() . ' DESC')
->where('is_read != 1')
->where('is_remove != 1')
->limit(1);
$data = $adapter->fetchRow($select);
if ($data) {
$object->setData($data);
}
$this->_afterLoad($object);
return $this;
}
这里有一些其他使用的代码...
ModuleName/Model/
public function loadLatestNotice()
{
$this->setData(array());
$this->getResource()->loadLatestNotice($this);
return $this;
}
ModuleName/Block/
public function getLatestNotice()
{
return $this->_getHelper()
->getLatestNotice()->getTitle();
}
Template/
href="<?php echo $latestNoticeUrl ?>" onclick="this.target='_blank';"><?php echo $this->getLatestNotice() ?>
我能够使用以下方法自行解决问题。
我尝试生成的第一件事是 4 条通知 table 行而不是 1 行,是将 ->limit(1);
更改为 ->limit(4);
并将 $adapter->fetchRow($select);
更改为 $adapter->fetchAll($select);
。问题是,解决方案需要的不仅仅是更改这 2 个值。
ModuleName/Model/Resource/
public function loadLatestNotice(Mage_AdminNotification_Model_Inbox $object)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from($this->getMainTable())
->order($this->getIdFieldName() . ' DESC')
->where('is_read != 1')
->where('is_remove != 1')
->limit(4);
$data = $adapter->fetchAll($select);
if ($data) {
$object->setData($data);
}
$this->_afterLoad($object);
return $this;
}
更改后,模板将停止输出信息,为了使模板输出新数组,必须复制一些代码并删除块文件中的->getTitle()
行,然后更改几行模板.phtml文件中的代码如下。
ModuleName/Block/
public function getNewFuncName()
{
return $this->_getHelper()
->getLatestNotice();
}
Template/
<?php
$notice = $this->getNewFuncName();
foreach ($notice as $item) {
foreach ($item as $value) {
echo '<div class="notemssg"><p id="notetitle" href='.$value['url'].' >'.$value['title'].'</p><p id="notedate">'.$value['date_added'].'</p></div>';
}
}
?>
更改代码以正确调用和显示数组将导致显示 4 table 行。可以修改代码以供使用以及您希望在前面显示信息的任何方式。
希望这对某人有所帮助!