Alphabetical order on While?

Asked

Viewed 235 times

1

I have a list of teachers on a website, and it’s being printed in chronological order, and I wanted to put it alphabetically.

Follows code:

<div class="row">
    <ul class="team-list sort-destination appear-animation animated fadeInUp appear-animation-visible" data-appear-animation="fadeInUp" data-appear-animation-delay="50" data-sort-id="team">
        <? while ($p = $prof->fetch(PDO::FETCH_ASSOC)){?>
            <li class="col-md-3 col-sm-6 col-xs-12 isotope-item <?=str_replace(' ', '-', tirarAcentos($p['materia']));?>">
                <div class="team-item thumbnail">
                    <p class="thumb-info team">
                        <img class="img-responsive" alt="" src="http://editoraopirus.com.br/uploads/<?=$p['unidade'];?>/professores/<?=$p['url_foto'];?>">
                        <span class="thumb-info-title">
                            <span class="thumb-info-inner"><?=$p['nome'];?></span>
                            <span class="thumb-info-type"><?=$p['materia'];?></span>
                        </span>
                    </p>
                </div>
            </li>
        <? }?>
    </ul>
</div>
  • 1

    Can’t you change the database query to sort alphabetically? The database is much faster in that sort than PHP or Javascript

  • 3

    You have to touch the Query that brings these teachers. Just give a ORDER BY nome ASC

  • Thank you, I’ll test!!

1 answer

3

If you cannot change in your SQL query (recommended)

SELECT * FROM `tabela` ORDER BY `nome` ASC

8.2.1.13 ORDER BY

--

You can use the Sort()

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

?>

Will be printed:

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

Browser other questions tagged

You are not signed in. Login or sign up in order to post.