How to make multi-level user referral system - php

I'm working on a multi-level referral system for signup based on PHP, but I can do the signup process to first level only. The referral system levels are managed from administrator control panel.
Table: referral_levels
id referral_level
-------- --------
1 1
2 2
3 3
4 4
Table: referrals
id new_user refferal_level referral_user_id
-------- -------- -------------- ----------------
1 7 1 6
2 8 2 6
3 8 1 7
4 9 3 6
5 9 2 7
6 9 1 8
7 10 4 6
8 10 3 7
9 10 2 8
10 10 1 9
On user signup I want to check the following:
1. If the current user who signup is referred by (1)another user - DONE!
2. If the (1)another user has been referred by (2)someone else
3. If (2)someone else is referred again by "x" user and so on...
I'm confused how to write the code for p.2 and p.3 when the signup form is posted and have to check who is referred by who and insert the data for sub-level referrals.

Related

percent match within the same table

Below I have an MySQL database where "id" is just an id for each row. "question" shows the id of the question. There are four questions, 11, 12, 13 and 14. For every question the user has four answer options (1,2,3 and 4) which is stored in the "answer" column. The "user" column indicates what user who answered. In this example we have user 10, 11 and 12
id question answer user
1 11 2 10
2 12 2 10
3 13 3 10
4 14 4 10
5 11 2 11
6 12 2 11
7 13 4 11
8 14 1 11
9 11 2 12
10 12 2 12
11 13 1 12
12 14 1 12
Let's say that user 10 is the reference user which means that I want to know how well user 10 matches with the others. Using SQL and/or php code how can I match the answers of the users such that I get the matches in percent with the highest percent shown first. So in this example I'm looking for something like.
user percent
1 11 50%
2 12 75%
I'm not sure if this is possible all the way with only SQL. Maybe a bit of php is needed to convert the count to % for instance.
Finally i got your desired output
Try this:
SELECT * ,round(count(*)/(SELECT count(*) FROM `list` where user=10)*100) as
pecentage FROM `list` as l where l.answer=(SELECT m.answer FROM `list` as m
where m.user=10 and l.question=m.question) group by (l.user) order by pecentage
It will produce out put as
and you can add l.user!=10 in where condition if you don't want user 10 value.

mysql advanced query select based on params

As first please be nice to me im a beginner with SQL language and english language.
So i have problem with this query.
I've created sqlfiddle.
My query look like it's working properly but I find it is not working
I would like to write a query that returns the product ID variant based on the parameters that will send
Correct query result would look like the following
PARAM = 6
id product_id productvariant_id attributevalue_id
1 3 1 6
2 3 2 6
---- BAD RESULTS -----
3 3 3 6
4 3 3 9
6 3 5 6
7 3 6 6
8 3 6 11
PARAM = 6,9
id product_id productvariant_id attributevalue_id
3 3 3 6
4 3 3 9
---- BAD RESULTS -----
3 3 3 6
4 3 3 9
6 3 5 6
7 3 6 6
8 3 6 11
What i really need is return productvariant_id which contains combination of inserted params, if I send only one attributevalue_id, i need to find productvariant which contain only ONE attributevalue_id. IF i send two params i finding combination of two.. not more or less
Unfortunately I do not have enough reputation for a comment, thus I have to write this as answer.
Could you clarify your goal?
Do you want to retrieve the productvariant_id of all datasets where the attributevalue_id matches your parameter?
SELECT productvariant_id
FROM productvariantattribute
WHERE attributevalue_id IN ($YOUR_PARAMETER_LIST);
Or do you want to retrieve the productvariant_id of all datasets where the productvariant_id has only these attributevalue_ids you specified as parameter?
SELECT `productvariant_id`
FROM `productvariantattribute`
WHERE `attributevalue_id` IN ($YOUR_PARAMETER_LIST)
AND `productvariant_id` NOT IN (
SELECT `productvariant_id`
FROM `productvariantattribute`
WHERE `attributevalue_id` NOT IN ($YOUR_PARAMETER_LIST)
)

Prevent duplicate row values

I've done some digging and I can't find an effective way to prevent duplicate entries based on my needs. I need columns 2 (proj_id) and column 4 (dept_id) never to be the same, as each dept would only work on a project once. So, rows 1 and 4, 6 and 7, and 14 and 15 shouldn't be allowed. I'll keep digging as well.
summary_id proj_id hours_id dept_id date_entered
1 8 3 6 9/9/2012
2 2 2 6 9/9/2012
3 1 6 19 9/9/2012
4 8 3 6 9/9/2012
5 2 5 17 9/9/2012
6 7 2 5 9/9/2012
7 7 2 5 9/9/2012
8 2 5 17 9/9/2012
9 7 4 17 10/10/2012
10 3 6 1 10/10/2012
11 5 1 15 10/10/2012
12 4 4 3 10/10/2012
13 3 5 1 10/10/2012
14 8 2 13 10/10/2012
15 8 2 13 10/10/2012
Before applying unique combine key to your table, you have to remove duplicate records first then apply the following sql command:
ALTER TABLE your_table_name ADD UNIQUE (proj_id, dept_id);
Define an unique key on both columns
ALTER TABLE `your_table` ADD UNIQUE (`proj_id`, `dept_id`);
Looks like you are new to php and mysql. So here's the easiest way to do it.
Log on to PHPMyAdmin
Select your DB and your table.
View the Structure of it (clicking the button on top of the screen).
Check the two fields (proj_id and dept_id) using the check box on the left.
At the bottom of the table you should find the the words "With selected" and in front of it some actions. Select the option to make "Primary".
Of course, if you have duplicate entries first delete them.

Mysql select switching rows for every execution

I am developing auto bidding system,There is a tricky scenario. That is, I want select next machine to bid from "table_machine" on each execution of code.
Which means, I am picking bids from active (status=1) machine to bid.
execution 1 bidz from mid 1
execution 2 bidz from mid 2
.......
execution 4 bidz from mid 4
execution 5 bidz from mid 1
after execution 1 if user 2 bidz on live then machine bid should negleate mid 2 and its should bid mid3
I am having two tables
table_bid_history
history_id user_id product_id price type
1 1 1 10 live
2 2 1 10.5 live
3 1 1 11 machine
4 2 1 11.5 machine
5 3 1 12 machine
6 4 1 12.5 machine
7 1 1 13 machine
table_machine
mid user_id product_id start end num_bidz status
1 1 1 8 12 5 1
2 2 1 10 15 15 1
3 3 1 11 16 10 1
4 4 1 11 16 10 1
How can i do this , I am feeling this is tricky But there must be way to do this. give me idea please .
Please advise me ! Thanks
If i understand correctly you could do this :
create a new fake field on your machine_table and named it lastSelect and in your Where Clause Use something like this :
SELECT FROM .............
LEFT JOIN table_machine tm ON tm.lastSelect=1
WHERE id = IF(tm.id=MAX(tm.id),1,tm.id)
Then update current lastSelect on table_machine to 1

Math question regarding a Fantasy Sports (snake) draft

If you're familiar with any fantasy sports draft, the draft-order grid looks something like this:
EXAMPLE 1 (3-teams):
Round Team 1 Team 2 Team 3
1 1 (1.1) 2 (1.2) 3 (1.3)
2 6 (2.3) 5 (2.2) 4 (2.1)
3 7 (3.1) 8 (3.2) 9 (3.3)
The numbers 1-9 represent the overall pick number of the draft.
The items in parentheses represent the round_number and pick_number_of_that_round.
I cannot figure out a formula which converts my overall_pick_number into it's proper pick_number_of_that_round.
In the above example, the number 8 equals 2 (the 2nd pick of the 3rd round). But in a 4-team league, the number 8 equals 4 (the 4th pick of the 2nd round).
EXAMPLE 2 (4-teams):
Round Team 1 Team 2 Team 3 Team 4
1 1 (1.1) 2 (1.2) 3 (1.3) 4 (1.4)
2 8 (2.4) 7 (2.3) 6 (2.2) 5 (2.1)
3 9 (3.1) 10 (3.2) 11 (3.3) 12 (3.4)
I thought about trying to dynamically build an associative array based on the number of teams in the league containing every pick and which pick it belonged to, but it's just beyond me.
I'll modify the answer by missingno to make it twist like a 'snake'
round_number = ((overall_pick_number - 1) / number_of_teams) + 1
pick_number_of_round = ((overall_pick_number - 1) % number_of_teams) + 1
if (round_number % 2 == 0) {
pick_number_of_round = number_of_teams - pick_number_of_round + 1
}
round_number = ((overall-1) / number_of_teams) + 1
pick_number_of_round = ((overall-1) % number_of_teams) + 1

Categories