Echo from two tables and two ids in array - php

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))

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

Nested Category display in Symfony2 with 4 level

I have following table structure store category
When I wiil start to create my form, I have add first dropdown for lvl 1 category by default, now i want to another dropdown for sub-category which based on category, then I want 3rd lvl sub-category which are based on 2nd lvl selected sub-cateogry list.
I have only store 4 levels, Any Idea about how can build this structure on Symfony2.3?
Any Possibility to make above functionality manage in symfony2.3 or need to change in SQL table structure?
If you're using an entity to access your table, this extensions seems to be made for what you're trying to achieve:
https://github.com/Yavin/symfony-form-tree
You might have to modify your table structure slightly, though.
If you're sure you'll never have more than 4 levels, you could have 4 nested foreach cycles and produce an array similar to:
$categories = array("Electronics" =>
array("Television" =>
array("LED" =>
array(7 => "X SERIES",
8 => "TRILUMINOS")
)
)
);
and then pass it into your form like so:
$form = $this->createFormBuilder()
->add('category', 'choice', array(
'choices'=> $choices
))
I would solve it like this:
don't save redundant data or optimize, so remove 'level' and let's solve it for any depth. Limitations are often more complicated and bad code than solving the whole problem. If you don't want more levels, than just check the level when inserting a child, but when designing the model/entity layer, think about 0,1,n relations.
save the parent id as nullable integer foreign key, so a root category is defined by having parent id NULL and a leaf is defined by not being parent to any other category. This 1:n relation means parent id is the owning side, and a pseudo field 'children' (ArrayCollection) is the reversed side, that's doctrine.
The idea:
The form and request data is just the 'current category'. There is only one path from the current selected category to its root, so we don't need more data.
The validation of the form is simply a check: if category is a leaf, than the form is valid.
The model-view transformer takes a category and transforms it into its children. NULL at first is transformed into the root categories (something like CategoryRepository::findRoots()), and any valid category is transformed into its children (e.g. Category::getChildren()). You have to declare your transformer as a service since it depends on the database.
So use 'entity' as form type so we don't need to write our own view transformation. The entity form type is actually a choice form type and takes an array of items, but is now taking a single category and showing its children.
And please don't use any validation logic in the controller. An action like http://symfony.com/doc/current/book/forms.html#handling-form-submissions works for you. Let the isValid method invoke your validators via a constraint like #Leaf in your data object (like this example: http://symfony.com/doc/current/book/forms.html#embedding-a-single-object).
And don't forget
to hide items with status = 0
remember maybe there is a parent with all children having status = 0, so check that there are no children with status != 0 and not just if it's empty
There are so many exception when dealing with forms. I really recommend to write tests (but also for other parts ;) )!

"self-referencing" table and arrays

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.

Should the father know about the children or vica versa?

Should the father know about the children or vica versa?
That is, in a situation where I have two kinds of objects (items and categories), each has its own characteristics and fields and we have a relation between them that every item has a category he is in.
I want to ask in two ways about what should happen, in theory and in php + mysql.
Should the categories know about all the items that are in it, or should every item know about its category?
In php + mysql the exact question is should I for every item in its row save its category's id, or should I create a special table for this relation (each row contains a category id and an item id)?
Best practice is the children know about the parents (ex: parent_id). The alternative is not scalable, and very taxing to your system. You could easily run a query to FIND the children though - SELECT * FROM items WHERE item_category = x. The reason for this is when you add or delete items, the category is not affected. It doesn't become a different category because you assign an item to it or remove an item from it, so it shouldn't care either way which items are assigned to it.
This is different from a tagging setup. In the one-to-many category assignment method, items would fall into a single "category" but might have many "features". Tagging features is a many-to-many relationship and would require the mapping table you mentioned. Assigning parent categories requires only a single "parent" field in the items table.

How to Handle Consuming Lots of Data from Multiple Sources in a Web SIte

This is a "meta" question that I am asking in a effort to better understand some tough nuts I've had to crack lately. Even if you don't get precisely what I'm reaching for here or there is too much text to read through, any practical input is appreciated and probably useful.
Assume you have a website that needs to use data that is stored in multiple tables of a database. That data will need to be iterated through in a multitude of ways, used for calculations in various places, etc.
So on a page that needs to display a collection of projects (from one db table) that each contain a collection of categories (from another db table) that each contain 1 or more items (from another db table) what is the best way to gather the data, organize it and iterate through it for display?
Since each project can have 1 or more categories and each category can have one or more items (but the items are unique to a specific category) what's the best way to organize the resulting pile?
My goal in the below example is to generate a table of projects where each project has the associated categories listed with it and each category has the associated items listed with it but I also need to aggregate data from the items table to display next to the project name
A Project Name (43 items and 2 of them have errors!)
- category 1
- item 1
- item 2
- category 2
- item 1
Another Project Name (12 items and no errors)
- category 1
- item 1
- category 2
- item 1
What I did was to retrieve the data from each table and stick it in a variable. Giving me something like:
var $projects = array("id" => 1, "proj_id" => 1, "name" => "aname");
var $categories = array("id" => 1, "cat_id" => 1234, "proj_id" => 1, "cat_name" => "acatname");
var $items = array("id" => 1, "item_id" => 1234, "location" => "katmandu");
Then I went through the variables in nested foreach() loops building the rows I needed to display.
I ran into difficulties with this as the foreach() loop would work fine when building something 2 levels deep (associating categories with projects) but it did not work as expected when went three levels deep (I N C E P T I O N .. hah, couldn't resist) and tried adding the items to each category (instead adding all of them to one item... first or last I don't recall which). Also, when something was present in the third level of the array, how would you add up that data and then get it out for use back up in the top level of the array being built?
I suppose I could have constructed a mega SQL query that did it all for me and put everything into a single array, saving me the loop confusion by flattening it out, but... well, that's why I'm here asking you all.
So, I suppose the heart of this question is: How do you handle getting lots of data from different tables and then combining it all for display and use in calculations?
Sounds like you're going to want to use SQL JOINs. Consider looking into them:
http://www.w3schools.com/sql/sql_join_left.asp
They'll pull data from multiple tables and aggregate it. It won't produce quite what you're looking for, but it will produce something that you can use in a different way.
is Hadoop the sort of thing you're looking for?

Categories