Creating query with multi-dimensional array

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

 



I have a form that allows users to edit an entry in a database. It displays the current values in a textbox/select box/textarea depending on the type of data (text, enum, etc). The same form is used for different tables, so no values are hard coded. Field names are retrieved with a show columns query and then content is retrieved with a select query.

This works fine for almost all fields, I am having trouble with the field that is of type set.

I build the REPLACE query by listing the values entered into the form. The first thing I do is loop through the $_POST variable and create two arrays - one with the keys, and one with the values. So the variable $vals is an array that holds the data the user has entered, and in theory the REPLACE statement could be REPLACE INTO table ($key) VALUES ($vals), if I could list the values of $keys and $vals as follows: (field1, field2, field3) VALUES ('data1', 'data2', 'data3').

I wrote the following function to loop through the array values and list them as needed for the query (separated by a comma, surrounded by single quotes - yes, I know the quotes haven't been added yet):

function print_array($arr) {
//This function takes an array and lists all values in a comma-separated list
//for easy printing in a query or web page
$count = count($arr);
$last = $count - 1 ;
for ($i = 0; $i < $count; $i++) {
if (is_array($arr[$i])) {
/************************************PROBLEM!!!!!!!!!!!!***********************************/
$count2 = count($arr[$i]);
$last2 = $count2 - 1 ;
for ($j = 0; $j < $count2; $j++) {
$list .= "$arr[$i][$j]";
}
/************************************PROBLEM!!!!!!!!!!!!***********************************/


	} else {
	   $list .= "$arr[$i]";
	}
	if ($i != $last) {
	   $list .= ", ";
	}
   }
   return $list;
}

The problem is that if a value in the array is itself an array, for instance a field of type set that has more than one value chosen, it's not listing the values of that array, just the word array. I tried making it a recursive function

if (is_array($arr[$i])) {
   print_array($arr[$i]);
}

but that also just gave me "Array", so I tried duplicating the code, as you can see in the function above, but this didn't work either. I feel like there must be something obvious I'm missing, but I just can't see it.

The code for the form itself is

<select name="Category[]" SIZE=5 MULTIPLE>

so it is being passed as an array, and I can do print_r of the specific variable and see the array values. I just can't get them to print properly in my query.

The other odd thing is that when I do print_r($vals) it also shows me "Array" for the field that is an array, instead of showing me the values. But when I do print_r($Category) it does show me that it is an array.

Can anyone help?

TIA,

-Lisi

--
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