Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3635088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:51:37+00:00 2026-05-19T00:51:37+00:00

hey guys, i manage to get this script working to pull data from my

  • 0

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.

my problem is i’m not sure how i can introduce a 3rd variable into the mix.

    // 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 '<div id=""><p>You have ' . $prospect_count . ' Prospect' . ($prospect_count == 1 ? '' : 's') . ' | ' . '<a href="#" class="new">Create new</a></p></div>';
    // Create Alpha link Listing
    foreach(range('A','Z') as $i) {
        echo (array_key_exists ("$i", $alpha)) ? '<a href="#'.$i.'" title="'.$alpha["$i"].' results">'.$i.'</a>' : "$i";
        echo ($i != 'Z') ? ' | ':'';
    }

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

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.

EDIT:

I am not trying to sort the code, what this script does is this.

query the table and grabs the last_name and cuts it down so for example ‘Smith’ become ‘S’ and ‘Doe’ will become ‘D’

it then selects first_name and last_name

then it tales the first letter from the last name that we have cut down and introduces as ‘letters’

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

D

Doe, John

S

Smith Bob

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

S

Smith Bob (7)

where 7 is the id for that item in the table.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:51:38+00:00Added an answer on May 19, 2026 at 12:51 am

    To sort by more database fields you can add it to the end of the ORDER BY clause.

    $sql = "SELECT SUBSTRING(`last_name`, 1, 1) AS letter, `first_name`, `last_name`, `id` FROM `prospects` WHERE `user_id` = ".$uid." ORDER BY `last_name`, `field2`, `field3`";
    

    I read through your question again, if you want to print out the id field then you can add it to the select statement and then print it out

    // 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;
        // Set the id field as key
        ${$records['letter']}[$records['id']] = array($records['first_name'], $records['last_name']);
    }
    echo '<div id=""><p>You have ' . $prospect_count . ' Prospect' . ($prospect_count == 1 ? '' : 's') . ' | ' . '<a href="#" class="new">Create new</a></p></div>';
    // Create Alpha link Listing
    foreach(range('A','Z') as $i) {
        echo (array_key_exists ("$i", $alpha)) ? '<a href="#'.$i.'" title="'.$alpha["$i"].' results">'.$i.'</a>' : "$i";
        echo ($i != 'Z') ? ' | ':'';
    }
    
    // Create Data Listing
    foreach(range('A','Z') as $i) {
        if (array_key_exists ("$i", $alpha)) {
        echo '<div class="alpha"><span class="expand-alpha-up"></span><a href="#" name="'.$i.'">'.$i.'</a></div><div class="show">';
        foreach ($$i as $pid=>$name)
            echo '<a href="#" id="'.$pid.'"><p>'.$name[0].' '.$name[1].'</p></a>';
        }
        echo '</div>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys. I am trying to get the selected option in an each loop.
Hey guys, I've been bashing my head around for hours now, over this login
Hey guys, I'm using GWT for a data-driven web application, and I'm having issues
hey guys, i'm getting an exception on the following inner exception: {Value cannot be
Hey guys i wrote a quick test. I want delete to call deleteMe which
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey guys, I have a client who wants their front site to have a
Hey Guys I have the following simple Code : WhereAmIViewController.h #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.