Sort through MySQL result to create multilevel dropdown menu - php

I am just puzzled here. Maybe because it's the end of the day, I dont know. Im using PHP to grab items from a MySQL db. I am just looking for some PHP help. I have the HTML and CSS done. Here is the basic structure of the db. Its a nested set btw. Imagine that my left and right values for the nested set have been sorted in MySQL already which leaves me with depth.
----------------------------------------
| id | title | sequence_number | depth |
----------------------------------------
| 1 | A | 1 | 1 |
| 2 | B | 2 | 1 |
| 3 | C | 1 | 2 |
| 4 | D | 3 | 1 |
| 5 | E | 4 | 1 |
| 6 | F | 2 | 2 |
| 7 | A | 1 | 3 |
| 8 | B | 5 | 1 |
| 9 | C | 3 | 2 |
| 10 | D | 2 | 3 |
| 11 | E | 6 | 1 |
| 12 | F | 4 | 2 |
----------------------------------------
Now if that's not enough to confuse you then keep reading. I am using a foreach loop to loop through each item and sort it manually like that. The problem is the menu eventually will go to 3 levels which gets me stuck as my loop is only for two levels. Can anybody help me sort this array?

you could use a recursive function
eg
loop through with depth set as 1 then re-call the function with depth set as depth + 1
does that help?
Josh

Related

PHP Recursive Function base on single id export excel

I have a search page that allow user to key in the member ID and it will list out all the downline that belongs to the user.
I am using easyui treegrid to generate the downline.
Now i need to do an extra button to export out all the downline that belongs to the search id and export each line of information into excel file.
This is part of my data, and actually the real data had more column and about 4000++ of data.
Is there anyone can help me or some references? Please let me know if you need more info
+-------------+---------------+---------------------------+------------+
| MemberID | parent_id | Name | Age |
+-------------+---------------+---------------------------+------------+
| 1 | 0 | Cassy | 8 |
| 2 | 1 | Peter | 7 |
| 3 | 1 | Maide | 7 |
| 4 | 1 | Samda | 7 |
| 5 | 4 | Kinso | 7 |
| 6 | 4 | March | 7 |
| 7 | 2 | Sandy | 10 |
| 8 | 0 | Mandy | 12 |
+-------+---------------+----------------------------------------------+

SQL calculate percentage of two values with same sub_id.

I'm working on a project where I list 5 problems with 2 solutions each. The users can vote on one solution per problem. Now I have to calculate the percentage of the highest value per problem.
For example in problem 1 I have 20 votes on solution 1 and 30 votes on solution 2, I want to get 60%. I know I will have to count the two vote values together per problem divide by 100 and then multiply by the value that is the highest.
How do I do this in my dao (with sql) ? Do I have to make another column in the table 'solutions'?
table: solutions
+----------+------------+---------+
| id | id_problem | vote |
+----------+------------+---------+
| 1 | 1 | 25 |
| 2 | 1 | 10 |
| 3 | 2 | 18 |
| 4 | 2 | 2 |
| 5 | 3 | 6 |
| 6 | 3 | 7 |
| 7 | 4 | 11 |
| 8 | 4 | 4 |
| 9 | 5 | 5 |
| 10 | 5 | 2 |
+----------+------------+---------+
Try this:
select
id_problem,
CONCAT(ROUND(MAX(vote) / SUM(vote), 2) * 100, '%') as Percentage
from solutions
group by id_problem;

Assigned multiple users to a 'task'

Okay so I'm creating a task manager for my company. A user can assign assign a task to multiple other users. So I've though of 2 ways of implementing this.
This is my tasks table for option one (at least the columns that are important in this discussion ):
----------------------------------------------
| id | assigned_to | assigned_from |
---------------------------------------------
| 1 | 1,3,6 | 4 |
--------------------------------------------
| 2 | 1,4 | 2 |
---------------------------------------------
So here I pretty much just comma separate each user_id that is assigned to this particular task
Option 2:
----------------------------------------------------------
| id | task_id | assigned_to | assigned_from |
------------------------------------------------------------
| 1 | 335901 | 1 | 4 |
-----------------------------------------------------------
| 2 | 335901 | 3 | 4 |
-----------------------------------------------------------
| 3 | 335901 | 6 | 4 |
-----------------------------------------------------------
| 4 | 564520 | 1 | 2 |
-----------------------------------------------------------
| 4 | 564520 | 4 | 2 |
-----------------------------------------------------------
So as you can see here instead of putting the assiged_to is's here I just create a task id which is a random number and then I can groupBy 'task_id'. This is currently they way I have built it but for some reason it feels like it might screw me over in the future (not that option one doesn't give me the same feeling). So my question is which way do you guys recommend or is there maybe a different better way that I could be doing this?
Option 2 ist the better solution since you can acutally work with the table. You may e.g. create another table Tasks with
Task_id | Task_name | Budget | ...
Or a table with user-IDs for assigned_to and assigned_from. All these tables can be joined together if you use 2nd Option.
btw it is the correct normalization form
You can use Option 2 and normalize further if tasks are always assigned by/from the same person.
Tasks table:
task_id | assigned_from
1 | 4
2 | 2
The Assignees table then doesn't need to have the assigned_from since it's always the same for that task_id:
id | task_id | assigned_to
1 | 1 | 1
2 | 1 | 3
3 | 1 | 6
4 | 2 | 1
5 | 2 | 4

Junction tables in MySQL

(Sorry, my english isn't very good)
Hi, I am trying to learn how to work with junction tables in MySQL and I can't figure how to do something. I know the basics of MySQL but I have never worked with "JOIN".
In this test project, I would like to be able to show on a page the app of a given category (you click on "Games", only the apps that are in the "Games" category will be displayed on the page). I would like to know what the SQL request should look like.
Second question, let's say that an App could fit 2 different categories, how can I manage to give that app 2 different Category_ID in my database ?
Here is what my Database looks like at the moment :
Table name: APPS
+------------+-------------------+
| App_ID (pk)| App_Name |
+------------+-------------------+
| 1 | Weather Network |
| 2 | Is it sunny 2.0 |
| 3 | The Weather App |
| 4 | Zelda |
| 5 | Megaman |
| 6 | Doom 3 |
+------------+-------------------+
Table name : CATEGORY
+-----------------+-----------------+
| Category_ID (pk)| Category_Name |
+-----------------+-----------------+
| 1 | Games |
| 2 | Weather |
+-----------------+-----------------+
Table name : JUNCTION_APP_CATEGORY
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+
For your first question, the answer is
SELECT a.*, c.*
FROM APPS a, CATEGORY c, JUNCTION_APP_CATEGORY ac
WHERE a.App_ID=ac.APP_ID
AND c.Category_ID=ac.Category_ID
AND ac.Category_ID=<category_id for category "Games">
For your second question, you can use both APP_ID and Categor_ID as the primary key of table JUNCTION_APP_CATEGORY(note NOT TWO pks, but use the two columns together as ONE pk). So that you can put data like this:
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 1 | <-- APP_ID=1 belongs to both cat 1 & 2
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+

how to bult N Type category tree with php?

we have this table "Categoris"
------------------------------------
| ID |parent.id | Category Name |
| 1 | 0 | php |
| 2 | 1 | Jquery |
| 3 | 2 | Java script |
| 4 | 2 | HTML |
| 5 | 4 | CSS |
| 6 | 1 | word Press |
| 7 | 3 | DOM |
Output should be
PHP->Jquery
->wordpres
Jquery->java Script
->HTML
Java Script->DOM
HTML-> CSS
Word Press
DOM
Have a look at PEAR's Tree package.

Categories