php/mysql group list by first character

I have the following mysql query:

$manufacturers_query = "select m.manufacturers_id, m.manufacturers_name, m.manufacturers_image, mi.manufacturers_url
                              from manufacturers m
                              left join manufacturers_info mi on m.manufacturers_id = mi.manufacturers_id
                              order by manufacturers_name";
$manufacturers = $db->Execute($manufacturers_query);

while (!$manufacturers->EOF) {
   $man_content .= $manufacturers->fields['manufacturers_name'];
   $manufacturers->MoveNext();
}

How can I make the generated list grouped by first letter and add a first letter in front of the group? For example:

A

  • Alfa Romeo
  • Austin Martin

B

  • BMW
  • Buick

C

  • Cadillac
  • Chevrolet
  • Corvette

Answers


You could try something like this -

<?php

$manufacturers_query = "select m.manufacturers_id, m.manufacturers_name, m.manufacturers_image, mi.manufacturers_url
                            from manufacturers m
                            left join manufacturers_info mi on m.manufacturers_id = mi.manufacturers_id
                            order by manufacturers_name";
$manufacturers = $db->Execute($manufacturers_query);

$last_letter = '';

while (!$manufacturers->EOF) {
    $man_content = $manufacturers->fields['manufacturers_name'];
    if (strtoupper(substr($man_content, 0, 1)) != $last_letter) {
        $last_letter = strtoupper(substr($man_content, 0, 1));
        print "<h2>$last_letter</h2>";
    }
    print "<p>$man_content</p>";
    $manufacturers->MoveNext();
}

Or maybe this which first creates an array of manufacturers index by first letter -

<?php

$manufacturers_query = "SELECT m.manufacturers_id, m.manufacturers_name, m.manufacturers_image, mi.manufacturers_url
                            FROM manufacturers m
                            LEFT JOIN manufacturers_info mi ON m.manufacturers_id = mi.manufacturers_id
                            ORDER BY manufacturers_name";
$manufacturers = $db->Execute($manufacturers_query);

$firstLetterIndexedArray = array();

while (!$manufacturers->EOF) {

    $index = strtoupper(substr($manufacturers->fields['manufacturers_name'], 0, 1));
    $firstLetterIndexedArray[$index][] = $manufacturers->fields;

    $manufacturers->MoveNext();
}

print '<ol>';

foreach ($firstLetterIndexedArray as $letter => $man) {
    print "<li>$letter</li><li><ol>";

    foreach ($man as $manufacturer) {
        print "<li>{$manufacturer['manufacturers_name']}</li>";
    }

    print "</ol></li>";
}

print "</ol>";

Need Your Help

firefox pagemod include sometimes not working?

facebook firefox firefox-addon-sdk

The piece of code for a firefox pagemod here doesn't work on every facebook page:

implementing swipe between tab using tab-host in Android

android android-fragments android-tabhost

In Android can I implement swipe between tabs functionality using tab-host. Is it possible to use this functionality without fragments. When we are targeting older android versions how can we imple...