close ad
Databridge V2 with MySQLi support IS Now Available!
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

One to many query

Thread began 10/31/2010 9:20 am by twitchr377598 | Last modified 11/03/2010 9:52 am by Jason Byrnes | 1551 views | 11 replies |

twitchr377598

One to many query

Well my friends, I have been pulling my hair out for over a day and I finally had to admit I needed help. I haven't had much experience with one to many relationships and this particular query is driving me batty and I don't think any of my Webassist extensions can help me with this one...haha

Ok, I am generating some checkboxes via a table like so:

php:
<?php do { ?>

              <input <?php if (!(strcmp($row_venues_RS['userID'],116))) {echo "checked=\"checked\"";} ?> name="venueID_<?php echo $row_venues_RS['venueID']; ?>" id="venueID_<?php echo $row_venues_RS['venueID']; ?>" type="checkbox" value="<?php echo $row_venues_RS['venueID']; ?>" />
              <?php echo $row_venues_RS['venueName']." ".$row_venues_RS['venueID']; ?><br />
     
<?php } while ($row_venues_RS mysql_fetch_assoc($venues_RS)); ?>



This query almost works. The proper venue (or venues) is checked, but as you can guess I get duplicates:

php:
SELECT venues.venueID, venues.venueName, venue_user_access.userID FROM

  venues INNER JOIN venue_user_access USING (venueID) WHERE venues.venueMasterID = %s AND venueStatus = 1 ORDER BY venueName ASC



These checkboxes are on an update record page so my goal is to show all venues tied to the venueMasterID (the value is stored in a sessions variable) from the venues table and check the ones that userID 116 has access. The access table essentially has a venueID and userID field. Is this possible with a single query? I haven't written a lot of complex ones therefore I may be missing some function to make this easier.

I have read up on and tried DISTINCT and GROUP BY, but never got the results I desired. So anyway, I am posting on the chance someone has an idea.

Thanks in advance,
Twitch

Sign in to reply to this post

Jason ByrnesWebAssist

you need to add a GROUP BY clause:

SELECT venues.venueID, venues.venueName, venue_user_access.userID FROM
venues INNER JOIN venue_user_access USING (venueID) WHERE venues.venueMasterID = %s AND venueStatus = 1 ORDER BY venueName ASC GROUP BY venues.venueID
Sign in to reply to this post

twitchr377598

Jason,

Can't tell you how much I appreciate the reply. Pulling my hair out for a couple of days. I had tried GROUP BY before and got an error. When I copied and pasted your query I got a similar error:

php:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY venues.venueID' at line 2



This happens whenever I try to add GROUP BY. Any ideas?

Thanks again

Sign in to reply to this post

Jason ByrnesWebAssist

did you try moving the group by before the order by:

GROUP BY venues.venueID ORDER BY venueName ASC
Sign in to reply to this post

twitchr377598

Again, thanks for the quick response. I think putting it before the order by was the only place I hadn't tried. Probably just missed it in my frustration...haha The error is gone, but now I need to figure out how to check any venue that userID 116 has access to. That only works when all the duplicates are showing up. I know why, but not sure how to fix it.

Again, thanks for the much needed help. Purchasing webassist extensions has been the best thing I've done since starting web design.

Sign in to reply to this post

Jason ByrnesWebAssist

looking at the code, the userID is being used to control the venue:
<input <?php if (!(strcmp($row_venues_RS['userID'],116))) {echo "checked=\"checked\"";} ?>

not the venue iD, it seams to me that the venue ID should be used here, not the users ID.

Sign in to reply to this post

twitchr377598

I'm sure my explanations are confusing...maybe a look at my structure will help. Maybe what I'm asking can't be done or my db structure is incorrect.

venue_user_access table:

userID | venueID
116 | 65
116 | 66
114 | 65
etc | etc

venues table:

venueID | venueMasterID | venueName | other columns with venue info
65 | 113 | My Club | blah blah blah etc
66 | 113 | Club 2 | blah blah


The page I'm doing this query on is a page to update the user's (in my test case userID 116) access to clubs owned by venueMasterID 113. On the insert page I used one of the extensions (can't remember which one) to repeat and insert some checkboxes that contained the clubs owned by venueMasterID 113. Now I would like to generate the checkboxes on the update user page based on clubs owned by venueMasterID 113 and have the ones that userID 116 has access to show up as checked and the other ones unchecked.

Seems like a simple doable thing, but it's been a hair puller haha.

Again, the help is certainly appreciated.

-Twitch

Sign in to reply to this post

Jason ByrnesWebAssist

my point is that the venue id is being used for the value:
value="<?php echo $row_venues_RS['venueID']; ?>


so it should also be used as the control for whether the checkbox is checked or not.

Sign in to reply to this post

twitchr377598

The reason I use userID to check or uncheck is because I only want the ones that userID 116 has access to to be checked when rendered. It works if I simply let it show the duplicate venues with a simple INNER JOIN query because it's pulling all the venues that 113 owns and checking the ones that 116 has access to. The problem is the duplicates showing.

For instance, instead of seeing a list of checkboxes with Club 1 and Club 3 checked:

[x] Club 1
[ ] Club 2
[x] Club 3

where userID 116 has access to club 1 and 3 I get:

[ ] Club 1
[ ] Club 1
[x] Club 1
[ ] Club 2
[ ] Club 2
[ ] Club 2
[ ] Club 3
[ ] Club 3
[x] Club 3

above is basically showing all the records (and users) that have access to clubs that are owned by 113 and checking the proper Clubs that userID has access to. My goal is the weed out the duplicates. I change the query to do that, but then it doesn't check them.

Maybe I just need to figure out a different way to structure my one to many relationship, but everything I've read on it seems to point to this way. I think the key is the proper query.

Sorry if I am confusing you Jason...haha

Sign in to reply to this post

Jason ByrnesWebAssist

yes, the key is the proper query.


you need one query to return all venues, and one query to return only the venues associated to the user, then use the venue id from the recordset that is being filtered to drive which checkboxes are selected.

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...