简体   繁体   中英

How would I sort this category list alphabetically with php?

I have looked at other examples and attempted to get this to work but the modifications to the code return only errors. Basically, I have a list of categories that are pulled from my database and displayed on 2 columns. I would like to have the categories listed alphabetically with the first half of the list in column 1 and the second half in column 2. I assume simply listing them alphabetically will automatically display them in the categories as I want. Thanks for any help. The code I have is:

<ul class="links">
            <?php if($this->is_loged) { ?>
            <li><a href="./"><strong><?php echo $this->translate('Pinners you follow');?></strong></a></li>
            <?php } ?>
            <?php if($this->categories) { ?>
            <li>
                <a class="arrow" href="<?php echo $this->all_url;?>"><?php echo $this->translate('Everything');?><?php if($this->category_active) { ?>: <?php echo $this->category_active;?><?php } ?></a>
                <div class="dropdown columns-2">
                    <?php $total = count($this->categories); ?>
                    <?php for($r=$i=0; $i<2; $i++) { ?>
                    <ul>
                        <?php for($j=0; $j<ceil( $total/2 ); $j++, $r++) { ?>
                        <?php if(isset($this->categories[$r])) { ?>
                        <?php 
                            $class = $this->categories[$r]['active'] ? 'active' : '';
                            if($r==0 || ceil( $total/2 ) == $r) { $class .= ' first'; }
                            if($r==($total-1) || (ceil( $total/2 )-1) == $r) { $class .= ' last'; }
                            $class = trim($class);
                        ?>
                        <li<?php if($class) {?> class="<?php echo $class;?>"<?php } ?>><a href="<?php echo $this->categories[$r]['href'];?>"><?php echo $this->categories[$r]['title'];?></a></li>
                        <?php } ?>
                        <?php } ?>
                    </ul>
                    <?php } ?>
                    <div class="clear"></div>
                </div>
            </li>
            <?php } ?></ul>

You can do this with MySQL sort_by name ASC , in PHP you can do this whith:

  1. sort($this->categories, SORT_STRING) or s ort($this->categories) ;
  2. ksort($this->categories) sort with keys;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM