How to calculate MLM User Income points in php - php

I'm trying to Calculate Income Points for MLM network Users (i.e).,
I'm having five types of kits such as Rs.0/-, Rs.1000/-, Rs.2000/-, Rs.4000/-, Rs.10000/- .
Once the kit is purchased a unique pin and user id will generate for each individual kit like
If 2 number of 2000/- kit purchased means userid and pin will be
2000100 539fdda37c435 and
2000101 5395b0d8b66d1 .....
Then Based on new user sponsor id(Sponsor id will be old user), registration will be proceed with Group left and right.
Now what's my problem is While calculating income point of a user how can i take their children user from top to bottom. I'd strucked in MYSQL Query.
For Example.,
now how can i get each node's value for a parent node(1) to root node(12) from this dynamic tree in php ??
I struck in this loop concept.,
Thanks IN Advance.,

database architecture
id par lev lef rgt status wallet userstatus
1 1 1 2 3 1 0 0
2 2 2 4 5 1 0 0
6 3 2 6 7 1 0 0
8 4 3 8 9 1 0 0
9 5 3 10 11 1 0 0
10 6 3 12 13 1 0 0
11 7 3 0 0 1 0 0

Related

Why does not work whereBetween in laravel

I have a model named Student and this model contains some field
id
teacher_id
roll_number
1
1
1
2
1
2
3
1
3
4
1
4
5
1
5
6
1
6
7
1
7
8
1
8
9
1
9
10
1
10
11
2
11
12
2
12
13
2
13
I have a form that passes values teacher_id, start_roll and end_roll
I have a query where I use whereBetween method to get student information
$studentData = Student::where('teacher_id',$request->teacher_id)->whereBetween('roll_number',[$request->start_roll,$request->end_roll])->get();
where start_roll value is 1 and end_roll value is 10 and teacher_id value is 1 but it is strange it only return two array values where id 1 and 10 only
Again if I search start_roll value is 5 and end_roll value is 10 and teacher_id value is 1 but it also strange it returns an empty array
If I print the above query when search it prints like below
Student::where('teacher_id',1)->whereBetween('roll_number',[5,10])->get();
If I execute the above query hardcoded(entry roll_number manually from 5 to 10) then it works fine for me and it returns 6 number of the array where id start 5 and end 10
I don't know why my search is not working. Any suggestion is appreciate please
You should add all requested values 1,2,3,4,5,6,7,8,9,10.
Like This:
$queryRolls = range($request->start_roll,$request->end_roll);
$studentData = Student::where('teacher_id',$request->teacher_id)->whereIn('roll_number',$queryRolls)->get();

MYSQL query that contains 2 things I need in 1 query

I have built a live scoring system for a game. When the scores are entered into the MySQL database, they are entered like this...
ID userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3
1 1 1 1 10 5 2
2 2 1 1 10 5 3
3 3 1 1 12 6 3
4 1 1 2 10 6 4
5 2 1 2 12 5 3
6 3 1 2 9 4 3
As you can see from the above, there is 1 session, 2 different sprints (iterations) and 3 different users. I want to display a leader board. The leader board would show (by iteration), user name (from another table), sprint, point value 1, point value 2, point value 3, sum of point value 1 cumulative for all iterations in that session. So on iteration 1, point value 1 and sum of point value 1 cumulative would be the same. But, in iteration 2, I should see a sum of point value 1 of 20 for user 1, 22 for user 2, and 21 for user 3, in the proper order (descending). I tried a subquery using a select sum, but couldn't quite get it right, and I tried a SUM(IF()), which seemed like it would be right, but it's not. I guess I need some guidance on which direction to go.
I think this query will get you the raw data you want. You will probably want to adjust the query with some form of ordering on total points, and possibly WHERE and LIMIT clauses to meet your exact needs.
SELECT s1.userid,
s1.sessionid,
s1.sprintid,
s1.pointvalue1,
s1.pointvalue2,
s1.pointvalue3,
SUM(s2.pointvalue1) AS sum_pointvalue1,
SUM(s2.pointvalue2) AS sum_pointvalue2,
SUM(s2.pointvalue3) AS sum_pointvalue3
FROM scores s1
JOIN (SELECT userid,
sessionid,
sprintid,
SUM(pointvalue1) AS pointvalue1,
SUM(pointvalue2) AS pointvalue2,
SUM(pointvalue3) AS pointvalue3
FROM scores
GROUP BY userid, sessionid, sprintid
) s2
ON s2.userid = s1.userid AND s2.sessionid = s1.sessionid AND s2.sprintid <= s1.sprintid
GROUP BY userid, sessionid, sprintid
ORDER BY sprintid
Output:
userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3 sum_pointvalue1 sum_pointvalue2 sum_pointvalue3
2 1 1 10 5 3 10 5 3
3 1 1 12 6 3 12 6 3
1 1 1 10 5 2 10 5 2
2 1 2 12 5 3 22 10 6
3 1 2 9 4 3 21 10 6
1 1 2 10 6 4 20 11 6
SQLFiddle Demo

array_push and array_search array_splice on Laravel query builder

Are there any other eloquent query method to deal with the gross process on laravel query builder?
product table
id A_product B_product
1 4,5,8,9 3,7,10,15
2 4,7,10,11 2,3,5,13
which is better does A_product column use array or json?
----search--------------------------------------
if I want to confirm if is there 3 in A_product column on id 1
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
if(in_array("3"){
$A_result='1';
}else{
$A_result='0';
};
------insert----------------------------
if I want to add new num 20 to the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
array_push($querys,"20");
$quearorder=array_value($querys);
DB::table('product')
->where('id', 1)
->update(['A_product' => $queryorder]);
--delete--------------------------------------
if I want to delete num 3 from the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
$key=array_search(3,$querys);
array_splice($querys,$key,1);
DB::table('product')
->where('id', 1)
->update(['A_product' => $querys]);
Its suggestion that you should store all product into separate column , with product type and the group or for user_id
id product product_type category(or for user)
1 4 A 1
2 5 A 1
3 8 A 1
4 9 A 1
5 4 A 2
6 7 A 2
7 10 A 2
8 11 A 2
1 3 B 1
2 7 B 1
3 10 B 1
4 15 B 1
5 2 B 2
6 3 B 2
7 5 B 2
8 13 B 2
Then you can easily apply CRUD operation

Select where rows exist more than once with variation

This is a hard one to put a title to so I apologise for ambiguity.
I have the following MySQL table (it's a Magento table):
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
3 45 1 175 Sale Outerwear
4 45 0 177 (null)
5 45 1 177 New Arrivals
6 45 0 178 New Tops
7 45 1 178 New Tops
As you can see, some of the rows have the same everything except store_id.
I want to do the following:
If a row with store_id = 0 has a duplicate row, but with store_id = 1 and different values (for example, rows 4 and 5 above), update the row with store_id = 0 to have the same value as the other.
Delete the row with store_id = 1
I know I will probably need a combination of both PHP and MySQL for this. I just don't know what the MySQL query would be.
Any help would be great!
EDIT
The end goal from the above table is the following
id attr_id store_id entity_id value
----+---------+---------+------------+------------------------
1 45 0 173 Sale Gifts + Apartment
2 45 0 175 Sale Outerwear
4 45 0 177 New Arrivals
6 45 0 178 New Tops
In order to retrive redundunt values having the same entity_id, you can do :
SELECT
*
FROM
magento m1, magento m2
WHERE
m1.attr_id=m2.attr_id
AND
m1.entity_id=m2.entity_id
AND
m1.id > m2.id
And for fixing null values, you will need to loop the above results and search for the null and replace it with the previous or next result.

mysql update sequence into one table depending on another table

Below are the 2 tables:
classes
academic_year_id student_id standard
2 1 10
2 2 10
2 3 10
2 4 10
2 5 10
2 6 10
2 7 11 Science
2 8 11 Science
Here standard is not INT
student
student_id roll_no name teacher_approval
1 0 S Sumeet G 1
2 0 Nair Nikhil R 1
3 0 Nayak Ankita R 0
4 0 Rathod Ketan P 0
5 0 Patel Vishal D 1
6 0 Patel Jignesh R 0
7 0 Prajapati Bhavesh A 1
8 0 Shah Harsh N 1
What i want to do:
when teacher selects standard -> 10 and press a button "Assign Roll No"
i want all student of 10th standard to be given roll no sequentially order by name and which are approved by teacher that is teacher_approval = '1'
So my student table becomes as below:
student
student_id roll_no name teacher_approval
1 3 S Sumeet G 1
2 1 Nair Nikhil R 1
3 0 Nayak Ankita R 0
4 0 Rathod Ketan P 0
5 2 Patel Vishal D 1
6 0 Patel Jignesh R 0
7 0 Prajapati Bhavesh A 1
8 0 Shah Harsh N 1
I have so far tried below code:
$standard = $_POST['standard']
SET #incr = 0
UPDATE
student AS st
JOIN
( SELECT * FROM
classes AS classes
WHERE standard = '".$standard."'
) AS tmp
ON tmp.student_id = st.id
SET
st.roll_no = #incr:=#incr+1
WHERE st.teacher_approval = '1'
ORDER BY st.name ASC
But it gives me an error: incorrect usage of update and order by
Anyone with a similar experience? Any help would be appreciated. Thanks.
In MySql, you can't have an ORDER BY as part of the UPDATE directly when using multiple tables (see this link). Try this instead :
UPDATE student AS table1
INNER JOIN (
SELECT st.student_id, st.roll_no, st.name, st.teacher_approval
FROM student AS st
JOIN (
SELECT * FROM
classes AS cl
WHERE cl.standard = '".$standard."'
) AS tmp
ON tmp.student_id = st.student_id
WHERE st.teacher_approval = '1'
ORDER BY st.name ASC
) AS table2
ON table2.student_id = table1.student_id
SET table1.roll_no = #incr:=#incr+1

Categories