Solved

CodeIgniter DropDown Field  Array to string conversion

Posted on 2014-03-02
6
768 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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Accepted Solution

by:
johnecollins earned 0 total points
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

743 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

14 Experts available now in Live!

Get 1:1 Help Now