help with php script for sorting list in alpha order and using 3 db fields
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 tryi开发者_如何学JAVAng 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.
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>';
}
精彩评论