I want to enable display or hide in my mysql table - mysql

This is my table. I need user to be able to be enable to either show or hide these attributes in their profile. For eg. a user might want to see city in profile but not the country. How do I do this without changing the structure of this table.

I'd say this is more of a front-end problem (rather than back-end)... One way you could perhaps solve this is to retrieve all data you need from the database for the selected user, and show a subset of the user data (that most users will likely want to see - name, age, favourite food etc)
Then you could use something like jQuery to power a 'Show more' toggle which will show/hide and give you the rest of the info.
SELECT a.*
, b.*
FROM contacts a -- assuming this is what your table is called
LEFT JOIN attributes b -- guessing that's the name of your example table
ON ( a.contact_id = b.contact_id )
WHERE a.contact_id = :contact_id -- bind variable
I would refrain from using the asterisk (*) in favour of just selecting the columns you need (you can write appropriate indexes and speed up the SQL this way).
If you've got a ton of data about a user (more than just basic stuff such as their name and some simple attributes) - then you might want to adopt a similar approach to above - but only retrieve the additional data you need if a user clicks 'Show more'.

Related

why some users do not appear in mdl_user_info_data in Moodle?

I am getting students added to course using a query but i noticed 2 of them do not appear( even if they are active and listed as participants) checking inside tables i noticed they do not appear in mdl_user_info_data. How can i prevent this? or whats the reason they dont were added to this table
This is my query:
SELECT u.id,u.username,u.firstname,u.lastname,u.email, b.data
FROM mdl_user u,mdl_role_assignments r, mdl_user_info_data b
WHERE u.id=r.userid
AND u.id=b.userid
AND r.roleid=5
AND r.contextid = 'somecontextId'
ORDER BY u.email ASC;
The table mdl_user_info_data holds the values for custom user profile fields (that are defined in mdl_user_info_field).
If a user's profile has not been edited since a particular custom user profile field has been created, then there will not be an associated mdl_user_info_data record for them.
Note that if there is more than one custom user field defined, there can be more than one mdl_user_info_data field - so your query could return more than one record per user.
You probably want to rewrite your query to LEFT JOIN with mdl_user_info_data. You probably also want to LEFT JOIN with mdl_user_info_field to identify which of the custom user profile fields it relates to.
Also note that your query makes a number of assumptions that may not always be true - if your query is running inside Moodle code, then you should use {user_info_data} instead of the 'mdl_' prefix, as that prefix can be changed. Hard-coding roleid 5 for 'student' can also fail on some sites (although it is usually the case).

Access query with join returns checkbox as null value

This is very strange even for my limited knowledge of Access. Background information: I am making a database which track the people I work with. The boss wants a report with the people leaving and the people replacing them, so I though this may work. However when I join the information, it results in the checkbox showing a NULL value. I made sure to set the default to NO and it is not set up for a triple state checkbox from what I can tell.
This is just a sample of what it would look like:
Table
I am using the fields IDReplicate and Replacement ID in order to JOIN the table and query. IDReplicate is just a copy of the Primary Key. Using the primary key as the first field in the JOIN produces a type mismatch
My query to pull information about the replacement people:
SELECT tblExample.IDReplicate, tblExample.Arrival, tblExample.City, tblExample.Package, tblExample.LName
FROM tblExample;
My query to which joins the people leaving and people arriving:
SELECT tblExample.LName, tblExample.Departure, tblExample.ReplacementID, qryReplacement.*
FROM tblExample LEFT JOIN qryReplacement ON tblExample.ReplacementID = qryReplacement.[IDReplicate];
Resultant Query Datasheet
It sounds like you may have a three-state checkbox where you want a two-state, check this SO post for details:
Determine whether a Access checkbox is checked or not
IMO, checkboxes are trouble and best avoided. My advice would be to change the field to a text field with a constraint to limit entry to yes or no, defaulting to no. That way you know exactly what data is really in your table, instead of counting on Access to translate an interface element into data. If you do it in the Access table design interface it would look something like this:

Database model for individual users seeing notice

I am looking for the best solution for the way the mySQL db should be set up for my app.
My app works like a noticeboard with two sections, "New Notices" and "Seen Notices".
Now when a user has viewed a notice, they click a button and it moves from New to Seen. But ONLY for this person.
Each person will have all of the notices viewable - but not necessarily in the same sections - as users will view them at different times and check them off as seen at different times.
My guess is having one table "Notices" for all notices, and a seperate table called "Seen" with the rows "UserID" and "noticeID". This means that for each notice it will need to consult the "Seen" table to find out if it should be shown or not. Is this ideal or is there another way?
Having a table with NoticeID and UserID is correct, I'd also add viewed date.
You can use 3 tables
Users
Notices
SeenNotices(maybe not the best name)
In the SeenNotices table have three columns UserID, NoticesID, HaveSeen. The have HaveSeen column will tell you if the user has seen it.
The way you are thinking should work, although over time you'll end up with a very big 'Seen' table, which is not scalable. An easy alternative is to use 'Unseen' table instead. This way the table gets smaller as people view the notice and you can also delete very old entries (old notices may no longer relevant so doesn't matter if they are not shown as Unseen to user).
Using the 'unseen' table your query will look like this:
SELECT n.notice_id, n.notice_msg, IF(u.user_id, 'new', 'seen') AS status
FROM notice n
LEFT JOIN unseen u ON (u.user_id = $user_id AND n.notice_id = u.notice_id)
WHERE user_id = $user_id;

MS Access - how to create a label on a form that populates data from another table

I have a fairly simple database that I inherited. For the purposes of this question, there are two tables:
Mastertable and Providertable.
Mastertable references Providertable thruogh provid, which is a FK to Providertable PK (provid).
So it looks like this:
Mastertable:
acct (PK)
(other fields)
provid (FK)
Providertable
provid (PK)
provname
provspecialty
Simple right? However, the Mastertable!provid field is actually a lookup table which displays Providertable!provname but stores provid. There is a form the users use to populate the Mastertable, and it has this lookup field shown.
The users now want to show the provider specialty based on what they select as the provid. I can't figure this out to save my life. I'm pretty well versed in SQL, having written many stored procedures and created a few db apps using .NET, but this is quite challenging. I tried creating a lookup field called provspeciality, but that's not what they want. I tried changing the "OnUpdate" event for the lookup field to point the Provider Specialty label to the right thing.
Right now, I can't even get a simple select going that joins the two tables since they are using this lookup field as the FK and Access I guess can't understand it. Any help appreciated.
Since the Mastertable provid field is a lookup type, the displayed value is the lookup value rather than the value which is actually stored in the field. This query will show you the stored provid values.
SELECT acct, provid
FROM Mastertable;
And I think you should be able to retrieve the matching provider specialties with a query similar to this:
SELECT m.provid, p.provname, p.provspecialty
FROM
Mastertable AS m
INNER JOIN Providertable AS p
ON p.provid = m.provid;
You may even be able to use that query as the Row Source for a combo or list box on your form. Make provid the bound column. You may wish to set the provid column width to 0" so it is not actually displayed in the control, but still stored in Mastertable.
I think you should modify the table to make provid a normal (numeric?) field instead of a lookup. Fortunately you indicated this is "fairly simple database", so that will hopefully limit the amount of additional changes you need to be compatible with the redesigned table. Good luck.
I created an "On Change" event for the form field "provid" which is a lookup field that displays the provider name while putting the provider id into the master table as a FK. However apparently Access is not able to do lookups based on this field (or I am doing something wrong) using queries - as shown above in comments. What I did is use this as the event code. Important, you must enable macros for this to work!
Private Sub provid_Change()
Me.txtProviderSpecialty = DLookup("Provspecialty", "Providertable", "provid = " & Me.provid.Value)
End Sub

MS Access. Editable Selection. Grouping of Items based on data other than primary key

I hope I can explain this clearly. I have a table, lets call it Widgets, that contains information (Color, Size, etc) about many many different widgets that are specified by an ID# which is the primary key. I also have a table, call it Tests, that is related to Widgets through a one to many relationship. Each row in this table represents a test on a particular widget and has information like WidgetID, Date, Info1, Info2, etc. Each WidgetID may have multiple tests in the table Tests. What I'm doing with these is simply displaying various reports that contain data from both tables based on various queryies. I do not have write privileges to either of these two tables.
Ok now that that is set up here is what I want to do. I'd like to set up a new table that contains a correlation between the Color of a widget and a new piece of data that I'll call Group. In short, I'd like to be able to define "Groups" of widgets where all the blue, green, and red widgets would be in one group and all of the yellow and orange widgets in another group, etc. (there is no overlap...ie each color correpsonds to only one group) I also want to set up forms that allow the user to add a Group and define which colors go in it, delete a Group, or edit a Group (truthfully add and delete would be enough).
What is the best way to do this? I'm not necessarily looking for code, but more so just direction. The best situation would be if each widget had a column in the Widgets table that contained the Groups data, but I do not have write access and neither will the users and I would like to make the Group data editable by the user.
It should be simple enough to set up a table:
Colour
Group
Users add data via a form by selecting the colour from the list and either entering a new or existing group. The whole thing should be possible without any coding, only wizards.
The new table is then joined to widgets on colour to create the various reports that you require.
EDIT re Comments
How you set up the table depends on the results you want returned, let us say that only the colours that are in groups are included, and we have this SQL to join to the widgets table:
SELECT WidgetID, GroupID FROM Widgets
INNER JOIN GroupColour
ON Widgets.Colour=GroupColour.Colour
Only the widgets that have a colour that is listed in the GroupColour table will be included in the listing, this is because of INNER JOIN. If the SQL was:
SELECT WidgetID, GroupID FROM Widgets
LEFT JOIN GroupColour
ON Widgets.Colour=GroupColour.Colour
You would get all widgets returned, but a Null for GroupID when there was no match in GroupColour. This can be quite useful.
You may wish to read http://www.devshed.com/c/a/MySQL/Understanding-SQL-Joins/, which is about MySQL, but works quite well for Jet SQL.