Welcome Guest, Not a member yet? Register   Sign In
Select Box Not Populating Options From Query
#1

[eluser]Lane[/eluser]
I have an issue with a form select not populating with options returned from a query.

Below, I have included the controller function, the model function, and the view code for review.

A bit of information. Apache 2.2.19, PHP 5.3.6, MySQL 5.1.7, CodeIgniter 2.1.0

I can take the code segment that populates the select input from the view and put it in a new view with nothing else and use the same model and controller and it works like a charm.

Also, this only seems to happen the first time the view is loaded by the controller. I have input validation on the form and if I submit it with errors, it produces the expected error list and if I click on the select box, it is populated with values. It's almost as if the query never gets executed on the initial page load. I've been staring at this code all day, searching the forums for a solution, and I haven't made any headway. I'm out of ideas. Anything come to mind?

Model Function: doginfo_model.php

Code:
function get_showclasses() {
  $sql = "SELECT name FROM show_classes ORDER BY name ASC";
  
  $query = $this->db->query($sql);
  
  if ($query->num_rows > 0) {
   return $query->result();
  }
  return FALSE;
}

Controller Function: dog_registration.php

Code:
function index() {
  $query = $this->doginfo_model->get_showclasses();
      
  $data['main_content'] = 'doginfo_view';
  $data['query'] = $query;
  $this->load->view('inc/template', $data);
}

View Code: doginfo_view.php (partial)

Code:
echo form_label("<span class='required'>&bull;</span>&nbsp;Show Class: ", 'show_class', $label);
   echo "<div class='style-selected'>";
   echo "<select name='show_class' id='show_class'>";
   echo '<option value="null">Choose a Show Class</option>';
  
   $max = count($query);
      for ($i=0;$i<=$max-1;$i++) {
       echo "<option value='".$query[$i]-&gt;name."'>".$query[$i]->name."</option>\n";
      }
  
   echo "</select>";
   echo "</div>";


Any ideas or suggestions are greatly appreciated.

- Lane




Theme © iAndrew 2016 - Forum software by © MyBB