CodeIgniter Forums

Full Version: problem drop -down dynamically
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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--> 
(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
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/he...m_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.
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