php / mysql组列出的第一个字符

时间:2012-04-04 10:51:41

标签: php mysql

我有以下mysql查询:

$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();
}

如何将生成的列表按第一个字母分组并在组前面添加第一个字母? 例如:

A

  • 阿尔法罗密欧
  • Austin Martin

  • 宝马
  • 别克

C

  • 凯迪拉克
  • 雪佛兰
  • 克尔维特

2 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情 -

<?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();
}

或许这首先通过第一个字母创建一系列制造商索引 -

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

答案 1 :(得分:0)

您可以使用php轻松完成此操作。

创建$currentChar = "";

并在循环中获取制造商的第一个字符,并将其与$currentChar进行比较 如果$currentChar != manufacturerFirstChar

关闭ol,将$currentChar更新为新的字符,打印并打开新的ol

像这样:

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

我没有测试过,但我相信是这样的。