?
Solved

CodeIgniter DropDown Field  Array to string conversion

Posted on 2014-03-02
6
Medium Priority
?
807 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
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…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Suggested Courses

752 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