• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

sort on foreach

trying to do a sort on a certain field but I am uncertain of how to do it.
<?php foreach($node->field_subcats as $sub): ?>
                <?php $cat = node_load($sub['nid']); ?>
                <li><a href="<?php print url('node/'.$cat->nid) ?>" id="#subcat-<?php print $cat->nid ?>"><?php print $cat->title ?></a></li>
                <?php endforeach ?>

Open in new window

0
iceman19330
Asked:
iceman19330
1 Solution
 
gazzzzzzerCommented:
Here is an example that should help you get started. I have ordered by the title and then called your code to display the links, then also ordered by cat id and then called your code, this was to give you 2 examples.

I have not been able to test this code but should get you started.

<?php
// Obtain a list of the columns to be sorted
foreach ($node->field_subcats as $key => $sub) {
    $cat = node_load($sub['nid']);
    $cattitle[$key]  = $cat->title;
    $catid[$key] = $cat->nid;
}

// Sort by the sub cat title ascending
array_multisort($cattitle, SORT_ASC, $node->field_subcats);
?>

<?php foreach($node->field_subcats as $sub): ?>
                <?php $cat = node_load($sub['nid']); ?>
                <li><a href="<?php print url('node/'.$cat->nid) ?>" id="#subcat-<?php print $cat->nid ?>"><?php print $cat->title ?></a></li>
                <?php endforeach ?>


<?php

// Sort by the sub cat id ascending
array_multisort($catid, SORT_ASC, $node->field_subcats);
?>

<?php foreach($node->field_subcats as $sub): ?>
                <?php $cat = node_load($sub['nid']); ?>
                <li><a href="<?php print url('node/'.$cat->nid) ?>" id="#subcat-<?php print $cat->nid ?>"><?php print $cat->title ?></a></li>
                <?php endforeach ?>

Open in new window


0
 
iceman19330Author Commented:
thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now