0

I have written a component that outputs a list in ul format. Currently the UL is built with the following:

<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <?php foreach ($this->items as $i => $item) : ?>
        <?php if ($item->latest_issue == '0') echo 
            "<li role='presentation'><a role='menuitem' tabindex='-1' href='" . $item->reader_url . "' target='_blank'>" . $item->issue_date . "</a></li>" ;?>
    <?php endforeach; ?>
</ul>

The only problem with this is that if an item is Unpublished, it still shows in this list. Any ideas where I'm going wrong?

1
  • 1
    i think you should filter only published items in model. – subashbasnet8 Jan 27 '15 at 11:12
1

You could use a simply check to ensure the state isn't 0, like so:

$item->state != 0

So your code would be:

<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <?php foreach ($this->items as $i => $item) : ?>
        <?php if ($item->latest_issue == '0' && $item->state != 0) echo 
            "<li role='presentation'><a role='menuitem' tabindex='-1' href='" . $item->reader_url . "' target='_blank'>" . $item->issue_date . "</a></li>" ;?>
    <?php endforeach; ?>
</ul>

Here is my version of your code which is prettified a little more. Always be sure to use : >> endif or { >> } for you conditional statements. I know you don't have to but when it comes down to debugging, it makes life so much easier:

<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <?php foreach ($this->items as $i => $item) : ?>
        <?php if ($item->latest_issue == '0' && $item->state != 0) : ?>
            <li role="presentation">
                <a role="menuitem" tabindex="-1" href="<?php echo $item->reader_url; ?>" target="_blank"><?php echo $item->issue_date; ?></a>
            </li>
        <?php endif; ?>
    <?php endforeach; ?>
</ul>

Update:

The ordering will be done in your model/database query like so:

->order($db->quoteName('id') . ' ASC');
0

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.