• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving data from a drop down

#1
I created a menu page where it has a drop down menu with a list of menus from the database and it also has a textbox to enter new menus.

The problem I'm having is that I can't seem to figure out how to save my dropdown. So for example I have a menu called "About Us" in the drop down list and I want to create a new menu called "Team", and "Team" is a child of "About Us"

So in my table I would have something like this

Quote:id | parent | title
------------------------
1 | NULL | About Us
2 | 1 | Team

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

        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();
        }


        //$titles = array();

        $query = $this->get('title');
        foreach($query->result() as $row){
            $titles[] = $row->title;
        }

        $data['titles'] = $titles;

        $data['update_id'] = $update_id;

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

    function submit()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');

        if($this->form_validation->run($this) == FALSE){
            $this->create();
        }else{
            $data = $this->get_data_from_post();

            //figure out wat the url is
            //$data['url'] = url_title($data['title']);

            $update_id = $this->uri->segment(3);

            if(is_numeric($update_id)){
                $this->_update($update_id, $data);
            }else{
                $this->_insert($data);
            }

            redirect('menus/manage');
        }
    }

    function delete($update_id)
    {
        $this->_delete($update_id);

        redirect('menus/manage');
    }

function get($order_by) {
$this->load->model('mdl_menus');
$query = $this->mdl_menus->get($order_by);
return $query;
}

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="menus">
            <?php
                foreach($titles as $title){
                    
                    echo "<option value=".$title.">".$title."</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();
?>

If there is any other information you need me to give please let me know
Reply

#2
What do you mean by how to save your dropdown?

Do you mean how to assign a menu item to a parent?
If so, here is what I would do:
- add a dropdown in the menu item creation form
- populate said dropdown with all existing menu items, ids as values, names as...well, names
When inserting check POST data if this dropdown has a chosen value, if yes, set this value to the parent column.
Reply

#3
I add this
Code:
function get_data_from_post()
    {
        $data['title'] = $this->input->post('title', TRUE);
        $data['parent'] = $this->input->post('parent', TRUE);

        if(!isset($data)){
            $data = '';
        }

        return $data;
    }

    function get_data_from_db($update_id)
    {
        $query = $this->get_where($update_id);

        foreach($query->result() as $row){
            $data['title'] = $row->title;
            $data['parent'] = $row->parent;
        }

        return $data;
    }

to my controller, which should check the items.

When I tried to save a menu item with a parent I got this error
Quote:Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (`webkrunch_ci`.`webkrunc_menus`, CONSTRAINT `webkrunc_menus_parent` FOREIGN KEY (`parent`) REFERENCES `webkrunc_menus` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

INSERT INTO `webkrunc_menus` (`title`, `parent`) VALUES ('Contact', 0)

Filename: /Applications/MAMP/htdocs/webkrunch-codeigniter/modules/menus/models/mdl_menus.php

Line Number: 44

The error
Code:
INSERT INTO `webkrunc_menus` (`title`, `parent`) VALUES ('Contact', 0)

should look something like this
Code:
INSERT INTO `webkrunc_menus` (`title`, `parent`) VALUES ('Contact', 5)
Reply

#4
Well, you are not sending in the value 5 in the POST data then. But you are sending 0. You have to watch out, that when you send 0, you don't add it to the query.
Reply

#5
Then how would I send the correct id to the parent column?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.