user1078494
user1078494

Reputation: 509

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

B

C

Upvotes: 0

Views: 1132

Answers (2)

Henrique Gonçalves
Henrique Gonçalves

Reputation: 1582

You can do this easily with php.

Create a $currentChar = "";

and in your loop get the first char of the manufacturer and compare it to $currentChar If $currentChar != manufacturerFirstChar

Close the ol, update $currentChar to the new char, print it and open a new ol

like this:

$currentChar = "";
$firstRun = true;
$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'];
    $temp = substr($man_content, 0, 1));
    if($temp != $currentChar)
    {
        $currentChar = $temp;
        if(!$firstRun)
        {
            // if it's not the first time, close previous li and ol
            echo "</li>";
            echo "</ol>";
        }
        else
        {
            $firstRun = false;
        }
        // print the char and open the ol
        echo "<span class=\"manChar\">".$currentChar."</span>";
        echo "<ol>";
    }
    echo "<li>".$man_content."</li>";
    $manufacturers->MoveNext();
 }
echo "</li>";
echo "</ol>";

I haven't tested it but I believe is something like this.

Upvotes: 0

user1191247
user1191247

Reputation: 12998

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>";

Upvotes: 1

Related Questions