Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CodeIgniter DropDown Field  Array to string conversion

Posted on 2014-03-02
6
Medium Priority
?
828 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 44

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 44

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 44

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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.
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 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