CodeIgniter Forums
Creating a form dropdown from db - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Creating a form dropdown from db (/thread-27637.html)

Pages: 1 2


Creating a form dropdown from db - El Forum - 02-16-2010

[eluser]Bionicjoe[/eluser]
I've went through several articles on creating forms and dropdowns, but I'm not getting anywhere. I just want to get a list from my database and use the data for the select items.

Here's what I've got.

Controller
Code:
function sitedropdown()
    {
        $this->load->model('outage_model');
        $this->outage_model->sitedropdown();
    }
}

Model
Code:
function sitedropdown()
    {
        $q = $this->db->get('location.sitename');
            return $q->result_array();
    }

View
Code:
<?php
            //Call Submit function from controller.
            echo form_open('outage/submit_addticket');
?>
                <table>
                    <tr><td>Number</td><td>&lt;input type="text" name="ticketnumber"&gt;&lt;/td><tr>
                    <tr><td>Summary</td><td>&lt;input type="text" name="summary"&gt;&lt;/td><tr>
                    <tr><td>Location</td><td>&lt;?php echo form_dropdown('sitedropdown', [b]$somedata[/b]); ?&gt;</td><tr>
                    <tr><td>Start</td><td>&lt;input type="text" name="starttime"&gt;&lt;/td><tr>
                    <tr><td>End</td><td>&lt;input type="text" name="endtime"&gt;&lt;/td><tr>
                    <tr><td></td><td>&lt;input type="submit" value="Submit"&gt;&lt;/td><tr>
                </table>
            &lt;/form&gt;

The $somedata variable produces an error regardless of the variable name. I've tried several things, but I just can't get the data to the view.

If I've totally missed the boat on creating forms tell me. I'm still very new to CI, and have limited coding experience.


Creating a form dropdown from db - El Forum - 02-16-2010

[eluser]jbreitweiser[/eluser]
You have to assign the return from sitedropdown() to a variable and then send it to the view.

function sitedropdown()
{
$this->load->model('outage_model');
$formVar['somedata'] = $this->outage_model->sitedropdown();
$this->load->view('<name of view file>', $formVar);
}
}


Creating a form dropdown from db - El Forum - 02-16-2010

[eluser]danmontgomery[/eluser]
Where are you loading the view? The variable is passed into the view from the controller:

Code:
$data['somedata'] = "some value";
$this->load->view("view_name", $data);



Creating a form dropdown from db - El Forum - 02-16-2010

[eluser]Bionicjoe[/eluser]
What you two have said makes sense, but I am still not getting any data fed to my dropdown.

I am loading my view for the page elsewhere.

This is the view for the Add page.
Code:
function addticket()
    {
        $this->template->set('title', 'Add ticket');
        $this->template->set('heading', 'Add Ticket');
            
        $this->template->load('template', 'addticket_view');
    }

New controller function. Should this be within the 'addticket()' function above?
Code:
function sitedropdown()
    {
        $this->load->model('outage_model');
        $data['somedata'] = $this->outage_model->sitedropdown();
                
        $this->load->view('addticket_view', $data);
    }



Creating a form dropdown from db - El Forum - 02-16-2010

[eluser]flaky[/eluser]
for the dropdown
in the model
Code:
public function sitedropdown(){
$query = $this->db->get('some_table')->result_array();
$result = array();
foreach($query as $q)
      $result[$q['id']] = $q['column_name'];

return $result;
}



Creating a form dropdown from db - El Forum - 02-16-2010

[eluser]Bionicjoe[/eluser]
Ok. I think I'm closer with this, but no matter what I try I get an undefined variable error on my view page.

Controller
Code:
function addticket()
    {
        $this->template->set('title', 'Add ticket');
        $this->template->set('heading', 'Add Ticket');
        
        $this->load->model('outage_model');
        $data['row'] = $this->outage_model->sitedropdown();
            
        $this->template->load('template', 'addticket_view');
    }

Model
Code:
public function sitedropdown()
    {
    //    $q = $this->db->get('location');
    //        return $q->result_array();
            
        $query = $this->db->get('location')->result_array();
        $result = array();
        foreach($query as $q)
         $result[$q['locationid']] = $q['sitename'];

        return $result;
    }

View
Code:
echo form_open('outage/submit_addticket');
...
<tr><td>Location</td><td>&lt;?php echo form_dropdown($data); ?&gt;</td><tr>

I made a typo earlier and got several repeating index errors on the page, so I'm confident I'm getting an array to the page.


Creating a form dropdown from db - El Forum - 02-16-2010

[eluser]laytone[/eluser]
[quote author="Bionicjoe" date="1266378005"]Ok. I think I'm closer with this, but no matter what I try I get an undefined variable error on my view page.

Controller
Code:
function addticket()
    {
        $this->template->set('title', 'Add ticket');
        $this->template->set('heading', 'Add Ticket');
        
        $this->load->model('outage_model');
        $data['row'] = $this->outage_model->sitedropdown();
            
        $this->template->load('template', 'addticket_view');
    }

Model
Code:
public function sitedropdown()
    {
    //    $q = $this->db->get('location');
    //        return $q->result_array();
            
        $query = $this->db->get('location')->result_array();
        $result = array();
        foreach($query as $q)
         $result[$q['locationid']] = $q['sitename'];

        return $result;
    }

View
Code:
echo form_open('outage/submit_addticket');
...
<tr><td>Location</td><td>&lt;?php echo form_dropdown($data); ?&gt;</td><tr>

I made a typo earlier and got several repeating index errors on the page, so I'm confident I'm getting an array to the page.[/quote]

You have to send the array of dropdown options to your view.
Controller
Code:
function addticket()
    {
        $this->template->set('title', 'Add ticket');
        $this->template->set('heading', 'Add Ticket');
        
        $this->load->model('outage_model');
        $this->template->set('dropdownoptions', $this->outage_model->sitedropdown());
            
        $this->template->load('template', 'addticket_view');
    }

Then in your view you have a variable called $dropdownoptions that you send to the form_dropdown() function.


Creating a form dropdown from db - El Forum - 02-17-2010

[eluser]Bionicjoe[/eluser]
Thanks Laytone, but I'm still getting an Undefined variable error in the view.
Tried several things, but still get this error.

The 'addticket_view'
Code:
&lt;?php
            //Call Submit function from controller.
            echo form_open('outage/submit_addticket');
?&gt;
                <table>
                    <tr><td>Number</td><td>&lt;input type="text" name="ticketnumber"&gt;&lt;/td><tr>
                    <tr><td>Summary</td><td>&lt;input type="text" name="summary"&gt;&lt;/td><tr>
                    <tr><td>Location</td><td>&lt;?php echo form_dropdown('options', $dropdownoptions); ?&gt;</td><tr>
                    <tr><td>Start</td><td>&lt;input type="text" name="starttime"&gt;&lt;/td><tr>
                    <tr><td>End</td><td>&lt;input type="text" name="endtime"&gt;&lt;/td><tr>
                    <tr><td></td><td>&lt;input type="submit" value="Submit"&gt;&lt;/td><tr>
                </table>
            &lt;/form&gt;



Creating a form dropdown from db - El Forum - 02-17-2010

[eluser]laytone[/eluser]
Ok, do this and post the results

this will print your $dropdownoptions array above your from, I want to see what it looks like.
Code:
--Add this code--
<pre>
&lt;?php print_r($dropdownoptions); ?&gt;
</pre>
-----------------
&lt;?php
            //Call Submit function from controller.
            echo form_open('outage/submit_addticket');
?&gt;
                <table>
                    <tr><td>Number</td><td>&lt;input type="text" name="ticketnumber"&gt;&lt;/td><tr>
                    <tr><td>Summary</td><td>&lt;input type="text" name="summary"&gt;&lt;/td><tr>
                    <tr><td>Location</td><td>&lt;?php echo form_dropdown('options', $dropdownoptions); ?&gt;</td><tr>
                    <tr><td>Start</td><td>&lt;input type="text" name="starttime"&gt;&lt;/td><tr>
                    <tr><td>End</td><td>&lt;input type="text" name="endtime"&gt;&lt;/td><tr>
                    <tr><td></td><td>&lt;input type="submit" value="Submit"&gt;&lt;/td><tr>
                </table>
            &lt;/form&gt;



Creating a form dropdown from db - El Forum - 02-17-2010

[eluser]Bionicjoe[/eluser]
Added code. Same issue.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: dropdownoptions

Filename: views/addticket_view.php

Line Number: 2