Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CodeIgniter DropDown Field  Array to string conversion

Posted on 2014-03-02
6
Medium Priority
?
823 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 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
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

 

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

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

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

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 article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

618 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