Welcome Guest, Not a member yet? Register   Sign In
Creating a form dropdown from db
#1

[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.
#2

[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);
}
}
#3

[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);
#4

[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);
    }
#5

[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;
}
#6

[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.
#7

[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.
#8

[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;
#9

[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;
#10

[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




Theme © iAndrew 2016 - Forum software by © MyBB