Hello Jay,
Tuesday, April 19, 2005, 1:28:25 PM, you wrote:
J Normalization has no effect on sort order where any DB is concerned.
J Sort order is determined in the sort clause in the query. So if you want
J to sort by location and you are using MySQL your ORDER BY clause should
J be location(regardless of joins)
J SELECT foo
J FROM bar
J ORDER BY location
J returns
J course instructor location
J Science Bill Elementary School
J Basket Weaving Dave High School
J Math Dave Middle School
J Biology Jessica Middle School
Maybe I wasn't clear.
SELECT *
FROM Course
ORDER BY locationID ASC
Now the ORDER BY can be any one of five things based on the variable
passed by the hyperlink for the column they clicked on (location,
course, date, category, and instructor
Because locationID is an integer which directly relates to the
Instructors table id field (also an integer), there is no way in heck
that it can sort that any other way than numerically. There is no join
between the Course, Locations, and Instructors tables. All the Course
table knows is that there is an integer in the instructorID,
categoryID, and locationID field. Therefore it gets sorted
numerically, not alphabetically.
As I said, I was trying to keep the example simple. I actually have
the following tables in the DB.
Courses
Instructors
Locations
Categories
Students
Registrations
Supervisors
Courses contains information pertinent to a course.
- id
- name
- courseDate
- courseTime
- ampm (morning or afternoon)
- capacity (number of seats available)
- locationID (corresponding id from the Locations table)
- instructorID (corresponding id from the Instructors table)
- seatsLeft (counter for the number of available seats)
- description (what the course is about)
- categoryID (corresponding id from the Categories table)
Instructors
- id
- firstName
- lastName
- email
- phone
- image
- about
Locations
- id
- name
- seatingCapacity (How many true seats there are in the room)
- address
- directions
Categories
- id
- name
Students
- id
- firstName
- lastName
- email
- phone
- password
- username
- divisionOrFacility
- programArea
- supervisorID
Supervisors
- id
- firstName
- lastName
- email
- phone
Registrations
- id
- courseID
- studentID
- attended
So... For me to list the upcoming courses on the main page, I have the
following column in the table:
Course Name - From Course Table
Open Seats- From Course Table
Category - From Course Table (match id in Categories table)
Date - From Course Table
Time - From Course Table
Location - From Course Table (match id in Locations table)
Instructor- From Course Table (match id in Instructors table)
Category, Location, and Instructor are integers. What I did to get the
names respective to the integer value is that beforehand I built an
array for those three tables then I match the id from the Course Table
for each of the three with their respective array, and then display
the name.
Since the SELECT statement is based on the Course table, and the
Course table contains only integers, it's sorting by the integer value
of the categoryID, instructorID, or locationID in the Course table.
The actual SELECT statement is as follows:
$sqlCourses=SELECT * FROM . $tbl_courses . WHERE courseDate ' .
date(Y-m-d) . ' ORDER BY . $orderBy . ASC;
the $orderBy variable is set via $_GET['orderBy'] which is sent by the
table headers as below:
$tblHeaders = 'tha href=' . $_SERVER['PHP_SELF'] .
'?orderBy=nameCourse/a/ththOpen Seats/ththa href=' .
$_SERVER['PHP_SELF'] . '?orderBy=categoryIdCategory/a/ththa href=' .
$_SERVER['PHP_SELF'] . '?orderBy=courseDateDate/a/ththTime/ththa
href=' . $_SERVER['PHP_SELF'] . '?orderBy=roomIdLocation/a/ththa
href=' . $_SERVER['PHP_SELF'] . '?orderBy=instructorIdInstructor/a/th';
The sorting works just fine, but it's numerical ASC based off the
integer value (instructorID, categoryID, or locationID). It is *not*
alphabetical, which is what the customer wants.
If they sort by Location, the location column should be alphabetical
ASC, if by course name, then alphabetical ASC by the course name.
Is that a bit clearer now?
Sorry that this has degenerated into a MySQL question rather than PHP.
I was originally looking for how people handled it in PHP.
BTW, just for clarity sake on the arrays used to build the lookups to
display the list of courses, I'm doing an include with the following:
?php
//Get instructors --
$sqlInstructors=SELECT * FROM . $tbl_instructors;
$resultInstructors=mysql_query($sqlInstructors) or die('Failed to get
instructors. Please contact ' . $maintainer);
while ($rInstructors=mysql_fetch_assoc($resultInstructors))
{