Welcome Guest, Not a member yet? Register   Sign In
dynamic form_dropdown()
#1

[eluser]selftaught[/eluser]
I have been trying to figure out how to populate a form_dropdown using the form helper but have had no luck.

Below are the pieces to my project. Any help is greatly appreciated.

controller
Code:
<?php

class Site extends CI_Controller {
    
    function contact_us()
    {
        $data['main_content'] = 'contact_us';
        $this->load->view('template',$data);
    }
    
    function about_us()
    {
        $data['main_content'] = 'about_us';
        $this->load->view('template',$data);        
    }
        
    function trx_form()
    {
        $data['main_content'] = 'trx_form';
        $this->load->view('template',$data);
        
        $this->load->model('trx_model');
        
        $data['query'] = $this->trx_model->get_markets();
        $this->load->view('trx_form',$data);
    }
}

model
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class trx_model extends CI_Model {

        function __construct()
        {
                // Call the Model constructor
                parent::__construct();
        }
        
        function get_markets()
        {
            $sql = "SELECT DISTINCT AASwitch FROM audit.btseqp";
            $query = $this->db->query($sql);
            return $query->result();
        }
}
view
Code:
<?php foreach($query as $market)
{
    echo form_dropdown('shirts', $market, 'large');
}
?>
The menu is being created but each element is in its own select box.
Also below are the errors I am getting from PHP.
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: query

Filename: views/trx_form.php

Line Number: 1

A PHP Error was encountered
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/trx_form.php

Line Number: 1


#2

[eluser]CroNiX[/eluser]
The 2nd parameter of form_dropdown() needs to be an array of key => value pairs that make up the <options>. So you will need to create an array like that from your $query. And you wouldn't use form_dropdown() in a loop unless you want to make multiple select boxes.

Code:
$markets = array();
foreach($query as $market)
{
    //$markets[key] = value (key is what gets sent in post, value is what you see in the select)
    $markets[$market->AASwitch] = $market->AASwitch;
}
echo form_dropdown('shirts', $markets, 'large');
#3

[eluser]selftaught[/eluser]
Thank you. The menu is now being created, but I have still getting the following errors:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: query

Filename: views/trx_form.php

Line Number: 4

A PHP Error was encountered
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/trx_form.php

Line Number: 4


Any suggestions on how to clear them?

Thanks

-Chris
#4

[eluser]InsiteFX[/eluser]
Do a:
Code:
var_dump($data);
exit;

Before you load your view and see what it returns...

#5

[eluser]selftaught[/eluser]
Thank you for the var_dump trick.

This is what $data contains:

array(2) { ["main_content"]=> string(8) "trx_form" ["query"]=> array(7) { [0]=> object(stdClass)#18 (1) { ["AASwitch"]=> string(3) "ABQ" } [1]=> object(stdClass)#19 (1) { ["AASwitch"]=> string(3) "DEN" } [2]=> object(stdClass)#20 (1) { ["AASwitch"]=> string(3) "LAS" } [3]=> object(stdClass)#21 (1) { ["AASwitch"]=> string(3) "MCI" } [4]=> object(stdClass)#22 (1) { ["AASwitch"]=> string(3) "PHL" } [5]=> object(stdClass)#23 (1) { ["AASwitch"]=> string(3) "PHX" } [6]=> object(stdClass)#24 (1) { ["AASwitch"]=> string(3) "SLC" } } }
#6

[eluser]selftaught[/eluser]
The query dump still doesn't help me understand what I am doing wrong.
I am still getting the following errors:

Seems to me if I can figure why query is being undefined I may be able to resolve this. Any ideas?

Thank you,

-Chris


A PHP Error was encountered
Severity: Notice

Message: Undefined variable: query

Filename: views/trx_form.php

Line Number: 4

A PHP Error was encountered
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/trx_form.php

Line Number: 4

#7

[eluser]GrahamDj28[/eluser]
You said the select box is being filled correctly?

Then the only thing I can think of is that you are loading views/trx_form.php twice.
Once with the $data filled, and a second time with $data being empty.

#8

[eluser]selftaught[/eluser]
I does seem as though I am loading it twice. Looking back at my code snippets do you know how I can fix this?

Sorry for being such a newbie. I hope to pick up on codeigniter very quickly.

Thank you

#9

[eluser]GrahamDj28[/eluser]
Post the complete controller so I can see what you are loading.
Or is the example above the complate controller?
#10

[eluser]selftaught[/eluser]
I found it.

Just needed to update the controller. I was having issues with the way I was generating the views.

Updated controller function below:
Code:
function trx_form()
    {
        $data['main_content'] = 'trx_form';
        //$this->load->view('template',$data);
        
        $this->load->model('trx_model');
        
        $data['query'] = $this->trx_model->get_markets();
        $this->load->view('template',$data,'trx_form',$data);
        
        //var_dump($data);
        //exit;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB