CodeIgniter Forums
Dont repeat myself...? -- Populating dropdown with DB data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Dont repeat myself...? -- Populating dropdown with DB data (/showthread.php?tid=5144)



Dont repeat myself...? -- Populating dropdown with DB data - El Forum - 01-08-2008

[eluser]rvent[/eluser]
Hello,

I am back at my CI intranet project and i have found this post: http://ellislab.com/forums/viewthread/64256/

I ended up with the following function in the controller:
Code:
function listPrio()
    {
        $dbres = $this->Boards->createBoard();
        $ddmenu = array();
        
        foreach($dbres->result_array() as $row)
        {
            $ddmenu[$row['PartPriorityID']] = $row['PartPriority'];
        }
        
        $data['options'] = $ddmenu;
        $this->load->view('test', $data);
    }

Function in the Model:
Code:
function createBoard()
    {
        $this->db->select('PartPriority, PartPriorityID');
        $this->db->from('PartPriority');
        
        $priority = $this->db->get();
        return $priority;
    }

And view:
Code:
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
    hello
        <form>
        <?php form_dropdown('Prioprity', $options); ?>
        </form>
    </body>
</html>

But i get a blank page..

Any ideas..?


Thanks


Dont repeat myself...? -- Populating dropdown with DB data - El Forum - 01-08-2008

[eluser]rvent[/eluser]
Having the controller the way i do would it allow me to also know the ID of the item selected..?

Thanks


Dont repeat myself...? -- Populating dropdown with DB data - El Forum - 01-08-2008

[eluser]sandwormusmc[/eluser]
Have you tried either returning $priority->result_array() or passing $options->result_array() into the form_dropdown function?

If that doesn't work, check to see what data your view is receiving by doing a print_r($data) or print_r($options) ...


Dont repeat myself...? -- Populating dropdown with DB data - El Forum - 01-08-2008

[eluser]rvent[/eluser]
Well it works now..
function in controller:
Code:
function listPrio()
    {
        $qpriority = $this->Smtm->getPriority();
        $ddmenu = array();
        
        foreach($qpriority->result_array() as $row)
        {
            $ddmenu[$row['PartPriority]] = $row['PartPriority];
        }
        
        $priority['options'] = $ddmenu;
        $this->load->view('test', $priority);
    }

function in model:
Code:
function getPriority()
    {
        $this->db->select('PartPriority, PartPriorityID');
        $this->db->from('PartPriority);
        
        $priority = $this->db->get();
        return $priority;
    }

view:
Code:
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <form>
        <?php echo form_('PartPriority, $options); ?>
        </form>
    </body>
</html>

Now, there are a few of the same kind i will need to do, like partLocation, PartNumber, PartState, PartStatus, etc. Those will only be dropdown since they are static data, but each value represents a different phase during the repair.

How can i avoid to repeat myself on the code...? Since the only thing that will be changing is the table that is being query..?

Basically, i will have a form for a "board" creation, that form will have several fields; the fields mentioned above reside on different tables and are foreign keys on the main table. So i will need to ID and value field of each of those query...

Will i have to repeat myself..? i dont mind repeating myself, but i think it defeats the purpose of the MVC..?

Thanks for any inputs.