"self-referencing" table and arrays - php

MySQL table:
categoryID
categoryName
categoryParent
Every category has ONE parent category, though it can be NULL, which I treat as the root-category.
I want to get all categories from the table, store it in an array and print it in a way, that shows the nesting.
Example:
ID name parent
1 a NULL
2 b NULL
3 c NULL
4 b1 2
5 d NULL
6 b2 2
HTML:
a
b
-b1
-b2
c
d
Later I'll try to make it draggable with jQuery so the user can choose the parent/child category by him-/herself.
Can I do all this with one single table or do I need an external junction table?

Your table structure is fine.
You'll render the nesting when you deal with the results; perhaps you loop through each result with no parent and append to the DOM, then loop through each result with a parent appending to existing elements. That only works for a two-level tree, but you get the idea.

Yes you can, just select all the rows from table. And using php, you can generate a good html table using a for loop. When you see an element with a parent, just process the table string you are constructing in your php script. From there using JQuery you can change the orders and parent child relationship of the table.

You can nest arrays and have a structure like (Array of (me, childrenArray)).
That way you use the "me" for indexing.

Related

How to import data from excel file and identify subcategory

I am making the function of importing data from the excel file with the following structure
i want loop the datas of excel then insert to categories table and determined subcategory by value of first column. Eg: 101 is sub category of 1, 101001 is sub of 101,...
I have been thinking for a long time and looking for it but not yet. Hope the helping.
Use PHP Spreadsheet https://phpspreadsheet.readthedocs.io/en/latest/
Reading from a file: https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-files/#reading-files
However you parse the cat/subcat, you would want to insert it back into the table with the database ID of the parent.
To determine the parent, start with the first row, and keep it in a parents array. When you hit the next row, check the parents array if the value exists. You will have to check char by char. Then add every new category to the parents. This structure is not really ideal, what happens when PHP becomes a parent, and so on. PHP should be 10101 at any rate to follow the structure. Ruby 10102
In all honesty, you don't need second half of the number, only the parent. Just use the row id.
1, , Programming
2,1,OOP
3,2,PHP
What you are missing here is the row id. The last part is only the "sort" and can just be removed. Imagine if the column was a fixed length field which would be "easier" to parse. 3 digit for parent, and 3 digits for the sort. Start parsing from the top ..
000001 Programming
001001 OOP
What is PHP now? You aren't pointing to 001 (OOP) cause 001 is also Programming. So adding a row id, you can now point to the id that will be entered into the DB.
1 null 1 Programming # id parent sort name
2 1 1 OOP
3 2 1 PHP
4 2 1 Ruby
5 1 2 Functional

Echo from two tables and two ids in array

I have a list of products, which can have different properties (color, size etc).
Properties are saved in my db as property table in the following form:
While the chosen properties get saved to each individual product in the form of parentid.id, for example this 2nd entry (id 4) has blue color property.
But what I'm trying to achieve now, is to echo these selected values on the product, the same way they are saved, so:
Parent Title - This id (child) or based on example: Color - Blue.
But I can't figure out, how to. I've tried imploding the properties array, but then I only get the last part, so only 2 from 1.2 and echoes Blue. I would need both, 1 and 2 (Color-blue).
It's kind of hard to explain, but if anyone understood at least a bit what I would need and has any idea how this could be done, I would be very grateful for any tips and hints.
Thanks in advance ;)
What you need to do to make this work:
Select your properties from db
Parse them into groups (explode by ',' to work with them one at a time)
Build an array of parent/children by parsing the groups
(ex: groupings = [
parent1 => [child1, child2, etc],
parent2 => [child1, child2, etc],
])
a. loop through each group
b. explode by '.' to get the parent/child pairing
c. put into array
Build your where clause
a. Parents: where (id in (parent1, parent2, etc))
b. Children: (parent = parent AND id IN (child1, child2, etc))

Concerning parent/child array relationship

This is my database structure:
I would like to loop through all the child elements (those who's parent_id is set to the data_id within the same table).
Therefore those with "parent_id" as "0" are parents, and those with parent_id with a number in it is a child.
I would like help creating a function that will generate tables, with the table itself as the parent, and all the rows within it would be children of that parent. There is only one layer of depth in this project (one parent and one child layer always. ).
Can anyone help, or would want a more detailed description?
Thank you, and looking forward to a reply.
From my perspective, as when I look at your database, I assume that your parent will always be added before your child is (If I am wrong, please correct me). And because you said that you only have 1 parent and 1 child, I believe this is how your database would look like:
1st - parent
2nd - child of 1st
3rd - parent
4th - child of 3rd
If that's the case, one single loop can help you out with this pseudo code
//get the data from the database
//run through the loop
//check if the parent_id is 0
// if it is, create an element (a table) to be the container with the id as cited in your data_id
// if is is not, create an element (a row). Then append this element to the table with the same id as this parent_id

Parent/Child MySQL query with CASE/WHEN or switch

I’m using PHP to dynamically add another table to a page based on a parent/child relationship.
I’ve greatly simplified things so lets suppose I have a table 'FAMILY' with three columns Parent Int(6), Child varchar(15), and Fname varchar(25). The table has four rows, whose values are;
Row 1 : 4, NULL , Bill
Row 2: 5, 4, Tom
Row 3: 6, NULL, Frank
Row 4: 7, 4, Sam
As you can tell Tom and Frank are children of Bill. And so Bill is the parent of Tom and Sam.
Regardless of whose record I’m currently looking at, I would like to be able to dynamically see the other associated records as well. Forget the PHP part, I have that working. I just need the MySQL query.
The way I see it, I need one query that will do two different things depending on the value of child.
1: If Child is NULL (or blank) as in Row 1, I need it to return Row 2 and 4 based on the parent value of 4. In other words I’m looking at a Parent.
2) If Child is NOT NULL, as in Rows 2 & 4, I need it to return row 1 and the other row 2 or 4 depending which child I was looking at. Now I’m looking at a child record.
SELECT * FROM FAMILY WHERE
CASE WHEN Child IS NOT NULL then parent = 4
WHEN child IS NULL then child = 4
END
GROUP BY PARENT
I’m pretty sure that I’m using CASE/WHEN incorrectly but after trying about a thousand things I give up and need to ask for help.
Please a MySQL query that can evaluate if it’s a parent or child and then pull the appropriate row(s) would be very much appreciated.
Thanks in advance.
Not much sure from your question statement but you can modify your WHERE condition to be like below
WHERE (Child IS NOT NULL AND parent = 4)
OR COALESCE(child, 4) = 4

Storing a nested navigation menu in MySQL

I would like to store a nested associative array in MySQL. I need this for a nestable navigation menu on my site. There should be no limit to the level of nesting.
I have researched the nested set model and the adjacency list, but am unsure of which to use for my needs.
Ideally, I will be able to query my table in a way that I can reconstruct my associative array in PHP and then use this to construct my navigation menu.
Records may be added / changed / re-ordered, but this will only happen infrequently.
I am looking for an example table structure for MySQL (InnoDB), queries to get and re-order the records, and update, delete and add new records. I am using PDO in PHP so any example code of how to turn the record set into the associative array would also be helpful.
Try the following design:
Column 'BEFORE'
Column 'AFTER'
Column 'NAME'
Column 'ID'
Now, just store ID as required.
Example: The navigation menu might be
A --> B --> C
Just give an ID to each entry in the menu and store it with an unique ID.
Then enter the BEFORE and AFTER values as IDs into the table.
So, for B we have
Entry for B:
BEFORE = ID(C)
AFTER = ID(A)
NAME = 'B'
This can be easily expanded for multiple nested menus.
I would suggest following design for implementing the solution.
Table : Navigation
Columns :
ID (INT NOT NULL IDENTITY),
Description (VARCHAR),
ParentID (INT NULL) - Refer its own table ID column,
AdditionalColulmns..

Categories