0

my controller function

public function add_books() {

  $this->load->model('User');

  $data['books'] = $this->User->getAuthor();

  $this->load->view('vadd_books',$data);

}

my user_model model

function getAuthor() {

  $this->db->select('author_firstname, author_lastname');

  $this->db->from('authors');

  $query = $this->db->get();

  return $query->result();

}
6
  • if your result is more than one you need to use foreach loop and display it in the html table. Commented Mar 18, 2017 at 6:24
  • I already used foreach in my view. Should I post my view here? Commented Mar 18, 2017 at 6:26
  • yes can you post it? Commented Mar 18, 2017 at 6:28
  • could you please check did you got proper data from your model? Commented Mar 18, 2017 at 6:28
  • I got a proper data from my model. My problem is just displaying/using my foreach. Commented Mar 18, 2017 at 6:29

3 Answers 3

0

Change in controller Modelname

public function add_books() {

  $this->load->model('user_model');

  $data['books'] = $this->user_model->getAuthor();

  $this->load->view('vadd_books',$data);

}

Use $this->db->last_query() to print query in model to debug MySQL Query

Sign up to request clarification or add additional context in comments.

2 Comments

But @RobertCapistrano Which answer is correct if any then please mark correct as correct answer ...:)
yes @RobertCapistrano which answer works for you please mark that as correct answer that will also help to other :)
0

You can try with below code

<select class="form-control">
            <?php 

            foreach($books as $row)
            { 
              echo '<option value="'.$row->author_firstname.'">'.$row->author_firstname.'</option>';
            }
            ?>
            </select>

For more reference refer url : http://stackoverflow.com/questions/19922143/display-data-from-database-to-dropdown-codeigniter

Comments

0

Try this one code. i try to explain step by step.

Model

public function getAuthor() {

   $this->db->select('author_firstname, author_lastname');
   $this->db->from('authors');
   $query = $this->db->get();
   return $query->result();
}

Controller

public function add_books() {

  $this->load->model('User');
  $data['books'] = $this->User->getAuthor();
  $this->load->view('vadd_books',$data);
}

View

  <select class="form-control my-class">
        <?php 

        foreach($books as $row)
        { 
          echo '<option value="'.$row->author_firstname.'">'.$row->author_firstname.'</option>';
        }
        ?>
        </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.