php/mysql table with users settings - php

So I created this application, that displays widgets. Each widget is a *.php file that displays different type of data from the mysql database. Also I created a file options.php, that each user can access and they can change the position where each widget is displayed, the height, the color, and the width. (lets say the widget is a div with info pulled out for the database)
The issue I am having is how to store the data to be specific to the user. At this point after the button save is pushed it UPDATES all the rows while each row contains a specific widget options.
For example:
Options table has these fields: [id] [name] [height] [position] [color]. For now all users see the same placement because it is not user specific. I was thinking about storing all the widget names in 1 row with a separator, then all positions with a separator ...but I'm not sure how to do it.

I recommend you to store every field in a different column, is more clear and elegant, use separator in a column to store different values seems a bit nasty.
You can create a table widget configuration where you have different columns for the required parameters :)

If every widget has a different amount of settings ( therefore different amount of columns ) you could make one field blob and pass serialized array there. Other option is to find common fields for every widget and serialize only what is left in separate column.
To make widget settings
specific to the user
just link corrent widget settings row to the user id. Better in same if every user will have personal row with settings.
All of the following is of course if I understood your question correctly.

You want one table for position options:
Divtable:
id - INT (You want this to be Auto Incrementing Primary Key)
Name - VARCHAR
userid - INT
Top - INT
Left - INT
Width - INT
Height - INT
the ID is the primary key, and really should be the first thing
in damn near every MySQL Table...
The name is the name of the Div.
UserId is a foreign key in your table that points to the ID number on the "Users" table.
Then you know which user owns that DIV, you can select all of HIS divs to redraw later easily, etc, etc...
you can get the user id number from the Users table by username (or whatever)...
select ID from `Users` where username='johndoe121'
Lets say it returns 21. Find all of /his/ DIV options now like so:
select * from `options` where userid = '21'

Related

Database desing Mysql

currently I found myself wondering if this is the right thing to do in this case.
You see I have a database called for example Warehouse
I this database I have two tables:
[the table items is for saving the items of the system add-modify-delete records]
Items (which its columns are )
TAG_ID
P_NUMBER
QUANTITY
TYPE
LOCATION
DESCRIPTION
REASON
USERID
DATE
[the table lastchanges is for saving the items of the system that has been changed]
lastchanges (which its columns are )
ID
TAGID
PNUMBER
USERID
ACTION
DATING
Now I have been asked to add "exactly what has been changed" to the current form, for example if the quantity changed I have to show that before and after in a bootstrap form.
My brain told me to just add all the columns of the table items into lastchanges and save on those columns the data before changing and into items the new modified data, but performance-wise I see this as a bad action and I want your opinion.
If I understand you correctly you need a history of your DB changes.
If thats the point I would recommend you to create a new row for each entry and soft delete the old one. Then nothing gets lost and you can always get differences or older values.
Adding a the field deleted_at, and created_at as dates would do that trick for you. If deleted_at is null its the current entry, if there is a date set you know exactly when it got "overwritten"

Dynamically add custom input fields and save them to database

I want to develop an web-service where a user can add tickets. Every ticket has got an input text field title and a textarea description. If the user klick the save button, the data will be saved in a mysql database.
The admin has got an admin panel. He can add or remove input fields in this admin panel to change the add ticket view form the user.
For example: The admin adds a select field category. You can select category A, B or C. If you select category A, there will be a new input text field called animal. If you select category B, nothing happens. If you select category C, there will be 2 new fields: A text field and a number field. The number field is required. And so on, and so on. After a week, the admin could remove some fields, or add a category or... To conclude, the admin can add and remove select, text, number, password,... fields to the add ticket section with relationships, length and requirements.
I dont know how to structure the database and to save the data. I think about something like a mysql table tickets with title, desc, and data and put an XML / JSON String to the data field and another table ticketFields with name (category, animal,...), type (text, number,...), required (yes/no), length (int), data (to store data for select fields). The problem is, that the relationships are missing in this model. So how can I save this data efficient?
The relational model might look like this:
Ticket:
id PK
Answer:
ticketId FK PK
fieldId FK PK
value
Field:
id PK,
name,
parentFieldId NULL FK,
parentFieldValue NULL,
type,
required,
min NULL,
max NULL,
range NULL
(...other "constraint" fields, checked regarding choosen type)
At first, fields with parentFieldId of NULL are displayed. Fields having parentFieldId set are shown only if the answer for parent field is given. Fields having parentFieldId and parentFieldValue set are displayed if the answer for parent field is given and it equals parentFieldValue.
Given answer is checked regarding to field's type (e.g. if type is "number" then the answer must be a number between min and max).
You can create another table with id, category and field for the admin to use and relate them both using a common id field, preferably primary key. Now, this table can be queried for the reference of new categories inserted against a particular id in your main table. Use javascript/jquery to dynamically create html code for the new fields and show them on your page.
Suppose your the user selects Option 'A', then the new table can be queried to see if there are any fields set by the admin against the option 'A'. If yes, retrive those fields and show them to the user.

PHP: Categorizing images into database

I'm building a website in PHP to share my comics. I'd like to implement categorization to allow people to filter which comics they'd like to see.
I've asked this question before, but at that time my site's architecture was not using a database.I've since implemented a database (which is amazing, btw) so I need to change things up.
I'm thinking the way to do this is:
1) Make 2 tables: 1 for categories, 1 for images
2) Insert images into their respective tables based on which filesystem folder they're in and assign that table id
3) Insert all images into All_Images table with their newly assigned category id
4) Take in user input to decide which images to show. So, if user input = cat 1, then show images with category 1 id.
So, basically I need a way to initially assign categories to the images when they come in from the filesystem. Is there an easier way to do this? Do I have to create multiple tables?
Any thoughts on this?
Thanks!!
The normal way would be to have an images table (presumably with filenames rather than the actual images?) and then a one-to-many relationship to categories so that each comic can have more than one:
Table:Image
-----------
rowid: integer identity
displayname: varchar
filename: varchar
Table:Category
--------------
rowid: integer identity
displayname: varchar
Table:ImageCategoryLink
-----------------------
imageid: integer foreign key references Image:rowid
categoryid: integer foreign key references Category:rowid
Clear?
One table category with id and name etc, one table for image with id src name etc.
Two choices after that :
If an image has one and only one category, put a field id_category in table image
If an image can has several categories make another table image_category with id_image and id_category
Since you are a beginner (no offence) and I don't think there will be millions or cartoons in the table. Use a SET field (or ENUM if your cartoon can only have one category) for you categories.
There are people who will vote this down since there are some negative side effects of using this (mainly when you want to change the categories). But with a relative small table this will not have any effect.
This will be the easiest solution. If you expect the site to grow big, use a second table for your categories and join the tables.

PHP/MySQL website with users table, need to add usergroup functionality

I have working website in PHP with a MySQL backend which has several tables for different purposes.
This site is based on different parts or 'environments' like a bugtracker, project management, etc.
There is one central 'users' database which has all the users with the associated details in them.
In each of previously mentioned 'environments', which all have their own set of tables, it is possible to specify a user ID in certain fields.
e.g. the bugtracker table has a column called AssignedTo which contains the user ID's of users to whom bugs have been assigned.
The field type of these 'user ID' columns (like the AssignedTo example) is the same as the UserId field in the central users table, which is an unsigned, zerofilled INT(5) field.
Now I have a request from the users of this site to also allow to specify user groups in certain of these user ID fields.
So again reffering to the AssignedTo example, it should now be possible to also assigned a bug to a user group, instead of a specific user.
What's the best way to do this regarding the PHP scripting and the database layout?
Now I have all these fields set to the same type as the UserId of the central users table, which is INT(5).
However my UserGroupId field in the UserGroups table, is of a different format, INT(3).
I could make this field also into an INT(5) field, but that would not solve the 2nd issue I'm having: how to see whether the AssignedTo value is reffering to a specific user, or to a usergroup.
I was thinking about make the UserGroupId field start from 99999 and counting downwards, while the UserId field is starting from 00001 and counting upwards, thus assuming that if the AssignedTo starts with 9, it's reffering to a usergroup.
But this doesn't seem like a clean solution to me...
Any better ideas?
Thanks!
I think I understand what you are trying to say. I have a question. Can a user be in multiple UserGroups?
I would probably add a column in the bug table that says whether the AssignedTo value refers to a UserID or a UserGroupID.
Create a separate table for UserGroups.
If Users can belong to multiple groups, create an association table like: AssociationID, UserID, UserGroupID.
Otherwise if each user can only belong to one group, just add a UserGroupID column to the Users table
If I am understanding correctly, my solution would be to instead of having an AssignedTo column pointing to either a user or a user group, I would create two columns. One pointing to the user id and another pointing to a user group id.
Actually a colleague at work came up with the following solution which I really like:
Change the UserId and UserGroupId field types from INT(5) to INT(4). And leave the different fields like AssignedTo set to INT(5).
Now in the PHP code I can add a prefix number to either the 'UserId' or 'UserGroupId' value, this prefix number can be used to determine if the value is reffering to a UserId or a UserGroupId value.
So if the AssignedTo field is '10005' it means it's a 'user' with 'id: 0005'. Also to prevent having to update all existing records, values which have a '0' at the first position will be considered users
The advantage over using positive/negative values here is that in both the Users and UserGroups tables I can still use a positive 'Id' field which can be left to autoincrement. As far as I know auto-incrementing is not possible with negative values

php dynamic checkboxes

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.

Categories