开发者

How do I use PHP, JS and HTML to appropriately handle many-to-many relationship?

开发者 https://www.devze.com 2023-03-18 22:41 出处:网络
Thank you in advance for your help! I have a large and complicated form with several many-to-many relationships that I\'m struggling to deal with. I have, after A LOT of encouragement, created the tw

Thank you in advance for your help!

I have a large and complicated form with several many-to-many relationships that I'm struggling to deal with. I have, after A LOT of encouragement, created the two appropriate tables for each of those relationships (e.g. one table cars filled with carID's and carName's, and an intersecting table userCars that stores userID's and carID's), no matter the size.

My question is: should my form HTML always be generated by PHP based on those tables? For example, should I have PHP check the cars table and generate one check-box for each row in the table, even though I'll only be starting with five? Is there a better way of doing this besides calling a PHP function onLoad and having that function call a javaScript function?

Also, after the user has submitted the form, how do I store those values? Do I need to go through each POST variable name and compare them to each carName in the table cars in order to get the carID associated with that name, and then make an entry in the userCars table? Or should I just say "if Volvo, carID = 4" because I only have five cars right now!

Again thank you for all of your advice! I would have bee开发者_开发技巧n totally stuck several times without your generosity!


I agree with Marek that as soon as a php application becomes a litlle bit complicated you are better off with a framework. Preferably one like yii which offers a many-to-many ORM.

That said one of the mistakes a lot of coders make early on is to make a form too complicated.

A good question to ask yourself is whether this form is trying to do too much. It's hard to make a judgement without seeing a link to the actual form.

One way to tackle a problem like this would be to use javascript (jquery:ajax) to update the join table as the user selects a car so that when you submit the form you are only updating the values of the main table.

So a form might look like

Your Name __

Your Age __

Your Favourite Colour __

Cars You have owned

Volvo _ Ford _ Fiat_ Mercedes _ {... these values could dynamically created from an options table ...} {using jquery each value selected/unselected sends an ajax post to update your user_cars table}

When form is submitted only the user table is updated

However you could also loop through the values of $_POST['cars'][] to update the user_cars table here as well. Which is probably the better solution.


You should make your HTML generated by your database values, or you should make some Javascript that fetches the values from the database, and sets the value in the page based upon those values. Secondarily, if you set the IDs of the form elements to the IDs of the associated entries in the database, then you should just be able to update the database from the set of IDs that were included in the POST.

0

精彩评论

暂无评论...
验证码 换一张
取 消