CodeIgniter Forums
problem drop -down dynamically - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: problem drop -down dynamically (/showthread.php?tid=61910)



problem drop -down dynamically - Delta-51 - 05-29-2015

Hello thank you for welcoming me , I just started programming in php and using Codeigniter , and I have a big problem that is in a registration form can not I add a drop -down gives me the values ​​and names dynamically .
Values ​​and names are in the database . In php without Codeigniter I managed to write the code ( picture attached ) but in Codeigniter not know how. Thanks in advance.


RE: problem drop -down dynamically - Athov - 05-30-2015

Well the easiest without model is

PHP Code:
<?php

//in some controller

function method_name(){
 
 $data['rows'] = $this->db->select('id, name')->get('users');
 
 $this->load->view('some_view'$data);

PHP Code:
<!-- some view -->

<?
php foreach($rows->result_array() as $row): ?>
  <?php echo $row['id']; ?>
  <?php echo $row['name']; ?>
<?php 
endforeach; ?>
<!-- just add the HTML and you are ready--> 



RE: problem drop -down dynamically - wolfgang1983 - 05-30-2015

(05-30-2015, 05:07 AM)Athov Wrote: Well the easiest without model is




PHP Code:
<?php

//in some controller

function method_name(){
 
 $data['rows'] = $this->db->select('id, name')->get('users');
 
 $this->load->view('some_view'$data);

PHP Code:
<!-- some view -->

<?
php foreach($rows->result_array() as $row): ?>
  <?php echo $row['id']; ?>
  <?php echo $row['name']; ?>
<?php 
endforeach; ?>
<!-- just add the HTML and you are ready--> 

Just notice in the image you are loading the database in there not sure why. I would recommend using the config/database.php and autoload the library


RE: problem drop -down dynamically - includebeer - 05-30-2015

If you're new to PHP programming and CodeIgniter, you better learn to do it the right way!

Get the data from a model and return an array in the format expected by the form_dropdown function.
Example of the format expected:
PHP Code:
$options = array(
 
       'small'         => 'Small Shirt',
 
       'med'           => 'Medium Shirt',
 
       'large'         => 'Large Shirt',
 
       'xlarge'        => 'Extra Large Shirt',
); 

And in your view, create the dropdown list with the form_dropdown helper function:
http://www.codeigniter.com/userguide3/helpers/form_helper.html#form_dropdown


PHP Code:
echo form_dropdown('shirts'$options'large'); 

Loading data directly in the controller is bad design and bad practice. Reinventing the wheel to create a dropdown list is also bad practice. I strongly suggest you read the user guide to learn all the features availble in CodeIgniter to make your life easier.


RE: problem drop -down dynamically - Delta-51 - 05-31-2015

PHP Code:
<select name="name" >       
                        <?
php foreach($rows->result_array() as $row): ?>
                           <option value="<?php echo $row['id']; ?>"> <?php echo $row['name']; ?></option>
                        <?php endforeach; ?>
                    </select> 

where mistake