Managing Many2Many with PHP/JS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi guys: 

Thanks in advance for your help. I am newby in this
PHP stuff and I've got quite a problem here, and I?ve
spent several days (and nights) trying to solve this
problem. 

I am trying to manage a many to many relationship in a
web database application. I have three tables:
tbl_project (which contains the name of a development
project), tbl_Contraparte(which contains the name of
the organisations which manage the project) and
tbl_ProyOrg (which contains two fields: proy_id and
OrgSigla) and is the join table for the many2many
relationship. 

I have created a form for the tbl_project table with
all the data referring to the project. Into this form
I´ve personalized some code (I have fetched from
internet) to create a table which dynamically creates
new rows into the tbl_ProyOrg table (in the client
side using JavaScript) with a drop down box, so the
page do not needs to reload every time the new row is
created.

As you will see my problem stands in populate the
select box with a query from a MySql database made
using PHP into the JavaScript function. 

You can find the code following: 

Thanks a lot

Best regards, 

Alvaro. 

OS: Linux Debian Sarge
Apache: 2.0
PHP: 4.3.10-2
Mysql: 4.1.8

<!--Here starts the code-->

<?php
$hostname_monitoreo_conn = "host";
$database_monitoreo_conn = "database_name";
$username_monitoreo_conn = "username";
$password_monitoreo_conn = "password";
$monitoreo_conn =
mysql_pconnect($hostname_monitoreo_conn,
$username_monitoreo_conn, $password_monitoreo_conn) or
trigger_error(mysql_error(),E_USER_ERROR); 
?>

<?php 
mysql_select_db($database_monitoreo_conn,
$monitoreo_conn);
$query_rs_contraparte = "SELECT orgSigla FROM
tbl_02Contraparte ORDER BY orgSigla ASC";
$rs_contraparte = mysql_query($query_rs_contraparte,
$monitoreo_conn) or die(mysql_error());
$row_rs_contraparte =
mysql_fetch_assoc($rs_contraparte);
$totalRows_rs_contraparte =
mysql_num_rows($rs_contraparte);
?>

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
 	
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd";>
 <html>
 	<head>
 		<link rel="stylesheet" href="monitoreo.css"
type="text/css" />

 		<title>Projects by organisation</title>
 	</head>
 	<body>

 		<h4>Projects by organisation</h4>
 		<form action="./ins_ProyOrg.php" method="post"
enctype="multipart/form-data">

Project_Id: <input name="tf_proyId" type="text"
id="tf_proyId" />

<p><a href="javascript:add_row()">Add an
organisation</a></p>

<!-- Here begins the function to create a new row for
a new organisation-->

<script type="text/javascript">

 var rows = 1;
 function add_row() 
 {
 	var counter;
 	var ptable = document.getElementById('tbl_ProyOrg');
 	
 	var trow = document.createElement('tr');  //creates
the tag for a a new table row
 	trow.setAttribute('id', 'r' + rows); //Here IE 6.0
reports an error, but it works in Firefox1.0 and Opera

 	var ptd = new Array(4);
	
	var arKind = Array('input', 'select', 'input');
//defines the kind of control
 	var arNames = Array('proyId[]','orgSigla[]','none');
//defines the name of the control
 	var arTypes = Array('hidden','text','button');
//defines de type of the control
 	for (counter = 0; counter < arTypes.length;
counter++) //Loop wchich creates one by one the
elements of the table
 	{
 		ptd[counter] = document.createElement('td');
 
 		var p = document.createElement('span')
 		var pinput =
document.createElement(arKind[counter]);

 		pinput.setAttribute('name', arNames[counter]);
 		pinput.setAttribute('type', arTypes[counter]);
 		pinput.setAttribute('id', arNames[counter]);

/*
THE MAIN PROBLEM IS HERE (at least I guess so :) ). I
am trying to give the option values to the select
control, 
from the query listed at the beginning of the file. I
suposse the big problem is in the array definition
but I have no idea of how to build a proper array.  

		if (arKind[counter] == 'select')
		{
		var num_rec = "<?php echo $totalRows_rs_contraparte;
?>";
		var list_rec = "<?php echo $row_rs_contraparte; ?>";
		var source_combo = new Array(num_rec);
			for (i=0, i < num_rec, i++)
				{
				source_combo[i]= list_rec[i];
				}
		pinput.options(source_combo[i]); 
 		}
*/

 		if (arTypes[counter] == 'button') 
 		{
 		pinput.setAttribute('value', 'x');
 		pinput.onclick = removeNode;
 		pinput.rId = 'r' + rows;
 		}
//This is the part which actually creates the table
row
 		
 		p.appendChild(pinput);
 		ptd[counter].appendChild(p);		
 		trow.appendChild(ptd[counter]);
 	}
 
 	ptable.appendChild(trow);
 	rows++;
 }

//This is the function to delete the row through the
button. 

 function removeNode()
 {
 	var ptable = document.getElementById('tbl_ProyOrg');
 
ptable.removeChild(document.getElementById(this.rId));
 	rows--;
 }
 			-->
 			</script>
	<table>
 			<thead>
 				<tr>
 				    <th>Proyecto</th>
 				    <th>Organización</th>
 				    <th>Eliminar</th>
 				</tr>
 			</thead>
		<tbody id="tbl_ProyOrg">
	    </tbody>

</table>
 		    <p><input type="submit" value="Add Record"
name="submit" /></p>

 		</form>
 	</body>
 </html>



	
	
		
___________________________________________________________ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux