![]() |
htmlform dropdown problem - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: htmlform dropdown problem (/showthread.php?tid=24986) |
htmlform dropdown problem - El Forum - 11-26-2009 [eluser]The Mask[/eluser] Hi, I have a model Client that has a one-to-many relationship with Sites. So when I wish to add a new Site, I need to select the Client from a dropdown. I can get the dropdown to render but the values of the options don't match the Ids in the table. Instead, they are populated as "0", "1".. etc. Do you know why I'm not getting the correct Ids? Site model class Com3_site extends Datamapper { var $has_one = array( 'com3_client' ); var $validation = array( 'com3_client_id' => array ( 'label' => 'Client', 'type' => 'dropdown' ), etc... ); etc... Client model class Com3_client extends Datamapper { var $has_many = array( 'com3_site' ); etc... Controller code $s = new Com3_site(); $c = new Com3_client(); $c->get(); $this->data['form_fields'] = array( 'com3_client_id' => array( 'list' => $c ), etc...); $this->data['s'] = $s; $this->load->view( $view, $this->data ); View code echo $s->render_form( $form_fields ); If I look at the generated HTML for the dropdown, it looks like this... <select name="com3_client_id" id="com3_client_id"> <option value="0">Client 1</option> <option value="1">Client 2</option> </select> But in the database, Client 1 has an ID of 15 and Client 2 has an ID of 16. So any ideas why the values are set to "0" and "1" and not the IDs? Thanks htmlform dropdown problem - El Forum - 11-27-2009 [eluser]joeizang[/eluser] hey I haven't used datamapper before but I do know that result from a database are multidimensional arrays and view always look for a flat array, i did something similar with a dropdown from a db but I had to in the model do this: Code: $data[$row['id']] = $row['name']; so row is just the variable carrying the fetched row from the db. Hope this helped! :-) htmlform dropdown problem - El Forum - 11-27-2009 [eluser]bEz[/eluser] I presume you're you're using DMZ (Datamapper Extended Version). Your code is written rather unorthodox to the example code provided with DMZ, however you don't need to add the "_id' Remove it from both the validation and form_fields declaration: i.e: Change Code: $this->data[‘form_fields’] = array( ‘com3_client_id’ => array( ‘list’ => $c ), Code: $this->data[‘form_fields’] = array( ‘com3_client’ => array( ‘list’ => $c ), htmlform dropdown problem - El Forum - 11-30-2009 [eluser]The Mask[/eluser] Thanks bEz - spot on! |