0

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

生成されたリストを最初の文字でグループ化し、グループの前に最初の文字を追加するにはどうすればよいですか? 例えば:

  • アルファロメオ
  • オースティン・マーティン

B

  • BMW
  • ビュイック

  • キャデラック
  • シボレー
  • コルベット
4

2 に答える 2

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>";
于 2012-04-04T12:04:50.230 に答える
0

phpで簡単にできます。

作成する$currentChar = "";

$currentChar ループ内でメーカーの最初の文字を取得し、それをIfと比較します$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>";

私はそれをテストしていませんが、私はこのようなものだと信じています。

于 2012-04-04T13:18:43.207 に答える