Welcome Guest, Not a member yet? Register   Sign In
problem drop -down dynamically
#1

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.

Attached Files Thumbnail(s)
   
Reply
#2

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--> 
Reply
#3

(This post was last modified: 05-30-2015, 08:26 AM by wolfgang1983.)

(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
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB