help with php script for sorting list in alpha order and using 3 db fields
<p>hey guys, i manage to get this script working to pull data from my database and sort them in alpha order seperating them into section based on the first letter of their last names.</p>

<p>my problem is i'm not sure how i can introduce a 3rd variable into the mix.</p>

<pre><code>    // Query for data
    $sql = "SELECT SUBSTRING(`last_name`, 1, 1) AS letter, `first_name`, `last_name`, `id` FROM `prospects` WHERE `user_id` = ".$uid." ORDER BY `last_name`";
    $query = mysql_query ($sql) or die (mysql_error());
    while ($records = @mysql_fetch_array ($query)) {
        $alpha[$records['letter']] += 1;
        ${$records['letter']}[$records['first_name']] = $records['last_name'];
    }
    echo '&lt;div id=""&gt;&lt;p&gt;You have ' . $prospect_count . ' Prospect' . ($prospect_count == 1 ? '' : 's') . ' | ' . '&lt;a href="#" class="new"&gt;Create new&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;';
    // Create Alpha link Listing
    foreach(range('A','Z') as $i) {
        echo (array_key_exists ("$i", $alpha)) ? '&lt;a href="#'.$i.'" title="'.$alpha["$i"].' results"&gt;'.$i.'&lt;/a&gt;' : "$i";
        echo ($i != 'Z') ? ' | ':'';
    }

    // Create Data Listing
    foreach(range('A','Z') as $i) {
        if (array_key_exists ("$i", $alpha)) {
            echo '&lt;div class="alpha"&gt;&lt;span class="expand-alpha-up"&gt;&lt;/span&gt;&lt;a href="#" name="'.$i.'"&gt;'.$i.'&lt;/a&gt;&lt;/div&gt;&lt;div class="show"&gt;';
            foreach ($$i as $key=&gt;$value)
                echo '&lt;a href="#" id="'.$pid.'"&gt;&lt;p&gt;'.$value.' '.$key.'&lt;/p&gt;&lt;/a&gt;';
        }
        echo '&lt;/div&gt;';
    }
</code></pre>

<p>i'm trying to pretty much make $pid = $records['id']; but i cant think of a way to do it without introducing a while loop inside the foreach or wrapping it around the last foreach.</p>

<p>EDIT:</p>

<p>I am not trying to sort the code, what this script does is this.</p>

<p>query the table and grabs the last_name and cuts it down so for example 'Smith' become 'S' and 'Doe' will become 'D'</p>

<p>it then selects first_name and last_name</p>

<p>then it tales the first letter from the last name that we have cut down and introduces as 'letters'</p>

<p>then it uses that to create a list. It outputs all the D's and groups them together, then it grabs all the S's and groups those together making it looks like this</p>

<h2>D</h2>

<p>Doe, John</p>

<h2>S</h2>

<p>Smith Bob</p>

<p>What I want to do is add another field from my query but dont know how to so that it could out put something like this</p>

<h2>S</h2>

<p>Smith Bob (7)</p>

<p>where 7 is the <code>id</code> for that item in the table.</p>
