Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arrays from php 2

Status
Not open for further replies.

sylve

Programmer
Jan 18, 2005
58
0
0
CA
I have two select objects. When the first changes, i would like to write a procedure that updates the options in the second.

The data is stored in a multidimentionnal array generated by php. The output looks something like this:
Code:
Array ( 
[Fruits] => Array ( [0] => Apples [1] => Bananas [2] => Oranges   ) 
[Veggies] => Array ( [0] => Peas [1] => Carrots ) 
)

I can get the second listbox to empty and add dummy data, but i can't figure out how to keep the array in memory. I'm getting confused, is this even possible?

Code:
<script type="text/javascript" language="Javascript">
<!-- //
	function update_list(myform, array_list)
	{
		myform.selectCtrl.length = 0;
		myform.selectCtrl.options[0] = new Option ('Aucun','none');
		for (var i = 1; i <= array_list.length; i++)
		{
			myform.selectCtrl.options[i] = new Option(array_list[i-1],array_list[i-1]);
		}
				
	}

// --></script>

where selectCtrl is the name of the second select. Can anybody suggest something?
 
Sorry if this solution seems a bit simplistic, but since your javascript code looks fine this was my only guess.

Does the array reside in a PHP variable? If this is the case, you won't be able to pass a PHP array to a javascript function. The reason being that the PHP array resides on the server and the javascript function resides on the client. In order to pass the array to the javascript function you must first pass the data from the PHP array to a javascript array. Let me preface this by saying that I don't use PHP, so I'll give you an example in ASP using JScript. However, the solution should be similar.

Code:
<%
var aspArr = new Array("1", "2", "3");
%>
<script language=javascript>
var clientSideArr = new Array();
clientSideArr[0] = <%=aspArr[0]%>;
clientSideArr[1] = <%=aspArr[1]%>;
clientSideArr[2] = <%=aspArr[2]%>;
</script>

-kaht

Do the chickens have large talons?
[banghead]
 
Real good, kaht. :)# With the original example, maybe something like:

Code:
<?php
$somearray = Array ( 
[Fruits] => Array ( [0] => Apples [1] => Bananas [2] => Oranges   ) 
[Veggies] => Array ( [0] => Peas [1] => Carrots ) 
) 

echo "<script language=javascript>\n";
echo "var clientSideArr = new Array();\n";

$cindex = 0;
foreach ($oneelement in $somearray)
  {
  echo 'clientSideArr[' . $cindex . ']=new Array("';
  echo $oneelement[0] . '", "';
  echo $oneelement[1] . '", "';
  echo $oneelement[2] . '");';
  echo "\n";
  $cindex++;
  }

echo '</script>';
?>

Lee
 
Thanks Lee, I don't know squat about PHP [lol]

-kaht

Do the chickens have large talons?
[banghead]
 
PHP can use associative arrays like JS can. There are a lot of similarities, so it's not that difficult to learn if you know JS. I miss the object-oriented things you can do in JS so easily; objects are relatively new in PHP, so not all servers out there have updated, I've found.

Before I actually learned anything about PHP, I visited the forum and answered a few questions. Got one star out of it, too. :)#

Lee
 
You gotta love getting st*rs for valuable posts [lol]

-kaht

Do the chickens have large talons?
[banghead]
 
Especially when you don't know the language! It just shows how similar the principles of programming are, no matter what the language. Anyone who learns a couple of unrelated languages well will have a good idea how to pick up on others.

Lee
 
Thank you so much both of you! Code seems much clearer now. Here was my solution:

Code:
echo "<script type=\"text/javascript\" language=\"Javascript\">\n<!--";
		
			echo "\nfunction update_list(myform,program)\n{\n";
			echo "var array_list = new Array();\n\n";
			
			//php: transfer array from server to client side
			$i = 0;
                        foreach ($project_list as $sub_array)
                        {
                        	echo "array_list[" . $i . "]=new Array();\n";
                        	$y = 0;
                          	foreach ($sub_array as $var)
                          	{
                        		echo "array_list[". $i ."][".$y ."] = '". $var ."';\n";
                        		$y++;
                        	}
                        	echo "\n";
                        	$i++;
                        }
                                                
                        //start javascript again
			echo "							
			myform.master.length = 0;
			myform.master.options[0] = new Option ('Aucun','none');
			for (var i = 1; i <= array_list[program].length; i++)
			{
				myform.master.options[i] = new Option(array_list[program][i-1],array_list[1][i-1]);
			}
				
			}\n
		// --></script>\n";
 
Good job sylve, glad you got it working.

-kaht

Do the chickens have large talons?
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top