1

I have a "Locations" module in our Magento store, but it spits out all of our store locations in a random order. It does list them by "State", but if there's 5 New Jersey listings, it doesn't alphabetize based on the Store name. This is the code we have now. This is the live locations page

http://www.jennyyoo.com/index.php/locations/

<div class="std locations">
    <div class="navigation">
        <ul>
            <li>US LOCATIONS:</li>
            <?php for ($i = 65; $i <= 90; $i++): ?>
                <li><a href="<?php echo $this->getUrl('jennyyoo_locations') ?>#letter-<?php echo chr($i) ?>"><?php echo chr($i) ?></a></li>
            <?php endfor; ?>
            <li><a href="<?php echo $this->getUrl('jennyyoo_locations/international') ?>">INTERNATIONAL</a></li>
        </ul>
        <ul class="legend">
            <li><img src="<?php echo $this->getSkinUrl('images/bridal-gowns.gif'); ?>"/>Bridal gowns</li>
            <li><img src="<?php echo $this->getSkinUrl('images/brides-maids.gif'); ?>"/>Bridesmaids</li>
        </ul>
    </div>
    <div class="listing">
        <?php foreach ($states as $state): ?>
            <div class="state-wrapper">
                <h5 <?php if (!in_array(substr($state['region'], 0, 1), $used_letters)) {echo 'id="letter-' . substr($state['region'], 0, 1) . '"'; $used_letters[] = substr($state['region'], 0, 1); } ?>>
                    <span><?php echo $state['region']; ?></span>
                </h5>
                <?php $locations = $this->getStateLocations($state['region']); ?>
                <?php foreach ($locations as $location): ?>
                    <div class="location-wrapper">
                        <h6>
                            <?php echo $location['name']; ?>
                            <?php if ($location['bridal_gowns']){?>
                                <img src="<?php echo $this->getSkinUrl('images/bridal-gowns.gif'); ?>"/>
                            <?php }?>
                            <?php if ($location['bridesmaids']){?>
                                <img src="<?php echo $this->getSkinUrl('images/brides-maids.gif'); ?>"/>
                            <?php }?>
                        </h6>
                        <address>
                            <?php echo nl2br($location['address_display']); ?>
                        </address>
                        <span class="www-url"><a href="<?php echo $location['website_url']; ?>"><?php echo $location['website_url']; ?></a></a>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endforeach; ?>
    </div>
</div>

1 Answer 1

0

This is php question and should be asked in stackoverflow. This is Magento dedicated SE site.

However, I will still try to answer here.

You just need to use one of the php sort function.

In your case, I would use asort($array), this uses value to sort.

So your code should be:

<div class="std locations">
    <div class="navigation">
        <ul>
            <li>US LOCATIONS:</li>
            <?php for ($i = 65; $i <= 90; $i++): ?>
                <li><a href="<?php echo $this->getUrl('jennyyoo_locations') ?>#letter-<?php echo chr($i) ?>"><?php echo chr($i) ?></a></li>
            <?php endfor; ?>
            <li><a href="<?php echo $this->getUrl('jennyyoo_locations/international') ?>">INTERNATIONAL</a></li>
        </ul>
        <ul class="legend">
            <li><img src="<?php echo $this->getSkinUrl('images/bridal-gowns.gif'); ?>"/>Bridal gowns</li>
            <li><img src="<?php echo $this->getSkinUrl('images/brides-maids.gif'); ?>"/>Bridesmaids</li>
        </ul>
    </div>
    <div class="listing">
        <?php foreach ($states as $state): ?>
            <div class="state-wrapper">
                <h5 <?php if (!in_array(substr($state['region'], 0, 1), $used_letters)) {echo 'id="letter-' . substr($state['region'], 0, 1) . '"'; $used_letters[] = substr($state['region'], 0, 1); } ?>>
                    <span><?php echo $state['region']; ?></span>
                </h5>
                <?php $locations = $this->getStateLocations($state['region']); ?>
                <?php foreach ($locations as $location): ?>
                    <div class="location-wrapper">
                        <h6>
                            <?php echo $location['name']; ?>
                            <?php if ($location['bridal_gowns']){?>
                                <img src="<?php echo $this->getSkinUrl('images/bridal-gowns.gif'); ?>"/>
                            <?php }?>
                            <?php if ($location['bridesmaids']){?>
                                <img src="<?php echo $this->getSkinUrl('images/brides-maids.gif'); ?>"/>
                            <?php }?>
                        </h6>
                        <address>
                            <?php echo nl2br($location['address_display']); ?>
                        </address>
                        <span class="www-url"><a href="<?php echo $location['website_url']; ?>"><?php echo $location['website_url']; ?></a></a>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endforeach; ?>
    </div>
</div>

Update

Since asort() doesn't work in multidimensional array, you should use usort() in this case.

I recommend using this in your block file rather than phtml file.

In your block file, sort your $states as follow:

$newStates = array();
foreach ($states as $key => $row) {
    // replace 0 with the field's index/key
    $newStates[$key]  = $row[0];
}

$finalStates = array_multisort($newStates, SORT_DESC, $states);

Then call your $finalStates in your phtml file.

Assumption

  1. $states is an multidimensional array
2
  • Hi Thanks for the help, but unfortunately this code didn't work, and broke the code. :( Mar 16, 2016 at 17:48
  • Check my update. Mar 16, 2016 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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