how create best schema database with ratings system and available in month? - php

i will create schema the database MySQL. There are username, password etc and rating system and available in month.
Standard it looks like this:
id | username | password | january | february | march | rating1 | rating2| rating3 |
1 | john | xxx | 1 | 0 | 1 | 3 | 3 | 6
2 | amy | xxx | 1 | 1 | 0 | 1 | 6 | 3
if a user buys article then must add a rating (1,2,3). if chose 2 in John then rating2 +1
id | username | password | january | february | march | rating1 | rating2| rating3 |
1 | john | xxx | 1 | 0 | 1 | 3 | 4 | 6
2 | amy | xxx | 1 | 1 | 0 | 1 | 6 | 3
january, february (1 - available, 0 - not available)etc set himself John or Amy.
this is good? Should I create separate tables for Januar/feb/march and rating1/2/3? Maybe only for rating?

im not a hard core database designer but at a glance this is schema is not normalized.
if you can create a separate table for month and rating would be better please find the below sample
d | username | password
1 | john | xxx
2 | amy | xxx
id | month | rating
1 | 1 | 3
MonthID | MonthName
1 | January
How ever this sujjestion may not be the ideal solution for your proposed database.

OK I know this is late but this is very serious!!!
First use bcrypt for encrypting passwords
Second NEVER store a password that is not encrypted properly. You need a "salt" to do this correctly.
See here: http://www.nathandavison.com/posts/view/13/php-bcrypt-hash-a-password-with-a-logical-salt
I'm not a php guy but it looks good.
As for the DB, Prabhakantha's answer looks OK but might not be complete suitable.
Users
id | username | password | salt
1 | john | xxx | blah
2 | amy | xxx | ughh
ratings
id | month_id | rating
1 | 1 | 3
Months
id | month | year
1 | January | 1990
I know in the ruby on rails world I would model this with a polymorphic association. Take a look how that works here: http://railscasts.com/episodes/154-polymorphic-association
Yes it is a video on rails but it applies here.

Related

Do we need to create separate tables for diff types of recharges such as(mobilerech,dthrech,elecbill, waterbill, etc) or can done in 1 table

I am working on a project for recharge and bill payments. I am confused about whether to use a single table for all type of recharges like mobile recharge, dtn recharge, electricity bill, water bill, card recharges, etc, which is difficult or do I create separate tables for each type of recharge and work on them.
Table has colums
recharge_id PRIMARY KEY,
recharge_amount ,
recharge_status,
recharge_time,
user_id,
payment_id
The data has to be added into the table when there is any recharge process with status and other details.
Although, you didn't show anything you tried, i think this is a viable question.
A possible approach would be to create one table for your type and one for your recharges
Something like the following should work
create a table recharge_type like
+----+------------------+--------+
| id | name | active |
+----+------------------+--------+
| 1 | Mobile recharge | 1 |
| 2 | Dtn recharge | 1 |
| 3 | electricity bill | 1 |
| 4 | water bill | 1 |
| 5 | card recharge | 1 |
+----+------------------+--------+
and your table recharge
+----+------------------+---------+------------+--------+--------+------------+
| id | recharge_type_id | user_id | payment_id | amount | status | time |
+----+------------------+---------+------------+--------+--------+------------+
| 1 | 1 | 1 | 1 | 2.00 | 1 | 2019-03-05 |
| 2 | 2 | 1 | 2 | 3.00 | 3 | 2019-03-05 |
| 3 | 2 | 1 | 2 | 4.00 | 4 | 2019-03-05 |
+----+------------------+---------+------------+--------+--------+------------+
With this type of construction you are pretty flexible for nearly any approach.
If you want to understand why this is a good approach, you should read
some articles about first normal form. You can find an article
here on Wikipedia.

How to best structure a database while keeping track of dataset changes

I've got a small CRM and I'm trying to figure out the best way to designing the DB tables.
I've currently got a single table for users that got around 30 columns which I alter from time to time. Since I am storing two different information on that table (user + company information) I was thinking of splitting that table into 3 (user + company + connection between these 2) but I am also interested in keeping a copy of any changes that are being made in these rows.
So going from:
user_id | firstname | last_name | company_name | company_city | company_subject | rank | status
1 | John | Borrows | Boink INC | NY | Web dev | 1 | 1
2 | Mike | Smith | Smithin INC | OC | Laywer | 1 | 2
3 | Mary | Anton | Caffin | SJ | Moving | 2 | 1
to something like this
user_id | firstname | last_name | rank | status
1 | John | Borrows | 1 | 1
2 | Mike | Smith | 1 | 2
3 | Mary | Anton | 2 | 1
comp_id | company_name | company_city | company_subject
1 | Boink INC | NY | Web dev
2 | Smithin INC | OC | Laywer
3 | Caffin | SJ | Moving
con_id | user_id | comp_id
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
But I'm not sure how to track the changes when for example a user changes the company name or some other info on user's table etc.
Just follow the normalization rules for structuring your database tables. You will find anything you need for that by just searching for database normalization.
Regarding your "update-history" you could add a Timestamp to your datasets and/or a separate boolean field "outdated" to be able to filter out the latest information.
Would be the simplest solution that comes into my mind.

Creating a report with more 60000 rows of a vertical table in codeigniter. How to paginate?

A lot of thought process went before I decided to post this question. Trying to explain my problem in a simplified format.
I have 2 tables in my mySQL table, one of which is the users and the other one is the questions and answers related to that user.
Simplified Example:
Users
| id | name | registered_on |
|----|--------|---------------------|
| 1 | Aaron | 2017-02-01 00:01:02 |
| 2 | Baron | 2017-02-01 01:01:02 |
| 3 | Chiron | 2017-02-01 02:01:02 |
Answer Keys
| id | user_id | keyword | value | created_on |
|----|---------|---------|---------|---------------------|
| 1 | 1 | gender | Male | 2017-02-01 00:01:02 |
| 2 | 1 | age | 24 | 2017-02-01 00:01:02 |
| 3 | 2 | gender | Male | 2017-02-01 00:01:02 |
| 4 | 2 | age | Unknown | 2017-02-01 00:01:02 |
| 5 | 3 | gender | God | 2017-02-01 00:01:02 |
I hope the relation above is clear. So what I wish to achieve is to create a CSV report like this
| name | gender | age | registered_on |
|--------|--------|---------|---------------------|
| Aaron | Male | 24 | 2017-02-01 00:01:02 |
| Baron | Male | Unknown | 2017-02-01 01:01:02 |
| Chiron | God | NULL | 2017-02-01 02:01:02 |
As my research suggests, this can be done in the following ways :
Prepared Statements (Cannot use because CodeIgniter does not Support)
Paging (The vertical table is the problem)
MySQL Pivot Tables (But with Dynamic column names -- feels complicated!)
Any other better way that I do not know of
I am thinking about paging but am yet to figure out how it could be
used in the case of vertical tables. I would like it if any of you guys have faced the same problem or have some meaningful suggestions! Any help is appreciated!
If I understand well your problem, you want a CSV file with all user informations available using the answer keys as columns?
Why not doing this with 2 queries?
First, you can select all different keys in your answer table (select distinct keyword...)
Then, create your query with joins looping on your results :
$this->db->select('u.id, u.name, u.registered_on');
$this->db->from('users u');
foreach($keywords as $i_key => $s_keyword){
$this->db->join('answers_table at'.$i_key, 'at'.$i_key.'.user_id = u.id', 'left');
$this->db->select('at'.$i_key.'.value as '.$s_keyword);
}
$a_users = $this->db->get()->result();

Is there a way to rank values in MySql according to what column they belong to?

Okay, so lets say that we have 4 columns and 3 rows of data.
|user_id|pick_1|pick_2|pick_3|
-------------------------------
|fred |C++ |java | php |
------------------------------
|eric |java |C++ | php |
------------------------------
|sam | C++ | php | java |
------------------------------
So right now, users are entering their favorite languages. The first pick(pick_1) would be the favorite programming language and the second pick (pick_2) would be the 2nd favorite programming language and etc.
How can I organize this in a way so that I can give a point value according to what columns the programming languages are. So maybe pick_1 can give 3 points, pick_2 can give 2 points and pick_3 can give 1 point.
So when you tally up the scores, C++ will have 8 points, java will have 6 points, and php will have 4 points.
That way I can give an overall ranking of what tends to be the more favorable programming language. Like so
|rank|language|points|
----------------------
| 1 | C++ | 8 |
----------------------
| 2 | java | 6 |
----------------------
| 3 | php | 4 |
----------------------
It doesn't even need to have a point system, I just couldn't think of another way to rank the languages on a scale of liked to un-liked. So if there's another way to yield the same results than please let me know. Otherwise how would I be able to do this. Preferably in just MySql. I am currently using PHP.
Thank you for reading.
You need a simpler structure
User_ID | Pick | Points
Fred c++ 3
Fred php 2
Fred java 1
This way you can do a simple sum(points) group by pick
for a SQL only solution, I would normalize your structure, and put the picks in a different table:
users: user_id; user_name
picks: pick_id; user_id; language; points;
then you would have your data in 2 tables:
| user_id | user_name |
-----------------------
| 1 | Fred |
-----------------------
| 2 | Eric |
-----------------------
| 3 | Sam |
-----------------------
| pick_id | user_id | language | points |
---------------------------------------------
| 1 | 1 | C++ | 1 |
---------------------------------------------
| 2 | 1 | Java | 2 |
---------------------------------------------
| 3 | 1 | php | 3 |
---------------------------------------------
| 4 | 2 | Java | 1 |
---------------------------------------------
| 5 | 2 | C++ | 2 |
---------------------------------------------
| 6 | 2 | php | 3 |
---------------------------------------------
| 7 | 3 | C++ | 1 |
---------------------------------------------
| 8 | 3 | Java | 2 |
---------------------------------------------
| 9 | 3 | php | 3 |
---------------------------------------------
And then use the following query to fetch the desired result:
SELECT language, SUM(points) FROM users JOIN picks ON users.user_id=picks.user_id GROUP BY language
As seen in this fiddle
This way it's also easy to add constraints so people can not vote for a language more then once, or give the same amount of votes to 2 different languages.

PHP Consolidate Inventory into Table

Hopefully this is a quick and easy question to answer.
I have a MySQL Database with the following fields:
shoe_size and waist_size
The shoe_size entries range anywhere from 3 to 21 (incrementing by halves-- 3, 3.5, 4, 4.5, etc).
The same is true for the waist_size entries.
My question is this:
Using PHP, how can I best consolidate this into a table that counts the total number of sizes, so that it could essentially be used as an Order Form?
For example, the MySQL table looks like this:
----------------------------------------------
| Name | shoe_size | waist_size |
----------------------------------------------
| John | 9.5 | 33 |
----------------------------------------------
| Steve | 9 | 32 |
----------------------------------------------
| Tom | 9.5 | 33 |
----------------------------------------------
| Sally | 7 | 8 |
----------------------------------------------
| Jane | 7 | 8 |
----------------------------------------------
And the output HTML (table?) would look something like this:
ORDER FORM
------------------------------------------
| Shoe Size | 7 | 9 | 9.5 |
------------------------------------------
| Total Pairs | 2 | 1 | 2 | <----- This is calculated from above
------------------------------------------
------------------------------------------
| Waist Size | 8 | 32 | 33 |
------------------------------------------
| Total Pairs | 2 | 1 | 2 | <----- This is calculated from above
------------------------------------------
I'm not even sure if my format for the "Order Form" table is the best way to go on this. Any nudge in the right direction would be greatly appreciated.
MySQL might be enough for this task, try something like
SELECT shoe_size, COUNT(id) as count FROM table GROUP BY shoe_size ORDER BY shoe_size asc
I think what you are looking for is GROUP BY.
(http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html)
e.g:
SELECT `Shoe Size`, COUNT(*) FROM table_name GROUP BY `Shoe Size`

Categories