943,923 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1821
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 9th, 2009
0

Creating form for many-to-many relationship

Expand Post »
Hi everyone,
I have a registration form for the book, which has two many to many relationships with Author and Category, So I don't know what is the best way to implement and to be user friendly.

If there is an example, please let me know and also share your idea...

NOTE:
I think AJAX would be suitable, but I wanna know how?
See attachment
Attached Thumbnails
Click image for larger version

Name:	register.JPG
Views:	54
Size:	35.1 KB
ID:	10408  
Similar Threads
Reputation Points: 8
Solved Threads: 1
Posting Whiz in Training
tanha is offline Offline
217 posts
since Aug 2007
Jun 9th, 2009
0

Re: Creating form for many-to-many relationship

there is no need to use AJAX if the author / category aren't in relation.
many to many relations should stored with connector tables.
Reputation Points: 12
Solved Threads: 11
Junior Poster in Training
djjjozsi is offline Offline
69 posts
since Jun 2009
Jun 9th, 2009
0

Re: Creating form for many-to-many relationship

main tables
========
author table
category table
book table (neither author nor category info, as books can have multiples of both)

link tables
=======
author_book (e.g. id; book_id; author_id)
category_book (e.g. id; book_id; category_id)

Therefore as quoted above, no need for many to many tables nor ajax in form. However, you'll need inner joins on your SQL statements and a multiple select listbox for authors and categories. Perhaps AJax would be useful for selecting multiple authors or adding authors to a form (like a new dynamic list when adding attachments to an email).
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 948
Sarcastic Poster
ardav is offline Offline
6,698 posts
since Oct 2006
Jun 9th, 2009
0

Re: Creating form for many-to-many relationship

If you then make an update code, on the internet you cannot find a good trick to list selected properties from a many-many relation connector table

i've set the table names with variable names, to make this code re-useable:


php Syntax (Toggle Plain Text)
  1. <?php
  2. include("dbconn.php");
  3. $filename="books_update.php";
  4.  
  5. $table1 = "books"; //this table woulnd not used in this code, becouse the updater code removed
  6. $idfield1 = "book_id"; //this ID field woulnd not used in this code, becouse the updater code removed
  7.  
  8. $filename="books_update.php";
  9.  
  10. /* many relations*/
  11. $idfield2 = "author_id";
  12. $table2 = "author";
  13. $fieldname_2 = "author_name";
  14.  
  15. /* connector table to handle many properties*/
  16. $conntablename = "author_book";
  17. $connector_id1 = "book_id";
  18. $connector_id2 = "author_id";
  19.  
  20.  
  21. if ( isset( $_GET["edit"] ) ) {
  22. /*
  23. You need to select the other field's values from the books table...
  24. */
  25.  
  26. /* Build a select box from author based on the data from the connector table*/
  27. $sql2 = sprintf( "
  28. SELECT `$idfield2`,`$fieldname_2`,if($table2.$idfield2 IN (select $connector_id2 from $conntablename where $conntablename.$connector_id1 =%d),1,0) AS is_in FROM `$table2`
  29. ;" , $_GET["edit"] );
  30. // print $sql2."<br />";
  31. print '<form method="post" action="'.$filename.'">';
  32. print "<input type=\"hidden\" name=\"id\" value=\"" . $_GET["edit"] . "\">";
  33. echo "<select size=\"5\" multiple=\"multiple\" name=\"ch[]\">";
  34.  
  35. $result = mysql_query( $sql2 );
  36. while ( $rows = mysql_fetch_assoc( $result ) ) {
  37. $selected = empty( $rows["is_in"] )?"":" selected=\"selected\"";
  38. echo "<option value=\"{$rows["$idfield2"]}\"{$selected}>".htmlspecialchars($rows["$fieldname_2"])."</option>\n";
  39. }
  40. print "</select>\n";
  41. /* Select ends...*/
  42.  
  43. /* other fields ... */
  44. print '<input type="submit" name="submit" value="submit">';
  45. print "</form>";
  46. die();
  47. }
  48. ?>
Last edited by djjjozsi; Jun 9th, 2009 at 2:26 pm.
Reputation Points: 12
Solved Threads: 11
Junior Poster in Training
djjjozsi is offline Offline
69 posts
since Jun 2009
Jun 10th, 2009
0

Re: Creating form for many-to-many relationship

Thanks for reply,
The tables are really what I have, but I think I need to use AJAX, because I have registered authors and also registered category in the list boxes, so what if I am in need to have new Author or Category, then I need to insert in place and then new author and category should be available in list boxes without page reloading.

I hope I could state what I meant..

Click to Expand / Collapse  Quote originally posted by ardav ...
main tables
========
author table
category table
book table (neither author nor category info, as books can have multiples of both)

link tables
=======
author_book (e.g. id; book_id; author_id)
category_book (e.g. id; book_id; category_id)

Therefore as quoted above, no need for many to many tables nor ajax in form. However, you'll need inner joins on your SQL statements and a multiple select listbox for authors and categories. Perhaps AJax would be useful for selecting multiple authors or adding authors to a form (like a new dynamic list when adding attachments to an email).
Reputation Points: 8
Solved Threads: 1
Posting Whiz in Training
tanha is offline Offline
217 posts
since Aug 2007
Jun 10th, 2009
0

Re: Creating form for many-to-many relationship

Hi and Thanks for reply,
Actually to have the list of author and category, I have no problem, because I have info and do the query and fill the list boxes according to the query, BUT

The problem is that if there is no author or category available, then I want to add them in place, not to go another page to add ones and then come back to book registration page and to see the author or category available.

Click to Expand / Collapse  Quote originally posted by djjjozsi ...
If you then make an update code, on the internet you cannot find a good trick to list selected properties from a many-many relation connector table

i've set the table names with variable names, to make this code re-useable:


php Syntax (Toggle Plain Text)
  1. <?php
  2. include("dbconn.php");
  3. $filename="books_update.php";
  4.  
  5. $table1 = "books"; //this table woulnd not used in this code, becouse the updater code removed
  6. $idfield1 = "book_id"; //this ID field woulnd not used in this code, becouse the updater code removed
  7.  
  8. $filename="books_update.php";
  9.  
  10. /* many relations*/
  11. $idfield2 = "author_id";
  12. $table2 = "author";
  13. $fieldname_2 = "author_name";
  14.  
  15. /* connector table to handle many properties*/
  16. $conntablename = "author_book";
  17. $connector_id1 = "book_id";
  18. $connector_id2 = "author_id";
  19.  
  20.  
  21. if ( isset( $_GET["edit"] ) ) {
  22. /*
  23. You need to select the other field's values from the books table...
  24. */
  25.  
  26. /* Build a select box from author based on the data from the connector table*/
  27. $sql2 = sprintf( "
  28. SELECT `$idfield2`,`$fieldname_2`,if($table2.$idfield2 IN (select $connector_id2 from $conntablename where $conntablename.$connector_id1 =%d),1,0) AS is_in FROM `$table2`
  29. ;" , $_GET["edit"] );
  30. // print $sql2."<br />";
  31. print '<form method="post" action="'.$filename.'">';
  32. print "<input type=\"hidden\" name=\"id\" value=\"" . $_GET["edit"] . "\">";
  33. echo "<select size=\"5\" multiple=\"multiple\" name=\"ch[]\">";
  34.  
  35. $result = mysql_query( $sql2 );
  36. while ( $rows = mysql_fetch_assoc( $result ) ) {
  37. $selected = empty( $rows["is_in"] )?"":" selected=\"selected\"";
  38. echo "<option value=\"{$rows["$idfield2"]}\"{$selected}>".htmlspecialchars($rows["$fieldname_2"])."</option>\n";
  39. }
  40. print "</select>\n";
  41. /* Select ends...*/
  42.  
  43. /* other fields ... */
  44. print '<input type="submit" name="submit" value="submit">';
  45. print "</form>";
  46. die();
  47. }
  48. ?>
Reputation Points: 8
Solved Threads: 1
Posting Whiz in Training
tanha is offline Offline
217 posts
since Aug 2007
Jun 10th, 2009
0

Re: Creating form for many-to-many relationship

THat's no problem - you can add an author - send the form - add an author to the db - use the mysql_insert_id() to get the new author id from the db and use it in your SQL query to insert a book.

However, things get a little more complicated if you want to add multiple new authors.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 948
Sarcastic Poster
ardav is offline Offline
6,698 posts
since Oct 2006
Jun 10th, 2009
0

Re: Creating form for many-to-many relationship

this is a really interesting question

you can find this useful.
http://phpcode.hu/teszt/ajax_listbox/
this is a part of a module from a form editor program, its a limited/standalone version.

Lets have a look at the generated code first, and try to intergrate it to your project. You just need to insert the category table's details, and you can get an ajax listbox and new item management too. If you generate two codes, there will be code parts which really makes the same.
i've tried to integrate a solution to build multiple listboxes at a same time, but its really easy to integrate the codes manually. This assumes that you already have the 'relation table', where the values populated.

Don't forget to set the correct unique KEYS on your fields to remove the duplications.

hello, jjozsi.
Last edited by djjjozsi; Jun 10th, 2009 at 4:52 am. Reason: addition ideas+explanation
Reputation Points: 12
Solved Threads: 11
Junior Poster in Training
djjjozsi is offline Offline
69 posts
since Jun 2009
Jun 14th, 2009
0

Re: Creating form for many-to-many relationship

Hi,
Thanks for replying...

I found the solution, and it would be very nice and easy if we use the Prototype Java SCript Framework.

I did and it is working, but there is small problem, when I add a new category I see the new category in the list without page reloading or refreshing, BUT when I add new author I don't see the new author in the list, I dont know why?
I hope those who are experienced and use that framework help and guide me here.

Thanks.
Attached Files
File Type: zip book.zip (24.6 KB, 33 views)
Reputation Points: 8
Solved Threads: 1
Posting Whiz in Training
tanha is offline Offline
217 posts
since Aug 2007
Jun 15th, 2009
0

Re: Creating form for many-to-many relationship

Hi,
No one experienced the problem I mentioned?
Reputation Points: 8
Solved Threads: 1
Posting Whiz in Training
tanha is offline Offline
217 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: show search results on same page
Next Thread in PHP Forum Timeline: Help with Dynamic Forms





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC