Jump to content


Photo

[CI] How to handle form with inputs as array

codeigniter arrays form

  • Please log in to reply
3 replies to this topic

#1 roksieu

roksieu

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 11 February 2013 - 10:33 AM

Hello,

I have a large form that can be built dynamically, user can add multiple fields and now I have problems with saving values to DB.

I'm using codeigniter.

This is my form
Posted Image
and this is part of my controller that handle those inputs:

$from  = array();
		    foreach ($this->input->post('from') as $froms)
		    {
			    $from[] = $froms;
		    }
		    $to = array();
		    foreach ($this->input->post('to') as $tos)
		    {
			    $to[] = $tos;
		    }

		    $step = array();
		    foreach ($this->input->post('step') as $steps)
		    {
			    $step[] = $steps;
		    }

$odvisnost = array();
foreach ($this->input->post('odvisnost') as $dependency)
{
$odvisnost[] = $dependency;
}

And I get error:

A PHP Error was encountered

Severity: Notice
Message: Array to string conversion
Filename: mysql/mysql_driver.php
Line Number: 552


and

A Database Error Occurred

Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `gc_dioptry` (`produkt`, `from`, `to`, `step`, `dependancy`, `value`) VALUES ('1', '2', '3', Array, Array, Array)
Filename: C:\xampp\htdocs\leckacms\system\database\DB_driver.php
Line Number: 330


1st value is "productid"
As you can see it tries to insert 'array' instead of values that I enter, I entered 2,3,4,5 and 6.

My html form is correct with all inputs names as array[] for example:

<input type="text" name="steps[]">


#2 behicthebuilder

behicthebuilder

    Advanced Member

  • Members
  • PipPipPip
  • 156 posts
  • LocationNew Zealand

Posted 12 February 2013 - 03:32 PM

It's not very clear how you are planning to store the values in the database and I can't see exactly which form fields map to which database fields. If each dynamic line should be a separate row in the "gc_dioptry" table then you could try something like this:


// Collect desired post arrays
$from = $this->input->post('from');
$to = $this->input->post('to');
$step = $this->input->post('step');
// etc.

$rows = array();  # we'll store each row in this array

// Setup a counter for looping through post arrays
// If there are x items in one post array (e.g. $from) then the others *should* contain the same number
$row_count = count($from);

for ($i=0; $i < $row_count; $i++) { 
    $rows[] = array(
        'produkt' => '???',
        'from' => $from[$i],
        'to' => $to[$i],
        'step' => $step[$i],
        'dependancy' => '???',
        'value' => '???',
    );
}

$this->db->insert_batch('gc_dioptry', $rows);

You could add some validation like checking that your post values are actually arrays and also check whether array keys ($i in this example) exist before using them. This will prevent errors being thrown.
Start procrastinating early. That way you might actually start something on time...

#3 roksieu

roksieu

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 13 February 2013 - 05:38 AM

Each line of values will be stored in separate row.
For example:
All values from "first option" will be saved to table "first_option"
All values from "second" will be saved to table "second_option"
etc.

With all the fields I will also save product_id (so that I know to which products it goes to).

#4 roksieu

roksieu

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 13 February 2013 - 06:06 AM

Sorry for double post, but I must say thank you behicthebuilder




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com