Solved

Insert an array of dates using codeigniter and active record

Posted on 2013-02-03
10
5,697 Views
Last Modified: 2015-08-05
Hi,
I'm painfully new to codeigniter and am trying to take the values from a form and simply insert into my db. Problem is I have one field (dates) which is an array. In the form the user can dynamically add additional date fields so they are submitted as an array ie: dates[]

I am trying to insert into my db using active record but it dies when it hits the array of dates. Not surprisingly...

My controller looks like:
		// If the form has been posted
		if($this->input->post())
		{
			// Set form validation rules
			$this->form_validation
				->set_rules('contestname', 'Contest Name', 'required|min_length[2]|max_length[200]')
				->set_rules('voice', 'Contest Voice', 'required|min_length[2]|max_length[200]')
				->set_rules('winningcaller', 'Winning Caller', 'required|min_length[2]|max_length[200]')
				->set_rules('dialphone', 'Winning Caller Dials', 'required|min_length[2]|max_length[200]')
				->set_error_delimiters('<div class="error">', '</div>');
			

			
			// Run validation
			if ($this->form_validation->run())
			{
				
				
						
					// Form validation passed.
					$data = array(
						'ContestType'		=> set_value('contesttype'),
						'ContestMode'		=> set_value('contesttype'),
						'ContestName'		=> set_value('contestname'),
						'ContestStartDate'	=> set_value('dates'),
						'Voice'				=> set_value('voice'),
						'WinningNumber'		=> set_value('winningnumber'),
						'DialPhone'			=> set_value('dialphone'),
						'ContestStartMessage'	=> set_value('startmessage'),
						'ContestEndMessage'		=> set_value('endmessage'),
						'PostToFacebook'			=> set_value('facebook'),
						'PostToTwitter'			=> set_value('twitter'),
						'PostToWebsite'			=> set_value('website'),
						'PostToTextMessage'		=> set_value('textmessage'),
						'ContestStop'		=> set_value('contestend'),
						'Description'		=> set_value('description'),
						'account_id'	=> set_value($user_id)
					);
					
					// Add new contest
					$this->contest_model->add($data);
					
					
					$page_view_data->show_succeeded = true;
				}
			}
			else
			{
				// Form validation failed
				$page_view_data->show_form_error = true;
			}
		
		
		// Load the page
		$this
			->require_authentication('user/login')
			->set_page('contests/contest_setup', $page_view_data)
			->set_title('Setup a Contest')
			->load_layout();
	}

Open in new window


My model looks like:

	public function add(array $data)
	{
		$this->db->insert('contest_setup', $data);
		
	}

Open in new window


With the model, I don't have a clue how to handle the dates field when it comes over in the $data object.

The error I get is:
Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `contest_setup` (`ContestType`, `ContestMode`, `ContestName`, `ContestStartDate`, `Voice`, `WinningNumber`, `DialPhone`, `ContestStartMessage`, `ContestEndMessage`, `PostToFacebook`, `PostToTwitter`, `PostToWebsite`, `PostToTextMessage`, `ContestStop`, `Description`, `account_id`) VALUES ('1', '1', 'Metallica', Array, '0', '', '9024547474', '', '', '', '', '', '', 'on', '', '')

You can see it says simply 'Array' as the value for ContestStartDate which is the date[] array.

Help....
0
Comment
Question by:tjyoung
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
It depends on how you want the data to be stored.
Multiple values normal ly means multiple records, while you seem to put them all into one. I don't know those classes you are using, so not sure how to solve in either way.
Still hope this gives food to think.
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
Comment Utility
try this way (lose the array keyword):

public function add($data)
	{
		$this->db->insert('contest_setup', $data);
		
	}

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
Comment Utility
THANKS VERY MUCH!! Was suffering on this.
0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Spoke a bit too soon.
the insert works but ignores the dates if I go like this:

ContestStartDate'      => set_value('dates[]'),

if I do this:
ContestStartDate'      => set_value('dates'),

I get the error message again:
Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `contest_setup` (`ContestType`, `ContestMode`, `ContestName`, `ContestStartDate`, `Voice`, `WinningNumber`, `DialPhone`, `ContestStartMessage`, `ContestEndMessage`, `PostToFacebook`, `PostToTwitter`, `PostToWebsite`, `PostToTextMessage`, `ContestStop`, `Description`, `account_id`) VALUES ('1', '1', 'final', Array, '0', '', '9998889999', '', '', '', '', '', '', 'on', '', '17')



Line Number: 43
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
How do you want to store an array in MySql database ? What is the field for this ? You cannot store an array in one column for a single database row.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Hmm, my scenario is:
A user filling out a form has the option of creating additional date fields by clicking 'add another date'. The field name is dates[] which if I understand things right, that creates an array of values (in my case multiple dates).

So, I thought I would insert the array of dates, perhaps separated by a comma. Done this before for adding multiple checkboxes (example below, unrelated app)
if (isset($_REQUEST['hotbuttons'])) {
 $hotbutton= implode(",", $_REQUEST['hotbuttons']); //concat the string using comma
 mysql_query("UPDATE customers SET hotbuttons = '$hotbutton' ");	
}

Open in new window


You can see how using implode, adds the comma in between each of the values of the array and dumps the whole thing into the hotbuttons column. When I want at the values, I just explode on the commas.

What I'm struggling with is how to do the same kind of thing in this case.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Then you need to implode the required dates value just before model adds it to the database (not tested code, but try below)

public function add($data)
{
      if(is_array($data['ContestStartDate'])) $data['ContestStartDate'] = implode(",", $data['ContestStartDate']);
      $this->db->insert('contest_setup', $data);
}

Open in new window


(edited - removed the array keyword as in original suggestion)
0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
That did the trick! Never would have figured it out without your help.
Thanks for your patience.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Glad it worked.
0
 

Expert Comment

by:Puneet Arora
Comment Utility
I m using this same   logic in my data model , but it is storing single /the last value of the multi select
dropdown : Is there a need for some changes in view , where the drop down is been created . Kindly
answer
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now