php – Sort List Alphabetically

<?php
// connect to your database. change info to your info
$conn = mysql_connect(‘localhost’,’username’,’password’) or die(mysql_error());
$db = mysql_select_db(‘dbname’) or die(mysql_error());

// select info from your database. The list is sorted
// alphabetically in the query. alter to suit your needs.
$sql = “select * from table order by name”;
$result = mysql_query($sql, $conn) or die(mysql_error());

// while there are rows to be pulled from the result source…
while ($list = mysql_fetch_array($result)) {
// get the first letter of the name. let’s convert
// it to uppercase to make sure that ‘A’ and ‘a’ get
// put into the same group.
$letter = strtoupper(substr($list[‘name’],0,1));

// now we will check to see if the first letter of the
// current name is the same as the first letter of the
// previous name. If it is not, then we make a new
// group ‘header’ with the letter. Since there is no
// previous row on the first pass, we will automatically
// have an underlined ‘A’ made (or whatever your list starts with)
if ($letter != $prev_row) {
echo “<br><u>$letter</u><br>”;
} // end if

// here we just echo out the name
echo “{$list[‘name’]} <br>”;

// and here we assign the current letter to $prev_row so
// that on the next iteration of the loop, we will have
// a previous row letter to compare
$prev_row = $letter;
} // end while
?>

11 Comments »

  1. Jack said

    will try this

  2. vivek said

    hello,
    i got the problem in sorting please give me suggestions

    <?php

    echo "”;
    foreach(range(‘A’,’Z’) as $i) echo ““.$i.” “;
    echo “”;

    if(isset($_GET[‘current_char’]))
    {
    $current_char = $_GET[‘current_char’];
    $result = mysql_query(“SELECT name, LEFT(name, 1) AS first_char FROM members
    WHERE UPPER(name) BETWEEN ‘A’ AND ‘Z’
    ORDER BY name”);

    //$result = strtoupper(substr($result[‘name’],0,1));

    while ($row = mysql_fetch_assoc($result))
    {

    if ($row[‘first_char’] == $current_char)
    {
    $current_char = $row[‘first_char’];
    echo ‘Members with name starting from ‘.strtoupper($current_char) . ”;

    $name = $row[‘name’];
    $specific_detail_query = mysql_query(“SELECT * FROM `members` WHERE `name` = ‘$name’ AND `active` = ‘1’”) or die(mysql_error());
    while($specific_rows = mysql_fetch_assoc($specific_detail_query))
    {
    $nokh_id = $specific_rows[‘nokh_id’];
    $zone_id = $specific_rows[‘zone_id’];
    $fname = $specific_rows[‘name’];
    $sur_name = $specific_rows[‘sur_name’];
    $location = $specific_rows[‘photo’];
    $profile_id = $specific_rows[‘profile_id’];
    ?>


    <a href="open_profile.php?profile_id=” target=”_blank”>

    <img src ="” width = ’76’ height = ’66’ >

    <?php echo "”.$fname.” “.$sur_name.””; ?>

    <?php
    $query4 = mysql_query("SELECT * FROM `nokh` WHERE `nokh_id` = '$nokh_id'");
    $row4 = mysql_fetch_assoc($query4);
    $nokh = $row4['nokh'];
    echo "”.$nokh.””;
    ?>

    <?php
    $query3 = mysql_query("SELECT * FROM `zones` WHERE `zone_id` = '$zone_id'");
    $row3 = mysql_fetch_assoc($query3);
    $zone = $row3['zone'];
    echo "”.$zone.””;
    ?>


    <?php
    }

    }
    else
    die ("No more records found”);
    }
    }
    //End of if when some aplhabet is selected.

    ?>

  3. sapna said

    hi, i m facing the same problem with sorting a list of categories..pls if some one can help send me the code i want to sort and show the sorted list in the form…

  4. Abhishek said

    Try this code to a new project

  5. Cristian said

    Code working fine, I just used it for sorting users, thanks 🙂

  6. booky said

    Books in PDF format, free to download

    Books in PDF format

  7. misu said

    how do i put in alphabetic order this code:
    “””
    $cat_father_id = $cat[‘cat_father_id’];
    }
    }

    if ($reverseSort) {
    arsort($dirArray);
    } else {
    asort($dirArray);
    }
    reset($dirArray);

    $dir = “”;
    foreach ($dirArray as $key => $value){
    $dir .= $value;
    }

    if ($skipFirstSymbol) {
    $dir = substr($dir, strlen($config[‘dirSymbol’]));
    }

    return $dir;
    }
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>

    a to z in order…
    thanks

  8. I got this web page from my friend who informed me
    regarding this web page and at the moment this time I am visiting this website and reading very informative articles or reviews here.

  9. I know this website presents quality dependent posts and other data, is there any other web page which presents such data in quality?

  10. starfish said

    What’s up to every one, its in fact a pleasant for me to pay a visit this site, it consists of useful Information.

  11. Hello he blog doesnt load properly when i try to load the pages using the web browser SEWERLUST 22 I suspect its a problem coming from either your theme or maybe your plugins https://mmorpggaminghd.wordpress.com/2016/04/28/swtor-commando-speedrun-full-mission-guide/

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: