Solved

CodeIgniter DropDown Field  Array to string conversion

Posted on 2014-03-02
6
778 Views
Last Modified: 2014-03-21
Hello,

I am trying to implement a CodeIgniter dropdown field and I am getting a php error and whilst I have solved other php errors, this one is perplexing me and I cannot work it out.

A further clue I am also getting  Invalid argument supplied for foreach(), the insinuation (perhaps) the array has been modified but I cannot figure it out.

Attached is a word doc season screen output, plus the controller, model and view.

If you can help I'll be very pleased to hear from you

Thank you very much

John Collins
Season-screen-output.docx
header2.php
archeryc.php
archerym.php
0
Comment
Question by:johnecollins
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39898649
Hey John,

The way your model is building the array means it's not suitable for the form_dropdown() function. You need to edit your getLeagues() method to something like this:

public function getLeagues() {
	$leagues = array(''=>'Select an Option' );
	$query = $this->db->query('SELECT league FROM archerleagues group by league order by league');

	foreach ($query->result() as $row):
		$leagues[$row->league] = $row->league;
	endforeach;
	
	return $leagues;        
}

Open in new window

0
 

Author Comment

by:johnecollins
ID: 39899086
Hi Chris,

Thanks a lot for the reply unfortunately it didn't help ;-(

But it made me think and I read the CI user guide again and again.

I copied one of the examples from form_dropdown complete with the array definition into my view, and it worked.

Further investigation, comparison with the one that worked the light came on.

Double quotes around the array

<td><?php echo form_dropdown('league', "$leagues", 'P'); ?></td>

echo form_dropdown('shirts', $options, 'large');

no quotes around the array.

I took out the quotes and it worked, so thank you for helping me find the way

That's at least a couple of pints now ;-)
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39899192
Ah. I missed the double quotes, but your array did look a little off as well, so you may still have to change your model method.

Pleased it's working :)
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Accepted Solution

by:
johnecollins earned 0 total points
ID: 39899788
Hi Chris,

I have taken your advice about the array building and changed it.

Thanks a lot for your help, I am always grateful, for any assistance, however afraid no points this time but keep an eye out for the next tragi-comedy question coming soon to a screen near you ;-).

Because I work at home on my own this website is a real godsend because it affords me the opportunity to receive other ideas than my own and the chance to view and search for examples and solutions.

Cheers Chris maybe be in touch soon

John
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39933849
The quotes around your array name was only a small part of the problem. As I've already pointed out, your array was being built incorrectly, so wouldn't have worked anyway.

Not even an acknowledgement of an assist!!!
0
 

Author Closing Comment

by:johnecollins
ID: 39944768
I compared the user guide details on how to implement dropdown fields, I noticed that the arrays in my failing fields were surrounded by double quotes. I deleted the quotes and the fields worked because the problem went away I awarded myself an 'A'
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hasing a url 16 27
Insert won't work due to space in column name 5 21
Places to advertise 6 35
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 33
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

770 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