Hey Everyone,
I've been pulling my hair out on this one. I'm creating an online contact spreadsheet which allows users to view contact info (email, first name, last name, state, status, etc) as well as sort it by Name, state, etc (much like Microsoft Excel). I have to use this database structure (see below) and I'm having trouble with multiple sorting for the database. Here's the DB set up:
TABLE1
UserID, Email
TABLE 2
UserID, UserFields, UserValue
(UserFields = things like First Name, Last Name, State, etc....UserValue is the value of those fields)
EXAMPLE:
TABLE 1
1 user1@email.com
2 user2@email.com
3 user3@email.com
TABLE 2
1 - First Name - Joe
1 - Last Name - Smith
1 - Address - 555 Main St
1 - State - Alaska
2 - First Name - Bob
2 - Last Name - Harper
2 - Address - 255 Poplar Ave
2 - State - Pennsylvania
3 - First Name - Eric
3 - Last Name - Klausen
3 - Address - 111 Chestnut Ave
3 - State - Alaska
I'm going to first parse all the fields by USERID and initially sort by First Name:
<?php
$getmember = mysql_query("SELECT UserID, UserEmail, UserField, UserValues FROM Table1, Table2 WHERE table1.UserID = table2.UserID AND UserField='Last Name' ORDER BY UserValue ASC");
while ($row=mysql_fetch_array($getmember)) {
/ Now I grab all the UserFields & UserValues associated with the UserID and parse them in an associative array/
$getfields = mysql_query("SELECT UserField, UserValues FROM Table2 Where UserID = $row[UserID]");
while ($val = mysql_fetch_array($getfields)) {
$details[$val[UserField]] = $val[UserValue];
}
/ Now from here I can display the row for each user and it will be sorted by First Name already...example:
$display = "$details[First Name] $details[Last Name]<br>$details[City]
$details[State]...etc";/
} //end of initial while loop
Now here's where I'm stuck. I have all the values for each User sorted by First Name and I've retrieved all the FieldValues for each User....how do I now go back and sort not just by First Name but by (let's say) STATE and display that. I don't know the proper way of doing it because the FieldValues and FieldID's are on seperate rows in Table2.
Any help would be appreciated and thanks
mmpromo