close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Filter data in select to omit records related to logged in user.

Thread began 3/01/2011 9:57 am by troyd | Last modified 3/21/2011 7:45 am by Jason Byrnes | 3339 views | 16 replies |

troyd

Filter data in select to omit records related to logged in user.

I wasn't sure where to post this. I can add more details but figured I would start with the basics in hopes it was just something I wasn't grasping but might be an easy fix.

I have a page that people will log in to and then see a list of event dates. They will also see a form with 2 selects. A drop down select with the event date and a second with a number of tables to choose. This form inserts a record into a relational table. All of this is working.

Everything working but the date drop down. See, it's still showing a date that is already in the user's reserved list.



Here are two of my tables. (there's a third for users info.)

Parent event table



Child user to event table



Here's what I need. Since the dropdown is dynamically populated by a recordset, it needs to show only dates that the user has not already registered for. Basically the opposite of the other list that displays the events they DID register for. I need this list (drop down in form) to show the events they did not register for.

My attempts so far have given me partial results.

Here is my SQL for the list that shows their registrations.
SELECT users.UserID, date_format(show_date, '%M %e, %Y') as show_date, UserFirstName, UserLastName, reserved
FROM tblbooths LEFT JOIN tblreserved ON tblbooths.booth_id = tblreserved.booth_id
LEFT JOIN users ON users.UserID = tblreserved.UserID
WHERE tblreserved.UserID = ParamUserID
GROUP BY show_date

(with)

ParamUserID
integer
-1
$_SESSION['UserID']


How would I create the opposite that would show all event dates that the user has not already registered for? Seems simple I know. But using WHERE UserID != ParamUserID won't do it because there are records that do not have the user's id in it, but still have the date they already registered for. So I tried HAVING so it would do it after the grouping of dates. That partially works, but still leaves a couple dates in there for some reason.

SQL for the select that I have tried, but doesn't work.
SELECT capacity, date_format(show_date, '%M %e, %Y') as show_date, tblbooths.booth_id, COALESCE(sum(reserved), 0) AS total, UserID, capacity - COALESCE(sum(reserved), 0) AS available
FROM tblbooths LEFT JOIN tblreserved ON tblbooths.booth_id = tblreserved.booth_id
WHERE show_date >= CURDATE()
GROUP BY show_date
HAVING UserID != ParamUserID


Ideally I would like just one recordset that worked for both areas. But that's not a big deal. On that note. Is there a way to bind the first recordset to both areas in php and maybe an if statement? I would alter it a little. Just a thought.

Thanks,
TroyD

Sign in to reply to this post

Jason ByrnesWebAssist

did you try:
WHERE tblreserved.UserID != ParamUserID

If that doesn't work, I will need a sql dump og the tables to work directly with the data.

Sign in to reply to this post

troyd

That didn't work.

What kind of sql dump where you referring to? Sorry, hadn't done that one yet.

Thanks
TroyD

Sign in to reply to this post

troyd

Sorry Jason. I no more than submitted that last reply and it dawned on me what you were needing. Hopefully this is right.

Thanks,
TroyD

Attached Files
WA_mwdblogin.sql.zip
Sign in to reply to this post

Jason ByrnesWebAssist

i must be missing something.

lets assume where searching using UserID 5.

If i run the query:

SELECT users.UserID, date_format(show_date, '%M %e, %Y') as show_date, UserFirstName, UserLastName, reserved
FROM tblbooths inner JOIN tblreserved ON tblbooths.booth_id = tblreserved.booth_id
inner JOIN users ON users.UserID = tblreserved.UserID
WHERE tblreserved.UserID != 5
GROUP BY show_date




I get the results shown in the first screen shot. this seams correct to me based on your original description. Can you explain what you you are expecting to see differently? this will help me understand what you are after.

Sign in to reply to this post

troyd

Sure, sorry if I wasn't explaining it right.

You see how the dropdown on my screenshot shows June 26?
The user as you can see in the bottom list already registered. So that June 26 date should not be there as a choice now.

What you can't see in the dropdown (because it's closed in the shot), is that August is also available. That one would be correct and is the only date that should be available to the user.

I looked at your screenshot. It shows a return of May1, June 26 and August 28.
The user as mentioned above should only be seeing August 28.

The query is doing what it's being told and filtering out the UserID = 5. But since userid 6 has registered for dates = to dates that userid 5 has, I need those gone too because I don't want userid 5 to re register for the same dates.

Sometimes things sound correct in my head but I am saying the wrong. I can try to explain this a different way if needed. Sorry for the rambling.

Basically what I need is a filter that says, remove all UserID = 5 AND also remove all other records which have a date = to any of the records with the UserID of 5. :)

Thanks,
TroyD

Sign in to reply to this post

Jason ByrnesWebAssist

OK, now it makes sense.

This should do the trick:

SELECT users.UserID, show_date, UserFirstName, UserLastName, reserved
FROM tblbooths inner JOIN tblreserved ON tblbooths.booth_id = tblreserved.booth_id
inner JOIN users ON users.UserID = tblreserved.UserID
WHERE tblreserved.UserID != 5 AND tblreserved.booth_id NOT IN (SELECT booth_id FROM tblreserved WHERE UserID = 5)
Sign in to reply to this post

troyd

Jason, your a genius!

Thanks so much. Works perfectly. And I can see by the way you wrote the sql, I was way off. But that's how I learn. Trust me, I will be dissecting this and learning from it.

I added in the ParamUserID and tested it on all of my fake user accounts. And as you would expect, it works as it should. This makes my day.

Again. Thanks,
TroyD

Sign in to reply to this post

Jason ByrnesWebAssist

glad to hear it is working, I just needed a good explanation of why the results weren't correct to find where the problem was.

Sign in to reply to this post

troyd

Jason,

I overlooked one detail. It still works, but only if there are several records in the tblreserved already. For testing (and in the sql dumb I sent you), I just happened to have each of the event dates reserved by someone. SO, if this is a fresh database and nobody has registered, or if some dates have not been registered, they are not included as a choice for anyone to register.

I tried for several hours to alter your sql before bugging you about it again. No luck.
My thoughts were that maybe I needed to include an "if" in the sql. So, if the UserID = ParamUserID or if the UserID != ParamUserID, then do all that. Otherwise include all the dates in the tblbooths table. But I couldn't get it to work.

Also, just a small added question. In your opinion, should my tblreserved table have a unique table id? Right now I have both booth_id and UserID as primary/unique together. But adding a WA delete or update behavior might not work.?

Thanks,
TroyD

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...