Not sure my title is definitive enough, but here's my dilemma.
Firstly, I have two database tables.
The first one lists all specialitities like this:
ID, Description
The second table stores a practitioner ID with a Specialititiy ID from the above table.
So I have say table 1 with 100 specialitities listed in it.
And in table 2 I have say Practitioner ID 1 with say 5 of those specialitities.
Example:
Practitoner ID, Specialitiy ID
123,31
123,45
123,67
123,68
123,81
Now, I want to edit those through a form.
I can list all the specialities through a loop through an array of all the specialitities, including a checkbox adjacent to each speciality.
But how do I check the box matching those specialitities listed for the practitioner?
I have a PHP function that gets all the specialitities from the specialitities table and I then loop through that array to list them all, with a checkbox for all of them.
I also have a PHP function that gets all the specialitity ID's for a specific practitioner.
Is there a method of matching the ID of the speciality from the practitioners table with the speciality listed on the form, and checking the box?
I'm not sure I've made myself as clear as I should here, but it might make sense!
Related
I am uncertain of the best way to store multiple select options into mysql. Initially, I created a comma separated list of selected values. I have read that this is bad programming practice and also had a difficult time trying to figure out how to search the three columns in my db with the comma separated varchar value (eg: 1,5,17). It seems the best practice would be to create a 3rd table that would insert 3 records for the example above. This table would be linked via an id.
Example:
Table 1 (age levels)has fields id & age level (baby,teenager,adult)
Table 2 has classes id & class name
Table 3 is to store the multiple select options of WHO can attend each class. I guess the fields would be id & classID & ageLevelID
My questions are as follows:
1. how do I efficiently insert this data into table 2 and table 3?
2. how do I pull it all together to search for a person who is looking for a class that allows for just babies and teenagers, for example?
Relating 2 tables is easy, relating 3 tables is a conceptual struggle. Also, am I correct that it is asking for trouble to store comma delimited values in one field? Thank you.
Table agelevel (ageid (unique), agelevel)
Table classes (classid (unique), classname)
Table classesinfo (classid, fieldname,fieldvalue)
With that you could have any 'fieldname' such as agelevel, detailed description, hours, etc and then the value of that field in fieldvalue. This allows your classes to any an infinite number variables attached to a single classid.
I have 3 table which have relation with each others... In 1st table, i have student details contains studentid (PK), studentname, dob, etc...
Table 2 contain course consist of coursecodeid(PK), studentid(FK), coursename, teachername and etc..
Table 3 contain grade consist of studentid(fk), courseid(fk), id(pk_autoincrement) and grade.
When system starts, studentid will be load into a dropmenu. Onchange this dropmenu, second dropmenu will be shown and load with courseid take by selected student (based on studentid)
When user select courseid onchange a label will be visible to show student grade.
Everything done in 1 page... Anybody can give me sample code either in php or javascript since i am using php and mysql database.
Ajax. That is the way to go. Check here for ideas, this is doing something similar to what you are trying to achieve. Check this for more abstract code reference. This is a possible duplicate. And generally, searching for loading combobox based on other combobox jquery will give you pretty decent results.
Im trying to create a checklist system eg. a list of items to collect. the user will be able to add the list to their profile and then as they collect the items then can check the box for an item in the list click submit and the checked item will now be marked as collected. I have it coded and working fine but it makes an insane amount of queries to the database to work.
i have 5 tables. a users table (for the registered users username, id..etc), a lists table (containing the list name, description, id ), a list items table (containing the individual items. id, title and listID (to reference the list it belongs to)). userslist table (for the lists the user has added to their profile. userID and listID) and collecteditems table (this has the list items that a person has checked the box for as collected. listItemsID, UserID)
the problem is when i view the mylists.php page it will query the userslist table and return all the ID's for the lists the user has added. then once i have the ID for the list it then queries the list table to find out what the name of the list is (this could mean having to make 10 queries to the list table if i have 10 different lists added). if i added a listname column to the userslist table i would only need to make 1 query for the page and that is to the userslists table and i could construct the page with that 1 query.
First, I wouldn't worry about queries on primary keys. All your tables have a primary key referenced by other tables. These will use joins.
Second, you don't have to get the list names separately. Use a query such as:
select l.listName
from UserLists ul join
Lists l
on ul.listId = l.listId
where userId = $userid
This will return all the names in a single query.
It sounds like you should keep your data normalized (that is, avoid the redundant data) and instead gather all the data you want in a single query, by using a JOIN.
I have a MySQL database with a growing number of users and each user has a list of items they want and of items they have - and each user has a specific ID
The current database was created some time ago and it currently has each users with a specific row in a WANT or HAVE table with 50 columns per row with the user id as the primary key and each item WANT or HAVE has a specific id number.
this currently limits the addition of 50 items per user and greatly complicates searches and other functions with the databases
When redoing the database - would it be viable to instead simply create a 2 column WANT and HAVE table with each row having the user ID and the Item ID. That way there is no 'theoretical' limit to items per user.
Each time a member loads the profile page - a list of their want and have items will then be compiled using a simple SELECT WHERE ID = ##### statement from the have or want table
Furthermore i would need to make comparisons of user to user item lists, most common items, user with most items, complete user searches for items that one user wants and the other user has... - blah blah
The amount of users will range from 5000 - 20000
and each user averages about 15 - 20 items
will this be a viable MySQL structure or do i have to rethink my strategy?
Thanks alot for your help!
This will certainly be a viable structure in mysql. It can handle very large amounts of data. When you build it though, make sure that you put proper indexes on the user/item IDs so that the queries will return nice and quick.
This is called a one to many relationship in database terms.
Table1 holds:
userName | ID
Table2 holds:
userID | ItemID
You simply put as many rows into the second table as you want.
In your case, I would probably structure the tables as this:
users
id | userName | otherFieldsAsNeeded
items
userID | itemID | needWantID
This way, you can either have a simple lookup for needWantID - for example 1 for Need, 2 for Want. But later down the track, you can add 3 for wishlist for example.
Edit: just make sure that you aren't storing your item information in table items just store the user relationship to the item. Have all the item information in a table (itemDetails for example) which holds your descriptions, prices and whatever else you want.
I would recommend 2 tables, a Wants table and a Have table. Each table would have a user_id and product_id. I think this is the most normalized and gives you "unlimited" items per user.
Or, you could have one table with a user_id, product_id, and type ('WANT' or 'HAVE'). I would probably go with option 1.
As you mentioned in your question, yes, it would make much more sense to have a separate tables for WANTs and HAVEs. These tables could have an Id column which would relate the row to the user, and a column that actually dictates what the WANT or HAVE item is. This method would allow for much more room to expand.
It should be noted that if you have a lot of of these rows, you may need to increase the capacity of your server in order to maintain quick queries. If you have millions of rows, they will have a great deal of strain on the server (depending on your setup).
What you're theorizing is a very legitimate database structure. For a many to many relationship (which is what you want), the only way I've seen this done is to, like you say, have a relationships table with user_id and item_it as the columns. You could expand on it, but that's the basic idea.
This design is much more flexible and allows for the infinite items per user that you want.
In order to handle wants and have, you could create two tables or you could just use one and have a third column which would hold just one byte, indicating whether the user/item match is a want or a need. Depending on the specifics of your projects, either would be a viable option.
So, what you would end up with is at least the following tables:
Table: users
Cols:
user_id
any other user info
Table: relationships
Cols:
user_id
item_id
type (1 byte/boolean)
Table: items
Cols:
item_id
any other item info
Hope that helps!
Currently I have a form that submits an image with textfields such as
title, description and another field that autoincrements for imageID, another
area for the actual file , called vfile, and *** another part that has
3 checkboxes and a text field.
Everything works fine, and this is what it does. Submits the data to a database so that it can pull the information to a page on the website.
The only part I am trying to update is:
The 3 checkboxes and the textfield.
Lets say the first checkbox reads: Apples
The second : Oranges
The Third: Grapes
And in the other category is a blank textfield that if you add something, it would add it to a category called "Other".
So the database design has 4 fields: 1 - apples, 2 - oranges, 3 - grapes, 4 - other.
When I click a checkbox, it would add checked to the database under the correct one, either apples, oranges, or grapes.
If I add a field to the textbox such as: Bannanas, then it would add "Bannanas" to the database field vother and show that in the database.
This is all fine, but what if the next picture has all 4 items, plus another one? Such as if the next picture had Apples, Oranges, Grapes, Bannanas, and Plums?
How could I have the "Bannanas" other category, change into a checkbox category that could be chosen for the next pics when I go to the add images page next time.
So that when I go to the second picture to submit, it would give me the option of not just 3 checkboxes, but 4 checkboxes now, that I could check the first 4, "Apples, Oranges, Grapes, Bannanas" and then put Plums in the other category.
Basically upon submit it takes what is in the other feild and addes a new category to the database, which is then displayed in the array of checkbox choices and it is removed from the Other Category now, for it is a checkbox. (thus it would not want the value left in the old field, for it would keep creating the same category over and rewriting the old data possibly.
Anyway, any suggestions?
Thanks in advance.
(It sounds like this is more of a database design question and not a php question, but I may be misunderstanding what it is you are looking for advice on)
It sounds like you are saying that these attributes (Apples, Orange, etc) are stored as columns in your main table; but the situation you are describing sounds more like Tagging. Typically you would maintain a list of things that get tagged (your images), and a separate list of all possible tags (Which would be a table containing the rows : Apple, Orange, Grape). Your UI has the option to select from pre-existing tags (rows in the tag table) or add a new tag using the "Other" box. New tags would be added as a new row to the tag table. Since tags and tagged items have a many-to-many relationship you would create a third table (called a join table) that stores keys of tagged items and keys of tags; that way you can select either side of the relationship easily : get all the tags for a given item; get all the items with a given tag.
Does that help?
(EDIT : for comments)
So, Activities sounds like the list of Tags. If I want to show a form with checkboxes for all the Activities I can query the activities table for them. Each of those checkboxes can have a name attribute or something that captures the ID of the row that its bound to.
Also I would select from the join table the ids of the tags that my currently viewed image has selected. As I am populating the checkbox list I can check this result set to see if the id of the checkbox I'm putting on the page is in the list of tags for the image.
To store this back to the db on submit, the easiest thing is probably to (in a transaction) delete all the entries for the image from the join table and replace them with new entries based on the state of the check boxes in the form.
Drop the apples, oranges and grapes columns.
Create a second table with two fields: imageID and itemtype.
Don't make any of the two a key. Now you can list as many different types of items for each image as you need. It will be comparatively expensive to get the list of item types in use from the database but unless you have millions of items this shouldn't be a problem. (Mikeb is suggesting to keep the list of item types in use in a separate table to speed this up.)
The alternative is to dynamically add a column to the first table for each item type you encounter. This will create a very sparse table. I wouldn't do this.
You need to change your database schema to support an infinite number of attributes.
You should move your list of attributes from a set of fields in the record to a list of related records in a new table. This new table requires 2 columns. The first is the primary key from the existing table so you can establish the relationship between the tables. The second is the attribute field ('Bananas' or 'Apples' or 'Plums', etc.) You can have as many records in the attributes table as you like for each record in your main table. You could also have no attribute records if none are checked.
This kind of relationship between two tables is called a one-to-many relationship.