recursively get all child categories from a parent id - php

I'm having trouble making a solution with this problem: my database looks like this.
ID parent_cat_id child_cat_id
1 27 10
2 15 20
3 80 90
4 90 88
5 60 30
6 88 40
7 15 60
8 30 40
9 27 95
10 40 30
The context is the user selects a category for product search, I need to get that category IDs and all of it's children which can go 4 levels deep.
The database is similar to this post:
Check if column value exists in another column in SQL
However the OP only needs 1 match for me it needs to be recursive. I'm still trying out several solutions but I'm at a loss. Would love some suggestions.
Thanks in advance.

Related

How to retrieve and display 3 highest value in Laravel?

id
names
voted
1
John
100
2
Mike
25
3
Kathy
200
4
William
186
5
Jasmine
60
6
Sui
57
7
Chris
43
8
Stewie
103
9
Steve
150
10
Jennifer
97
I was able to retrieve the highest value in my table and decided to try retrieving and displaying the 3 highest value from my table but I don't know how and can't seem to find a tutorial or similar codes to what I'm trying to do. What should I do?
My code
public function votesList(){
$data=votes::all()->max('voted')->get();
return view('list',compact('data'));
}
The result I want
id
names
voted
3
Kathy
200
4
William
186
9
Steve
150
$data=votes::orderby('voted', 'desc')->limit(3)->get();

Ranking Leaderboard using MYSQL Query

i am having a problem, i am not that good in logic, so i am trying to do this by just using queries only,
i have a list of data, where i need to get their ranking, but the problem is, i need to get the first two upper to me and the last two who is lower to me.
for example
id| name | score
1 bob 20
2 anna 10
3 jose 30
4 boni 30
5 lea 100
6 leo 10
7 qwertina 90
8 josh 50
9 king 40
10 queen 10
imagine that my id value as a user is 7
so if i log in and my id is 7
i need to get the output of
id| name | score
5 lea 100
6 leo 10
7 qwertina 90
8 josh 50
9 king 40
is this possible in mysql query? any help would be really appreciated, i am really stuck with this problem.
You seem to base your logic on id's which is quite strange but here we are :
SELECT id, name, score
FROM yourTable
WHERE id BETWEEN yourId-2 AND yourId+2
Not sure of a MySQL only solution, but in pseudo php code
Grab records ordered by score
set a counter at 0
for loop starting 0, <= count of results
if the id your id?
if the loop index < 3 ? // there aren't two people ahead of you
return rows 0 to 4
elseif the loop index > count -3 ? // you are one of the last
return rows count -4 to count
else
return rows index -2 to index + 2 // you have 2 above and below you
Try that and if you get stuck post your PHP

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 php sort by highest of 3 separate fields

I've got a table with 3 separate scores in 3 separate fields:
User / Score 1 / Score 2 / Score 3
Person 1: 10 21 7
Person 2: 17 4 20
Person 3: 1 5 22
Is there a mysql command that will effectively sort each person out by the highest score from the 3 fields.
So here I need it to return:
Person 3: 1 5 22
Person 1: 10 21 8
Person 2: 17 4 20
The only way I can think of doing it would be to put them in an array, check each number for each person against each other to find the highest, then sort them into a different array.
This seems very long-winded and labour intensive though.
Add order by greatest(score1,score2,score3) desc
Manual for GREATEST()

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.

Categories