foreach逻辑不重复html

I want to get all my page titles from my WordPress database. So I created a query like:
$results = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'company_name' ORDER BY meta_value ASC");

After that a foreach loop to extract all values.
I want to seperate every page name by the first letter.
So I created a substr function to get the first letter.
This all works. The problem is the following
I need to echo the values and store them under their right letter.
Like:

---------A---------
A company
A nother company
---------B---------
B company
B nother company

So I did this:

if($first == 'A')
{
    echo '<a name="A"></a>
          <div class="nav-indicator" id="nav-a">A</div>
          <ul>'.$table.'</ul>
          </li>';
}   
if($first == 'B')
{
    echo '<a name="B"></a>
          <div class="nav-indicator" id="nav-b">B</div>
          <ul>'.$table.'</ul>
          </li>';
}   
.... ETC.

The $table var contains a <li><table>...</table></li>

Every time the foreach loops over the array he repeats the ---------A---------

For some dumb reason I can't figure out how to not repeat this. I don't really want to create 26 different foreach loops to do evert letter of the alphabet.

-- EDIT --
This is the foreach loop:

foreach ($results as $result){
    $post_id = $result->post_id;
    $company_name = get_post_meta($post_id, 'company_name', true); 
    $first = substr($company_name, 0, 1);
    $table = '<li><table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#FFFFFF"><tr><td width="75%"><strong>'.$company_name.'</strong></td>';        
     if($first == 'A'){
        echo '<a name="'.$first.'"></a><div class="nav-indicator" id="nav-'.strtolower($first).'">'.$first.'</div><ul>'.$table.'</ul>';}                                    
    }

Ok a really simple way to fix your code would be to do the following:

Set an external variable:

$i = 0; 

if($first == 'A')
{   
    //Check if it's the first time in this loop. 
    if($i == 0){
        $i = 1;
        echo '<a name="A"></a>';
        echo '<div class="nav-indicator" id="nav-a">A</div>';
    }

    echo '<ul>'.$table.'</ul>
    </li>';
}   

That way you can tell the first time that A has been hit. If it has been hit a second time then the i variable is now set to 1 and it won't display your title again.

This might do the trick for ya. I've guessed at your object property names :-)

// prime an array of A-Z results
$letters = array_fill_keys(range('A', 'Z'), array());

// for each result, plop it into the correct lettered array
foreach ($results as $result) {
    $letters[strtoupper($result->companyName[0])][$result->companyName] = $result;
}

// echo to screen
foreach ($letters as $letter => $companies) {
    // we put the companies in key by title, so sort by key
    ksort($companies);

    echo '<a name="' . $letter . '"></a>
          <div class="nav-indicator" id="nav-' . strtolower($letter) . '">' . $letter . '</div>
          <ul>';

    foreach ($companies as $company) {
        echo '<li>';

        // spit out your stuff

        echo '</li>';
    }

    echo '</ul>';
}