0

The following code currently outputs the first row matching the criteria is_read != 1', 'is_remove != 1'. I need to modify this code to output the last 4 table rows that matches said criteria. I tried a few things but none worked.

This current code displays the latest notification at the top of the admin page. i want it to display the last 4 notification, one after the other.

Please Help!

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;
}

Here are some other codes that are used...

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/

<div class="notemssg" >
        <p id="notetitle" href="<?php echo $latestNoticeUrl ?>" onclick="this.target='_blank';"><?php echo $this->getLatestNotice() ?></p>
        <p id="notedate"><?php echo $this->getLatestNoticeDate() ?></p>
    </div>

ive tried changing the limited to 4, but i didnt see any changes. I've also tried using fetchall instead of fetchrow, which didnt show anything. I;m starting to wonder, do i need to do a array echo in the template code to display it?

  • try with limit(-4) – obscure Jan 1 '16 at 18:56
  • Or try limit(4, -4). Not sure if it accepts zend accepts negative values. – obscure Jan 1 '16 at 19:34
  • @obscure "-4" produced the same results as "4", while "4, -4" produced an error! – Nxlevel Jan 1 '16 at 19:49
  • How about adding a created_at column and sort by created_at desc ? Usually in this case you'd do somthing like this. – obscure Jan 1 '16 at 20:54
3
+50

in sql limit will display the number of rows you want to fatch. you can get 4 rows by limit it to 4 ->limit(4);

->order($this->getIdFieldName() . ' DESC') will display the and get the table rows in descending order so you can get last record first and than limit it to 4 will get latest 4 notifications in admin.

use $data = $adapter->fetchAll($select); instead of $data = $adapter->fetchRow($select);

this works for me i hope this also works for you buddy.

please clear cache after editing.

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);
    echo "<pre>";
    print_r($data);
    if ($data) {
        $object->setData($data);
    }

    $this->_afterLoad($object);

    return $this;
}

I hope this will help you.

  • it does print the array using the "print_r(data)" line for testing. but how do i display it on the webpage using niotification.phtml so it shows correctly? – Nxlevel Jan 4 '16 at 0:04
  • where you want to display? – denish vachhani Jan 4 '16 at 4:29
  • In order to achieve the results i wanted. i had to modify the template file to echo each array. just changing the code to what you provided doesn't work. It actually breaks the system and doesn't display anything it the template area. so the template code must be modified. – Nxlevel Jan 5 '16 at 20:29
0

If you want latest 4 record use limit with 4 and fetchAll method

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);
    return $adapter->fetchAll($select);
}

Yo can use collection object as well to get data in collection object

  • Your code didn't seem to work. The area is now blank. How do i use collection object? – Nxlevel Jan 1 '16 at 18:26
  • $collectionObject = Mage::getModel("your model")->getCollection() add your filter in this and set limit 4 – Minesh Patel Jan 2 '16 at 9:32
0

I was able to solve the problem myself, by using the following method. The first thing i tried to produce 4 notification table rows instead of 1, is to change ->limit(1); to ->limit(4); and $adapter->fetchRow($select); to $adapter->fetchAll($select);. After posting the issue, i realize many suggested this same solution. The issue is, the solution requires more than just changing these 2 values.

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;
}

After changing this, the template will stop outputting information, In order for the template to output the new array, you must duplicate some code and remove ->getTitle() line in the block file, then change a few line of codes in the template .phtml file as follows.

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>';
                }
            }
            ?>

Changing the code to properly call and display the array will result it 4 table rows being displayed. the code can be modified to be used and any way you would like to display the info on the fronted.

Hope this helps Someone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.