Re: [PHP] Sorting table columns dynamically on normalized MySQL tables [LONG]

2005-04-20 Thread Leif Gregory
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))
{
  

Re: [PHP] Sorting table columns dynamically on normalized MySQL tables [LONG]

2005-04-20 Thread Chris Boget
 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:

I'm curious why you aren't joining the other tables?  That will
solve all your woes...

thnx,
Chris

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Sorting table columns dynamically on normalized MySQL tables [LONG]

2005-04-20 Thread Jay Blanchard
[snip]
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
[/snip]

If you had a JOIN to the location table you could order by the actual
location. Here is an example...

SELECT c.courseName, l.locationName
FROM Course c, Location l
WHERE c.locationID = l.locationID
ORDER BY l.locationName

[snip]
Sorry that this has degenerated into a MySQL question rather than PHP.
I was originally looking for how people handled it in PHP.
[/snip]

Actually it didn't degenerate, it was a SQL question all along. Unless
you had a desire to build sortable arrays in PHP this type of sort
should always be done on the SQL side because it would be much more
efficient.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Sorting table columns dynamically on normalized MySQL tables [LONG]

2005-04-20 Thread Jay Blanchard
[snip]
Sorry that this has degenerated into a MySQL question rather than PHP.
I was originally looking for how people handled it in PHP.
---
Actually it didn't degenerate, it was a SQL question all along. Unless
you had a desire to build sortable arrays in PHP this type of sort
should always be done on the SQL side because it would be much more
efficient.
[/snip]

I also should have said that it the joins that allow you to maintain
normalized tables, for without joins you would be stuck with doing the
magic in the programming language. Since most DB engines are optimized
for handling joins leave that efficiency on that side of the equation.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php