Creating form for many-to-many relationship

Thread Solved

Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Creating form for many-to-many relationship

 
0
  #1
Jun 9th, 2009
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
register.JPG  
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 68
Reputation: djjjozsi is an unknown quantity at this point 
Solved Threads: 10
djjjozsi djjjozsi is offline Offline
Junior Poster in Training

Re: Creating form for many-to-many relationship

 
0
  #2
Jun 9th, 2009
there is no need to use AJAX if the author / category aren't in relation.
many to many relations should stored with connector tables.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 954
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 126
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: Creating form for many-to-many relationship

 
0
  #3
Jun 9th, 2009
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).
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 68
Reputation: djjjozsi is an unknown quantity at this point 
Solved Threads: 10
djjjozsi djjjozsi is offline Offline
Junior Poster in Training

Re: Creating form for many-to-many relationship

 
0
  #4
Jun 9th, 2009
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:


  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Re: Creating form for many-to-many relationship

 
0
  #5
Jun 10th, 2009
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..

Originally Posted by ardav View Post
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).
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Re: Creating form for many-to-many relationship

 
0
  #6
Jun 10th, 2009
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.

Originally Posted by djjjozsi View Post
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:


  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. ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 954
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 126
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: Creating form for many-to-many relationship

 
0
  #7
Jun 10th, 2009
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.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 68
Reputation: djjjozsi is an unknown quantity at this point 
Solved Threads: 10
djjjozsi djjjozsi is offline Offline
Junior Poster in Training

Re: Creating form for many-to-many relationship

 
0
  #8
Jun 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Re: Creating form for many-to-many relationship

 
0
  #9
Jun 14th, 2009
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, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Re: Creating form for many-to-many relationship

 
0
  #10
Jun 15th, 2009
Hi,
No one experienced the problem I mentioned?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC