Getting Count and Order of Records in PHP

I have a page that pulls info from a database:

The current page orders all records by ARRIVAL. User clicks on PATNUM and it sends the PATNUM to a new page and creates a page that displays data for that specific PATNUM:
Support person

Everything is in one database.

User wants the case specific page to show a "position number" for that case based on SCHTM field. So let's say Surgeon A has 10 cases, Surgeon B has 8 cases, and Surgeon C has 4 cases.

When the case specific page is printed, the system would look for all records for that day that have the same surgeon, put them in order of SCHTM and tell me if that specific case is first, second, third, fourth, etc for that surgeon.

If a new row is added, it would update that position for the case.

What I'm looking for is how to do this - not necessarily the code for it. Just the steps to do it. I can do count for the surgeon to see his total number of cases for that day, but how do I get the number of the position for that specific case? Do I populate data to an array?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
You need a 'WHERE' that requires the PATNUM and Surgeon and ORDER BY SCHTM (which needs to be part of the result set for ORDER BY).  This is the general idea though since you have multiple tables, you'll have to add some JOINs to it.

SELECT PATNUM, Surgeon, SCHTM FROM Schedule WHERE PATNUM = '$patnum' AND Surgeon = '$surgeon' ORDER BY SCHTM

Open in new window

You can manipulate that to get the single item you want.
BHUCAuthor Commented:
Thanks Dave.. two things.  There aren't two tables - all information is in one table. Just pulling different columns out based on the page.

What you gave me isn't giving me the position of that case.  I have been doing more reading and wondering if I need Row_Number in my query.

Example data would be:

Jones           Pap               0800
Smith           Lee               0800
Foster         Pap                0900
Johnson      Lee                 1000
Olson           Wilson          0930

If I pulled the datasheet on Jones - he would be position 1 (first patient of the day for Pap)
If I pulled the datasheet on Johnson - he would be position 2 (second patient of the day for Lee)

Does that make sense?
Dave BaldwinFixer of ProblemsCommented:
I know it didn't give a single answer but it should give you the schedule for that Surgeon for the day in order of SCHTM.  At that point they could click on PATNUM and get the details.  I don't immediately have the exact answer you asked for.

Row_Number is for Microsoft SQL Server.  Is that what you are using?
SolarWinds® Network Configuration Manager (NCM)

SolarWinds® Network Configuration Manager brings structure and peace of mind to configuration management. Bulk config deployment, automatic backups, change detection, vulnerability assessments, and config change templates reduce the time needed for repetitive tasks.

BHUCAuthor Commented:
ROW_NUMBER is not working.. must be because it isn't a Microsoft server. I'm just trying anything at this point.
Dave BaldwinFixer of ProblemsCommented:
First try the query I posted.  You can use PHP to put a 'position' number on each line that is displayed which is one way to get that 'position' number.  Since there is nothing in the table to define the 'position', you will have to do something like that to get it anyway.

As I think about it, the 'position' could change during the day if someone schedules a new procedure in between two existing procedures.
BHUCAuthor Commented:
On the sheet the position is printing, I am not printing ALL the positions - just the one that corresponds to this case. And yes, it would need to update if more cases are added.
Dave BaldwinFixer of ProblemsCommented:
corresponds to this case
How are you selecting that?

The point is that you have to use a query like I posted to find out what the position / order is... especially since it could change at any time.  Maybe you could identify the patient and count in that query and only display the one you wanted.
SELECT @i=@i+1 as rownumber, PATNUM, Surgeon, SCHTM 
FROM Schedule,(SELECT @i:=0) AS foo 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BHUCAuthor Commented:
I wound up adding a new query inside the loop of the other and added the counter like you suggested. It only shows the number if the PATNUM matches the first loop. Thanks for the suggestion!
BHUCAuthor Commented:
Didn't use exact solution, but it spurred the idea of counting and only showing the one that matches.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.