Page 1 of 1

Passing multidimensional array between forms.

Posted: Tue Jul 15, 2003 1:49 pm
by sujitha
I have a database with agencies table, contacts table and phone numbers table.
Each agency can have many contacts and each contacts can have many phone numbers.
I have a dynamic Update form for each agency that lists out all the contacts for that agency and their
phone numbers. When the user performs changes, i need these updated form variables(multidimensional arrays)
that need to be passed on to the processing page to update the database.

here is the code:

Code: Select all

<?php
$result4 = " SELECT contacts.contact_id, agencies.agency_id, last_name, first_name,
              title
FROM agencies, contacts
WHERE agencies.agency_id = contacts . agency_id
AND agencies.agency_id='$agency_id'
ORDER BY last_name";
$qresult4 = mysql_query($result4) or die(mysql_error());
<table>
 <?php while($row = mysql_fetch_array($qresult4))
{
?>
  <tr> 
    <td>&nbsp;<? echo $row ["contact_id"]; ?> </td>  
    <td width="25%" class="content"><input size="25" type="text" name="last_name" value="<? echo $row["last_name"]; ?>"></td>
	<td width="25%"><input type="text" size="25" name="first_name" value="<? echo $row["first_name"];?>"></td>
$result5 =  "select area_code, phone_number, extension, phone_type
FROM contacts, phone_numbers, phone_number_phone_types,phone_types
WHERE contacts . contact_id = phone_numbers . contact_id
AND phone_numbers.phone_number_id = phone_number_phone_types . phone_number_id
AND phone_types.phone_type_id = phone_number_phone_types . phone_type_id
And contacts.contact_id = '$row[contact_id]' ";
$qresult5 = mysql_query($result5) or die(mysql_error());

   while($row1 = mysql_fetch_array($qresult5) )
   { 
     ?><td width="5%" bgcolor="#9999ff" class="content"><input size="5" type="text" name="<? echo "$area_code[$i]" ?>" value="<? echo $row1["area_code"]; ?>"></td> 
  	<td width="15%"><input type="text" name="phone_number" size="15" value=" <?php echo $row1["phone_number"]; ?>"> </td> 
	<td width="5%"><input type="text" name="extension" size="5" value=" <?php echo $row1["extension"] ; ?>"> </td> 
	<td width="10%"><input type="text" name="phone_type" size="10" value=" <?php echo $row1["phone_type"] ;?>"></td> 
  </tr><? } }?>
</table>


?>
Here both the contacts as well as their phone numbers are dynamic, deping on the agency selected.

Please can anyone suggest on how I can retrive, store and pass these arrays on to the next page?
Any help is much appreciated.
Thanks
Sujitha

Posted: Tue Jul 15, 2003 2:26 pm
by Heavy
I hope I got you right, because I didn't understand this:
sujitha wrote:I have a dynamic Update form for each agency that lists out all the contacts for that agency and their
phone numbers. When the user performs changes, i need these updated form variables(multidimensional arrays)
that need to be passed on to the processing page to update the database.
I had a similar problem before that I solved by finding out how the serialize() function works.

I made a javascript array of what I wanted to do and passed a "hidden type form element" with the content of the javascript array serialized with a home made "javascript serialize function": when it arrived to the next page I just unserialized it, and there was my nice structure.

This is though depending on client side capabilities...

Here it is, though this was specialized for a specific javascript structure...

Code: Select all

<SCRIPT language="JavaScript1.3">
					function Adressee(Queued, UserID, AliasName, Email)&#123;
						this.Queued = Queued
						this.UserID = UserID
						this.AliasName = AliasName
						this.Email = Email
					&#125;
					
					function Reason(MailID, Reason, CreatedInitially)&#123;
						this.MailID = MailID
						this.Reason = Reason
						this.CreatedInitially = CreatedInitially
						this.DeleteMe = 0
					&#125;
					
					function Mail(MailID, Type, TopicID, TopicTxt, Comment, Adressees, Reasons)&#123;
						this.MailID = MailID
						this.Type = Type
						this.TopicID = TopicID
						this.TopicTxt = TopicTxt
						this.Comment = Comment 
						this.Adressees = Adressees
						this.Reasons = Reasons
						this.DeleteMe = 0
					&#125;
					var Sending = 0 
					var MayDelete = true
					
					<?php
						EchoMailQueueObject();
					?>

					function SaveQueue()&#123;

						//This loop converts the data array to the format of a serialized PHP array. 
						//After submit, PHP can unserialize the data and use it as a PHP Array directly.

						Str1 = "a:" + MailArr.length + ":&#123;"
						for(var a = 0 ; a < MailArr.length ; a++)&#123;
							Str1 += "i:" + a + ";a:7:&#123;"
								Str1 += 's:6:"MailID";i:' + MailArr&#1111;a].MailID + ";"
								Str1 += 's:7:"TopicID";i:' + MailArr&#1111;a].TopicID + ";"
								Str1 += 's:8:"TopicTxt";s:' + MailArr&#1111;a].TopicTxt.length + ':"' + MailArr&#1111;a].TopicTxt + '";'
								Str1 += 's:7:"Comment";s:' + MailArr&#1111;a].Comment.length + ':"' + MailArr&#1111;a].Comment + '";'
								Str1 += 's:8:"DeleteMe";i:' + MailArr&#1111;a].DeleteMe + ";"

								Str1 += 's:9:"Adressees";a:' + MailArr&#1111;a].Adressees.length  + ":&#123;"
								for(var b = 0 ; b < MailArr&#1111;a].Adressees.length ; b++)&#123;
									Str1 += "i:" + b + ";a:2:&#123;"
										Str1 += 's:6:"Queued";i:' + MailArr&#1111;a].Adressees&#1111;b].Queued + ";"
										Str1 += 's:6:"UserID";i:' + MailArr&#1111;a].Adressees&#1111;b].UserID + ";"
									Str1 += "&#125;"
								&#125;
								Str1 += "&#125;"

								Str1 += 's:7:"Reasons";a:' + MailArr&#1111;a].Reasons.length  + ":&#123;"
								for(var b = 0 ; b < MailArr&#1111;a].Reasons.length ; b++)&#123;
									Str1 += "i:" + b + ";a:2:&#123;"
										Str1 += 's:6:"Reason";s:' + MailArr&#1111;a].Reasons&#1111;b].Reason.length + ':"' + MailArr&#1111;a].Reasons&#1111;b].Reason + '";'
										Str1 += 's:8:"DeleteMe";i:' + MailArr&#1111;a].Reasons&#1111;b].DeleteMe + ";"
									Str1 += "&#125;"
								&#125;
								Str1 += "&#125;"
							Str1 += "&#125;"
						&#125;
						Str1 += "&#125;"
						document.getElementById('SerializedData').value = Str1
						document.Form1.action += '?WhatNow=Save'
						return true;
					&#125;
</script>
This was about an email queueing system for automatic sending where every user could change the content and adressees of every email and also delete mails if they thought they were unneeded.

It was like this:
A topic has changed in some way. So its ID goes into a db table called "Mail".
Then the reasons for the mail, ie why the mail exists, could be many, so that has another db table called Reasons.
Then there are the users that should recieve the mail in table MailAdressees.

I let PHP write all this info to the client side as an object in javascript, see the php tag above, and when all the client side maneuvers where done, I called the SaveQueue() function and the posted the result...

This ugly function converts all this multidimentional data to a serialized string that is passed to PHP via POST so that php can unserialize() it and have the same structure at the server side.

Maybe you can do something similar?
Just know that you need to know exactly how the serializer works in PHP and do exactly the same way.