I have two tables, Parent and child in my MySQL database. I want to make a dropdown list in my child form in Laravel so I can select a parent that already exist in my database table and I want it to be added in the child table as well. Help me please
As you have asked a question without any code, I believe you are only looking for a right nudge so here you go.
First of all, you will need to make one foreign key inside your child table for your parent table. That way parent and child tables will be connected.
Then from Controller, pass all parent ids/name pair when loading blade of child form and then loop through that data and make a dropdown with value as parent id and label as parent name, that way you can catch the parent id easily of the selected dropdown.
Next just save the data with the foreign key as the selected parent id inside your child table.
Related
I'm using the Sonata Admin and currently trying to get me to be able to change my query to every click event in the sonata_type_collection.
I have the 'Variation' entity in which it contains the collection type in the form to call the entity 'Value'.
However, this 'Value' entity contains an auto relationship to reference parent values and child values ex: (Id 1 does not contain any parent, so its field will be null, but id 2 contains a parent value, putting id 1 in the parent field parent).
The idea was that with each click that was given to add a new row of values, check the previous row and perform a new query to bring only the "child values".
Take a look into the SonataMediaBundle and SonataClassificationBundle source code it seems there is a similar case like you described.
I am trying to find the best way to handle category and sub category. I have 20 category and 50 sub category. Which of these is best way to do so :
Save data in a json file and reading content directly on client side.
Save data in database in single table and using parent id to see the relation and using foreach on result array inside another foreach of same array.
Save data in database in two table, making one sql call to parent category another one call to sub category and using parent id to see the relation and using foreach of sub category array inside another foreach of parent array.
Save data in database in two table, making one sql call to parent category and then inside its foreach making multiple sql calls to database.
I tried to find the best practice to handle categories but couldn't find any article for the same.
The solution depends purely on how complex your database schema and other entities relate to the categories. And how you intend to read the information.
The json approach would be faster, but has issues when it comes to queries that would require you to link up to category additional information.
Another approach I have used and had good performance is storing all categories in a single table. The relationships are not stored in the main table.
Another table stores the relationships as graph edges. This is quite advantageous if you have cyclic relationships within the categories. Or more than one parent.
The schema would look like :
categories ( id, name )
category_edges ( parent_id, child_id)
I used oqgraph with my implementation to get the relationships queries faster. But that was with MariaDB and not mysql.
Hope this helps.
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
I have pic related parent/child tables. For every one row in the parent table, there can be like 100 or 200 child rows in the child table. When I upgrade the child table; I also give it's parent row's tID number to child's row under thread ID column. So, there isn't more than one parent row to each child row.
If I had a column in the parent table like "LastChildTimestamp" could I upgrade related row with the last child timestamp? If so can you show me an example?
What I want to achieve is to keep the most recently updated parents upper when I list out the parents and I want to make it easy. So if you have a better solution or method I'm open to it too.
There are two different approaches you could use to solve this problem.
Create the parent.LastChildTimestamp field, then use a database trigger to update it.
See: http://www.techonthenet.com/mysql/triggers/after_update.php
In particular, you would set up after insert and after update triggers in the child table that would update the associated parent row.
Alternately, you could instead simply aggregate the data from the child rows when you need to read it. Your query would looks something like:
SELECT MAX(TIMESTAMP)
FROM child
WHERE child.threadID = (thread ID)
i have made a static drop-down menu for my e-commerce website. i have various categories and then sub categories.
my main tabs are, "CLOTHES", "FOOTWEAR", "ACCESSORIES" etc.
The clothes tab is divided into two parts one is brand wise and one is type(shirts, jeans, etc.)
Now my question is, if i go to the clothes tab and then click on shirts, how can i traverse and retrieve the records from my SQL table.
I have made the connection with the database, my table name is 'products'. The page to display products is list.php.
I am new to PHP and i know a little bit about this language.
have a pages table and categories table
also have cat_parent in the categories table
Then have a php loop to go through each topmost categories and then subcats and ......... and then pages.
watch it in action this is the site i am working a the moment
First you should have categories & sub categories in single 'categories' DB table.
In products table, you will have a category_id column as foreign key.
So based on this category_id column, you can retrieve products from your DB.
Also each product in DB should have assigned a particular category.
You have to use AJAX or a Javascript function to achieve this type of functionality. Here is how:
Use an onChange event in your static drop down menu HTML code.
Make a function in a <script> tag to catch the id of selected item.
Pass this id to the PHP file in the URL.
Give your <div> id name in this Javascript code.
After calling the PHP file catch the id from get method and pass it to the query.
You can get the desired result.