How to construct table with dynamic input fields? - php

I have a PHP script where you can (as admin) select how many input-fields there will be in a question form. Some of the fields are non optional, but som are (as many as you like).
The table in MySQL for collecting the answers looks like this:
id | userid | fname | ename | seat | optional
If the admin want it to be two optional input-fields then the result of one filled form would take three tows in the table:
| 5 | 3 | Peter | Pan | 4 | |
| | 3 | | | | opt.value1 |
| | 3 | | | | opt.value2 |
Is this really the best way to store this in? How would you solve it?
And also, how can I make shure that the userid is unique for the user? I can't use the auto-increment key value thing in MySQL because the same value is on three rows...

The way i learned it you have to use multiple tables. Like this:
Table1:
id | userid | fname | ename | seat
Table2:
userid | optional
Table2.userid is a reference to Table1.userid
Then the fields that has to be filed can be put into the first table and all the optional in the second.
If i follow your example your database should look like this:
Table1:
id | userid | fname | ename | seat
5 | 3 | Peter | Pan | 4
Table2:
userid | optional
3 | opt.value1
3 | opt.value2
By the way, why do you have both id and userid in Table1?

Best practice would be to store "id" and "optional" values in a separate table.
Then pull the information you want from it for each "id".

Related

Using Two Tables in PHP & MySQL to display one loop result

Ok so a bit of a funny question here. I have two tables: -
LiveTable
ArchiveTable
The data is as follows: -
Table: LiveTable Table: ArchiveTable
| ID | NAME | | ID | NAME |
------------------ ------------------
| 1 | Test One | | 4 | Test Four |
------------------ ------------------
| 2 | Test Two | | 5 | Test Five |
------------------ ------------------
| 3 | Test Three| | 6 | Test Six |
What I want to do is merge them into one table for querying purposes only. Not as a Database Structure.
In essence when I do a PHP Loop I want the results to work like this: -
Merged Results
| ID | NAME |
------------------
| 1 | Test One |
------------------
| 2 | Test Two |
------------------
| 3 | Test Three|
------------------
| 4 | Test Four |
------------------
| 5 | Test Five |
------------------
| 6 | Test Six |
How would I go about doing this? Also is there a way of doing this with Doctrine?
You can use an SQL query with UNION:
SELECT ID, Name
FROM LiveTable
UNION ALL
SELECT ID, Name
FROM ArchiveTable
Note: UNION ALL will retain duplicates. If you want to remove duplicate records, then use UNION.
Yould use UNION:
SELECT id, name FROM tbl1
UNION ALL
SELECT id, name FROM tbl2
Create DB view using above two tables
CREATE VIEW view_name AS SELECT
id, name FROM tbl1
UNION
id, name FROM tbl2
Then you can query on your view
you can use the sql query multi table select
Select * from LiveTable,ArchiveTable
that's it

Best way store an array of items with details in SQL table

I'm trying to find out the best way store array of items with details in SQL table.
I have a user account database. The user have multiple input fields to enter multiple details like:
___ : ______ +(get more multiple field field)
User can input any details like
Output1 : Output2
Mobile : 2455...
email : sdf
city : dfs
Other : sf
On an average a user will use around 20 options
Since the fields (mobile, email etc.) are not known to me, I have to store Output1 field with the answer field (output2).
I will be having a very huge user base, so I think it's not better to create separate tables for each user.
Is there any way to store and get the details in limited or single column.
Since both the attribute name and value comes from users, a typical 3-table model of saving many-to-many relationship is a bit of overkill.
I would just kept users and their attributes in two separate tables:
+---------+-----------+--------------+
| user_id | user_name | user_email |
+---------+-----------+--------------+
| 1001 | John | john#doe.com |
+---------+-----------+--------------+
| 1002 | Tim | tim#doe.com |
+---------+-----------+--------------+
+----------+-----------+--------------+--------------+
| field_id | user_id | field_name | field_value |
+----------+-----------+--------------+--------------+
| 1 | 1001 | Option1 | Option2 |
+----------+-----------+--------------+--------------+
| 2 | 1001 | Mobile | 2345656565 |
+----------+-----------+--------------+--------------+
| 3 | 1001 | city | dfs |
+----------+-----------+--------------+--------------+
| 4 | 1002 | Other | something |
+----------+-----------+--------------+--------------+
Possibly with some additional columns for sorting, tagging, etc.
I would use 3 tables.
Table 1 - user. PK is UserId. Other fields are name, rank, serial number, etc
Table 2 - Attribute - Primary key is AttributeId. Other Field is attribute name. Examples of attribute names are emailAddress, cellphoneNumber, cityName.
Table3 - UserAttribute. Primary Key is UserId and AttributeId. Other field is Attribute value.

how to maintain a continuous id number row count in mysql

i have an id coloumn which is integer and auto incremented type.
The problem is when ever i delete a row the continuity of the number breaks.
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Behance | 2 |
| Techcrunch | 3 |
| flipkart | 4 |
+----------------------+----+
FOR EXAMPLE if i delete the row with id=2, then i output in id will be
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Techcrunch | 3 |
| flipkart | 4 |
+----------------------+----+
but i want it to be like :
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Techcrunch | 2 |
| flipkart | 3 |
+----------------------+----+
How to do it ??
To directly answer your question, here's how you fix those gaps in sequential numeric fields: Fixing gaps in mysql table row id after we delete some of them
But let's be careful here for a moment.
Let's assume id is your primary key. ID's are usually the point of reference to an object, because auto-generated ID's are unique. Call it a convention.
That means that If ANY part of your code depends on the id column, your application will break.
If you NEED to do this, then use some other field as main reference. Perhaps an unique name field or something similar.
If ID is NOT your primary key, then you probably should've chosen another name for it to begin with. Anyway, in this case, the chances of you breaking anything are much smaller.
Notice that I said smaller, but not zero. We don't know your application, so it's possible that your code uses id for something important, and that'll mean trouble for you.

MySQL query how to get list of all distinct values from columns that contain multiple string values?

I am trying to get a list of distinct values from the columns out of a table.
Each column can contain multiple comma delimited values. I just want to eliminate duplicate values and come up with a list of unique values.
I know how to do this with PHP by grabbing the entire table and then looping the rows and placing the unique values into a unique array.
But can the same thing be done with a MySQL query?
My table looks something like this:
| ID | VALUES |
---------------------------------------------------
| 1 | Acadian,Dart,Monarch |
| 2 | Cadillac,Dart,Lincoln,Uplander |
| 3 | Acadian,Freestar,Saturn |
| 4 | Cadillac,Uplander |
| 5 | Dart |
| 6 | Dart,Cadillac,Freestar,Lincoln,Uplander |
So my list of unique VALUES would then contain:
Acadian
Cadillac
Dart
Freestar
Lincoln
Monarch
Saturn
Uplander
Can this be done with a MySQL call alone, or is there a need for some PHP sorting as well?
Thanks
Why would you store your data like this in a database? You deliberately nullify all the extensive querying features you would want to use a database for in the first place. Instead, have a table like this:
| valueID | groupID | name |
----------------------------------
| 1 | 1 | Acadian |
| 2 | 1 | Dart |
| 3 | 1 | Monarch |
| 4 | 2 | Cadillac |
| 2 | 2 | Dart |
Notice the different valueID for Dart compared to Matthew's suggestion. That's to have same values have the same valueID (you may want to refer to these later on, and you don't want to make the same mistake of not thinking ahead again, do you?). Then make the primary key contain both the valueID and the groupID.
Then, to answer your actual question, you can retrieve all distinct values through this query:
SELECT name FROM mytable GROUP BY valueID
(GROUP BY should perform better here than a DISTINCT since it shouldn't have to do a table scan)
I would suggest selecting (and splitting) into a temp table and then making a call against that.
First, there is apparently no split function in MySQL http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/ (this is three years old so someone can comment if this has changed?)
Push all of it into a temp table and select from there.
Better would be if it is possible to break these out into a table with this structure:
| ID | VALUES |AttachedRecordID |
---------------------------------------------------------------------
| 1 | Acadian | 1 |
| 2 | Dart | 1 |
| 3 | Monarch | 1 |
| 4 | Cadillac | 2 |
| 5 | Dart | 2 |
etc.

How to sum a "n" number of columns or rows

Each user must fill each month a number of parameters.
The number of parameters that need to supplement it varies from user to user, and
each user can add a new parameter (but they can not delete existing
ones if they already have values filled in).
EX: user_1 has: param_1, param_2; user_2 has: param_1, param_3, param_4, and so.
I have to sum these parameters at the user level.
I have two problems:
which is the best option for table structure?
considering: the max number of parameters a user cah have is 12; the number of active users will reach around 5000 (i hope)
distinct columns
| user_id | month | param_1 | param_2 | ... | param_12|
distinct rows:
| user_id | month | param_id | param_value |
| 001 | nov-2012 | param_1 | 100 |
| 001 | nov-2012 | param_2 | 125 |
| 002 | nov-2012 | param_1 | 110 |
| 002 | nov-2012 | param_3 | 150 |
| 002 | nov-2012 | param_4 | 175 |
and how can I calculate the sum of parameters fosr each user, considering the best option for table structure?
I think the second approach is the best suited for your case because in that case you are not wasting any memory and in future if you have to extend your database then also it not need any significant changes to the database.
Now the second problem of summing the parameter then you can fetch all the record of a particular user by its user_id and then add all those values
You can write query as follow's
$query = "SELECT SUM(param_value) FROM table_name WHERE param_id = $id";
mysql_query($query);

Categories