first thank you for solving privious problems.
I am working on codeigniter . i am making signup form in which countries dropdown list is required. i am unable to make this drop down list with table country in my database.Can any one help me.

Recommended Answers

All 8 Replies

i am making drop down list with code igniter to display countries it is my controller code-

function index()
{
$this->load-> helper(array('form','url'));
$this->load->view('signup_view');
$this->load->model('user');
$data=$this->user->country();


}

and
this is my model file named user code-

function country()
{
$this->db->select('id','country_name');
$records=$this->db->get('countries');
$data=array();
foreach($records->result() as $row)
{
$data[$row->id] = $row->country_name;
}
return ($data);
}

and this is my view file code-

<?php echo form_dropdown('country_name', $country); ?>

It is giving following error

A PHP Error was encountered


Severity: Notice


Message: Undefined property: stdClass::$country_name


Filename: models/user.php


Line Number: 27

and

A PHP Error was encountered


Severity: Notice


Message: Undefined variable: country


Filename: views/signup_view.php


Line Number: 22



and



A PHP Error was encountered


Severity: Warning


Message: Invalid argument supplied for foreach()


Filename: helpers/form_helper.php


Line Number: 331

First of all remember to use CODE to wrap data you post in the forums.
Second: this is wrong $this->db->select('id','country_name'); you should write $this->db->select('id, country_name'); otherwise country_name will be seen as second argument.

Check if this solves the problems. Bye.

First of all remember to use CODE to wrap data you post in the forums.
Second: this is wrong $this->db->select('id','country_name'); you should write $this->db->select('id, country_name'); otherwise country_name will be seen as second argument.

Check if this solves the problems. Bye.

the problem is not solved.

Change the controller function with this:

function index()
{
    $this->load->model('user');
    $this->load->helper(array('form','url'));
    $data['country']=$this->user->country();
    $this->load->view('signup_view',$data);
}

If you load the view before you collect data you will get an error, and you need also to send data array to the view, so you need to use a second parameter in the load->view as I wrote above. If you get new errors post them, bye.

Change the controller function with this:

function index()
{
    $this->load->model('user');
    $this->load->helper(array('form','url'));
    $data['country']=$this->user->country();
    $this->load->view('signup_view',$data);
}

If you load the view before you collect data you will get an error, and you need also to send data array to the view, so you need to use a second parameter in the load->view as I wrote above. If you get new errors post them, bye.

now it is giving the error
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\signup\application\controllers\signup.php on line 14

And what you have on line 14? $data['country']=$this->user->country(); ?
If yes you may want to paste the updated user model code?

And what you have on line 14? $data['country']=$this->user->country(); ?
If yes you may want to paste the updated user model code?

Now it is giving to errror-

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/signup_view.php

Line Number: 24


And

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331

foreach loop is not working and undifined data view file is showing.thanks for replying.

Hello daniel36, you can do this:

in the your view:

foreach($data_db as $dt_db):
$drop[$dt_db->id] = $dt_db->name;
enforeach;

echo form_dropdown('name_dropdown',$drop);

Ready!...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.