0

I have a view (myView) in which there is a form. The form action is myController/myFunction1 which is used to validate the input variables in the form and insert it to the database by calling a model function. This works perfectly fine.

Now, I need a dropdown box inside the form, for which the values will be fetched from a table (called business) in the db.

This is the code I wrote in my model to fetch the values

    public function get_dropdown_list() {
        $this -> db -> select('business_name');
        $result = $this -> db -> get('business');
        if ($result -> num_rows() > 0) {
            foreach ($result->result_array() as $row) {
                $new_row['value'] = htmlentities(stripslashes($row['business_name']));
                $row_set[] = $new_row;
            }
        }

        return $row_set;
    }

I'm not entirely sure if this is correct. What I need to know is, if this is correct, what should be the code inside the controller and the view to display the result as a dropdown in the form in the myView. And if this model itself is wrong, how do I get it working?

P.S. : I'm new to CodeIgniter. I have been going through S.O and various other sites to get this thing working for quite a bit of time now. This might seem to be a repeated question for which I'm really sorry, because I could not find a solution from the already available discussions dealing with the same issue. Any help is very much appreciated.

0

2 Answers 2

1

try Model :-

 public function get_dropdown_list() {
    $this -> db -> select('business_name');
    $result = $this -> db -> get('business');
    if ($result -> num_rows() > 0) {
       return $result->result_array();
    }
    else {
      return false;
    } 
}

Controller :- 1. include model in your controller
2. call the function and send data to view.

    $this->load->model('model_name');
    $this->data['dropdown'] = $this->model_name->get_dropdown_list();
    $this->load->view('yourview', $this->data);

get value in view:-

print_r($dropdown) 
Loop your data and make a dropdown
<select name="dropdown">
<?php foreach($dropdown as $d) {?>
<option value="<?php echo $d;?>"><?php echo $d;?></option>
<?php }?>
</select>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This helped me perfectly in getting the array printed in the view file. However, I still can't figure out how to loop the data and make it a drop down. :(
Why are you looping anything? Use the Codeigniter form_dropdown function. ellislab.com/codeigniter/user-guide/helpers/form_helper.html
I was about to come here to say I got it done using the form_dropdown function. :) Thank you so much Rakesh and Rick. :)
I'm sorry I've got one more question. In <option value="<?php echo $d;?>">, how can I get the value to be business_id, which is another column in the business table? (i.e. obviously after selecting business_id along with business_name)
0

Call This function in controller for getting your records from DB

$data['records'] = $this->my_model->get_data();

In my_model.php

function get_data()
    {
        $query = "select * from my_tab";
        $res = $this->db->query($query);

        if ( $res->num_rows ) 
        {
            return $res->row_array();
        }
        return false;
}

In view.php

<select>
 <?for($i=0;$i<count($records);$i++)
    {
    ?>
     <option>$records[$i]->name</option>
    <?php } ?>

</select>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.