Welcome Guest, Not a member yet? Register   Sign In
Creating a dynamic drop down list
#1

I'm trying to create a page that you can create a new menu and edit a menu.

In that page I have a drop down list that lists all the menus that has been saved and a textbox where I can add or edit a menu.

The problem I'm having is that I can't get the drop down list to display the menus that are already in the database.

Here is my menus.php controller

Code:
function create()
{
$update_id = $this->uri->segment(3);
$submit = $this->input->post('submit', TRUE);

$query = $this->get('title');
foreach($query->result() as $row){
// $menu_id = $row->id;
// $menu_title = $row->title;
$data['title'] = $row->title;
}

if($submit == "Submit"){
//person has submitted the form
$data = $this->get_data_from_post();
}else{
if(is_numeric($update_id)){
$data = $this->get_data_from_db($update_id);
}
}

if(!isset($data)){
$data = $this->get_data_from_post();
}



$data['update_id'] = $update_id;

//$data['title'] = $this->get('title');

$data['view_file'] = "create";
$this->load->module('templates');
$this->templates->admin_template($data);
}

and this is my create.php view
Code:
<div class="row">
<div class="col-md-12">
<h2>Create Menus</h2>  
<h5>Welcome Jhon Deo , Need to make dynamic. </h5>
</div>
</div>

<hr />

<?php
echo validation_errors("<p style='color: red;'>", "</p>");
echo form_open('menus/submit/'.$update_id);
?>
<div class="row">
<div class="col-md-12">
<form role="form">
<div class="form-group">
<select name="menu_id">
<?php
foreach($title as $titles){
echo '<option value="'.$titles.'">'.$titles.'</option>';
}
?>
</select>


</div>

<div class="form-group">
<label>Title</label>
               <!-- <input class="form-control" /> -->
               <?php
                $data = array(
                'name' => 'title',
                'id' => 'title',
                'value' => $title,
                'class' => 'form-control',
                );

                echo form_input($data);
               ?>
</div>

<?php

$data = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Submit',
'class' => 'btn btn-success',
'style' => 'width: 100%',
);

echo form_submit($data);
?>
</form>
</div>
</div>
<?php
echo form_close();
?>
Reply
#2

(This post was last modified: 11-25-2014, 12:09 PM by Rufnex.)

Dont know exactly what you do, but first of all you should not mix database stuff in the controller. You should have a model for all of your database operations.

In your example you try to get all items from the table "title" and there the field title. In the loop you set a variable to the title and on every iteration its been overwritten. You should save it in an array like

PHP Code:
$titles = array();
foreach(
$query->result() as $row)
{
    
$titles[] = $row->title;
}
$data['titles'] = $titles

In your view you can then loop over the items

PHP Code:
<?php foreach($titles as $title): ?>
    <?php echo $title?><br />
<?php endforeach; ?>

I think you have to do some refactoring to make a clean code for your project ;o)

So as homework have a look at the
Code:
$query->result_array()
method and refactor the code before: http://www.codeigniter.com/userguide3/da...sult_array

Reply
#3

Thanks that worked
Reply




Theme © iAndrew 2016 - Forum software by © MyBB