Forums

form array[] getvar foreach insert

peers 21 Feb, 2009
Hi,

I have a number of checkboxes in a form, all named regions[]
on submitting the form, this section of POST:array is as it should be [regions] => Array ( [0] => North_East [1] => North_West [2] => Yorkshire_And_The_Humber [3] => Wales [4] => East_Midlands )

I have a number of database tables being writen to with similar syntax as the below(without arrays, without the autogenerated code running, queries are in onsubmit after email)they work ok, it must be the way im handling the array that is not inserting the $regions into the table. Anything obvious you can see wrong with the array?
ChronoFormsv3.0

...
$regions = array();
$regions = JRequest::getVar('regions', array(), 'post', 'array');
foreach($regions as $key=>$region) {
	$query ="INSERT INTO #__sobi2_fields_data
		SET fieldid = '46',
			data_txt = '$region',
			data_char = '',
			itemid = '$sobi_id;";
	$db->setQuery($query); 
	$db->query(); }
...

ive searched everywhere, and tried several variations over a number of weeks, ive just not found anything that clicks and works

also, another quick one..
is it possible to use the array key as a variable such as $key, if its set in the form as say arrayname[key] ? - this could cut the code down by 1000 lines!
many thanks

peers
Max_admin 21 Feb, 2009
Hi peers,

I can see this problem in your code:
itemid = '$sobi_id;";
should be
itemid = '$sobi_id'";//added 1 single quote before the string end!


you can use the $key inside the foreach loop, it will has 0,1,2,3,4 based on the loop number as you can see in the DEBUG code!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
peers 22 Feb, 2009
thanks Max! after so many hours it was a missing '
reference for if useful for anyone else..

...
// all textbox form elements are named txtboxes[key] where key is the sobi2 directory fieldid
$txtboxes = array();
$txtboxes = JRequest::getVar('txtboxes', array(), 'post', 'array');
foreach($txtboxes as $key=>$txtbox) {
   $query ="INSERT INTO #__sobi2_fields_data
      SET fieldid = '$key',
         data_txt = '$txtbox',
         data_char = '',
         itemid = '$sobi_id';";
   $db->setQuery($query); 
   $db->query(); }
...
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger